可以重点看下我突出的部分:这是一个有对话框的简单示例程序<<windows程序设计示例>>第11章 about2.
case WM_INITDIALOG:
iColor = iCurrentColor ;
iFigure = iCurrentFigure ;
CheckRadioButton (hDlg, IDC_BLACK, IDC_WHITE, iColor) ;
CheckRadioButton (hDlg, IDC_RECT, IDC_ELLIPSE, iFigure) ; hCtrBlock = GetDlgItem (hDlg, IDC_PAINT) ; SetFocus (GetDlgItem (hDlg, iColor)) ; //SetFocus ((HWND)wParam) ; return FALSE ;
这里设置了获得键盘焦点的子控件,为什么还要返回False 根据MSDN上的解析,应该返回TRUE的:
(The dialog box procedure should return TRUE to direct the system to set the keyboard focus to the control specified by wParam. Otherwise, it should return FALSE to prevent the system from setting the default keyboard focus. )而实际上经过测试,返回False是对的,难道MSDN上解析有误吗,所以我迷了,请高人解答,在线等,好了马上结贴给分分啊,谢谢了
以上这个示列的完整的代码,可看看不看啊
#include <Windows.h>
#include "resource.h"LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK AboutDlgProc(HWND,UINT,WPARAM,LPARAM);int iCurrentColor = IDC_BLACK,
iCurrentFigure = IDC_RECT;int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[] = TEXT("About2");
MSG msg;
HWND hwnd;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance,szAppName);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows NT!"),
szAppName,MB_ICONERROR);
return 0;
} hwnd = CreateWindow(szAppName,TEXT("About Box Demo Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL); ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd); while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}void PaintWindow(HWND hwnd,int iColor,int iFigure)
{
static COLORREF crColor[] = {RGB( 0, 0, 0),RGB( 0, 0,255),
RGB( 0,255, 0),RGB( 0,255,255),
RGB(255, 0, 0),RGB(255, 0,255),
RGB(255,255, 0),RGB(255,255,255)}; HBRUSH hBrush;
HDC hdc;
RECT rect; hdc = GetDC(hwnd);
GetClientRect(hwnd,&rect);
hBrush = CreateSolidBrush(crColor[iColor - IDC_BLACK]);
hBrush = (HBRUSH)SelectObject(hdc,hBrush);
if(iFigure ==IDC_RECT)
Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
else
Ellipse(hdc,rect.left,rect.top,rect.right,rect.bottom);
DeleteObject(SelectObject(hdc,hBrush));
ReleaseDC(hwnd,hdc);
}void PaintTheBlock(HWND hCtrl,int iColor,int iFigure)
{
InvalidateRect(hCtrl,NULL,TRUE);
UpdateWindow(hCtrl);
PaintWindow(hCtrl,iColor,iFigure);
}LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static HINSTANCE hInstance;
PAINTSTRUCT ps; switch (message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
return 0; case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_APP_ABOUT:
if(DialogBox(hInstance,TEXT("AboutBox"),hwnd,AboutDlgProc))
InvalidateRect(hwnd,NULL,TRUE);
return 0;
}
break;
case WM_PAINT:
BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps); PaintWindow(hwnd,iCurrentColor,iCurrentFigure);
return 0; case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}BOOL CALLBACK AboutDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
static HWND hCtrBlock;
static int iColor,iFigure; switch(message)
{
case WM_INITDIALOG:
iColor = iCurrentColor ;
iFigure = iCurrentFigure ;
CheckRadioButton (hDlg, IDC_BLACK, IDC_WHITE, iColor) ;
CheckRadioButton (hDlg, IDC_RECT, IDC_ELLIPSE, iFigure) ; hCtrBlock = GetDlgItem (hDlg, IDC_PAINT) ; SetFocus (GetDlgItem (hDlg, iColor)) ;
//SetFocus ((HWND)wParam) ; return FALSE ; case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
iCurrentColor = iColor;
iCurrentFigure = iFigure;
EndDialog(hDlg,TRUE);
return TRUE; case IDCANCEL:
EndDialog(hDlg,FALSE);
return TRUE; case IDC_BLACK:
case IDC_RED:
case IDC_GREEN:
case IDC_YELLOW:
case IDC_BLUE:
case IDC_MAGENTA:
case IDC_CYAN:
case IDC_WHITE:
iColor = LOWORD(wParam);
CheckRadioButton(hDlg,IDC_BLACK,IDC_WHITE,LOWORD(wParam));
PaintTheBlock(hCtrBlock,iColor,iFigure);
return TRUE;
case IDC_RECT:
case IDC_ELLIPSE:
iFigure = LOWORD(wParam);
CheckRadioButton(hDlg,IDC_RECT,IDC_ELLIPSE,LOWORD(wParam));
PaintTheBlock(hCtrBlock,iColor,iFigure);
return TRUE;
}
break;
case WM_PAINT:
PaintTheBlock(hCtrBlock,iColor,iFigure);
break;
}
return FALSE;
}
case WM_INITDIALOG:
iColor = iCurrentColor ;
iFigure = iCurrentFigure ;
CheckRadioButton (hDlg, IDC_BLACK, IDC_WHITE, iColor) ;
CheckRadioButton (hDlg, IDC_RECT, IDC_ELLIPSE, iFigure) ; hCtrBlock = GetDlgItem (hDlg, IDC_PAINT) ; SetFocus (GetDlgItem (hDlg, iColor)) ; //SetFocus ((HWND)wParam) ; return FALSE ;
这里设置了获得键盘焦点的子控件,为什么还要返回False 根据MSDN上的解析,应该返回TRUE的:
(The dialog box procedure should return TRUE to direct the system to set the keyboard focus to the control specified by wParam. Otherwise, it should return FALSE to prevent the system from setting the default keyboard focus. )而实际上经过测试,返回False是对的,难道MSDN上解析有误吗,所以我迷了,请高人解答,在线等,好了马上结贴给分分啊,谢谢了
以上这个示列的完整的代码,可看看不看啊
#include <Windows.h>
#include "resource.h"LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK AboutDlgProc(HWND,UINT,WPARAM,LPARAM);int iCurrentColor = IDC_BLACK,
iCurrentFigure = IDC_RECT;int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[] = TEXT("About2");
MSG msg;
HWND hwnd;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance,szAppName);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows NT!"),
szAppName,MB_ICONERROR);
return 0;
} hwnd = CreateWindow(szAppName,TEXT("About Box Demo Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL); ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd); while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}void PaintWindow(HWND hwnd,int iColor,int iFigure)
{
static COLORREF crColor[] = {RGB( 0, 0, 0),RGB( 0, 0,255),
RGB( 0,255, 0),RGB( 0,255,255),
RGB(255, 0, 0),RGB(255, 0,255),
RGB(255,255, 0),RGB(255,255,255)}; HBRUSH hBrush;
HDC hdc;
RECT rect; hdc = GetDC(hwnd);
GetClientRect(hwnd,&rect);
hBrush = CreateSolidBrush(crColor[iColor - IDC_BLACK]);
hBrush = (HBRUSH)SelectObject(hdc,hBrush);
if(iFigure ==IDC_RECT)
Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
else
Ellipse(hdc,rect.left,rect.top,rect.right,rect.bottom);
DeleteObject(SelectObject(hdc,hBrush));
ReleaseDC(hwnd,hdc);
}void PaintTheBlock(HWND hCtrl,int iColor,int iFigure)
{
InvalidateRect(hCtrl,NULL,TRUE);
UpdateWindow(hCtrl);
PaintWindow(hCtrl,iColor,iFigure);
}LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static HINSTANCE hInstance;
PAINTSTRUCT ps; switch (message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
return 0; case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_APP_ABOUT:
if(DialogBox(hInstance,TEXT("AboutBox"),hwnd,AboutDlgProc))
InvalidateRect(hwnd,NULL,TRUE);
return 0;
}
break;
case WM_PAINT:
BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps); PaintWindow(hwnd,iCurrentColor,iCurrentFigure);
return 0; case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}BOOL CALLBACK AboutDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
static HWND hCtrBlock;
static int iColor,iFigure; switch(message)
{
case WM_INITDIALOG:
iColor = iCurrentColor ;
iFigure = iCurrentFigure ;
CheckRadioButton (hDlg, IDC_BLACK, IDC_WHITE, iColor) ;
CheckRadioButton (hDlg, IDC_RECT, IDC_ELLIPSE, iFigure) ; hCtrBlock = GetDlgItem (hDlg, IDC_PAINT) ; SetFocus (GetDlgItem (hDlg, iColor)) ;
//SetFocus ((HWND)wParam) ; return FALSE ; case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
iCurrentColor = iColor;
iCurrentFigure = iFigure;
EndDialog(hDlg,TRUE);
return TRUE; case IDCANCEL:
EndDialog(hDlg,FALSE);
return TRUE; case IDC_BLACK:
case IDC_RED:
case IDC_GREEN:
case IDC_YELLOW:
case IDC_BLUE:
case IDC_MAGENTA:
case IDC_CYAN:
case IDC_WHITE:
iColor = LOWORD(wParam);
CheckRadioButton(hDlg,IDC_BLACK,IDC_WHITE,LOWORD(wParam));
PaintTheBlock(hCtrBlock,iColor,iFigure);
return TRUE;
case IDC_RECT:
case IDC_ELLIPSE:
iFigure = LOWORD(wParam);
CheckRadioButton(hDlg,IDC_RECT,IDC_ELLIPSE,LOWORD(wParam));
PaintTheBlock(hCtrBlock,iColor,iFigure);
return TRUE;
}
break;
case WM_PAINT:
PaintTheBlock(hCtrBlock,iColor,iFigure);
break;
}
return FALSE;
}
解决方案 »
- VS2005增量编译未能开启
- 请问,用VC编写的程序在运行之后,如何动态地修改它的某一个菜单项的访问键和快捷键呢?
- !!!为什么都找不到成员变量?只有成员函数?在线等!!!!
- 对话框窗口为什么不能处理WM_KEYDOWN消息
- 高分请教!用ADO在一个很大的表(1万多条记录)中查询一些记录(200条),如何高效的实现!
- TAPI的lineConfigDialog函数使用前需要lineInitialize,lineOpen并打开modem电源吗?
- 串口通讯的问题(writefile成功 ,可是readfile得不到结果),
- HELP:有将ANSI C的程序源代码转换为标准C或C++的软件吗? (NULL)
- ------好奇怪,为什么赋值不上??--------
- api函数和mfc中的类成员函数同名但参数,使用起来出错(内详)
- VS2005 新建项目里面怎么什么都没有了。
- 怎么样在视频上加标靶(十字瞄准)
因为你在WM_INITDIALOG设置了控件焦点,所以返回FASE,不让系统再重设默认焦点,否则返回TRUE系统可能会把你刚才设置的焦点改掉。