我用SDK做的一个小的基于Dialog程序,但是因为用API去写窗体上的控件很麻烦,所以我用了一个自己已经设计好的Dialog来做主窗体,可是不知为什么窗体上的控件会无法切换焦点。而且在主消息函数中不能使用 DefWindowProc 来处理其它消息,一用这个函数系统界面就没有办法完全出现,会处于一种似乎在不断刷新的状态,怎么回事?ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)MainProc;//WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCSTR)IDI_ICON1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCSTR)IDI_ICON1); return RegisterClassEx(&wcex);
}BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
HICON hIcon;
RECT rc;
DWORD dwWidth, dwHeight;
hInst = hInstance;
hWnd=CreateDialog(hInst, MAKEINTRESOURCE(IDD_MAINFRAME),NULL,(DLGPROC)MainProc);
if (!hWnd)
{
return FALSE;
}
hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
mhWnd = hWnd;
SetClassLong(hWnd, GCL_HICON, (long)hIcon);
GetWindowRect(hWnd, &rc);
dwWidth = rc.right - rc.left + 1;
dwHeight = rc.bottom - rc.top + 1;
SetWindowPos(hWnd, HWND_NOTOPMOST,GetSystemMetrics(SM_CXSCREEN) / 2 - dwWidth / 2, GetSystemMetrics(SM_CYSCREEN) / 2 - dwHeight / 2, dwWidth, dwHeight, SWP_SHOWWINDOW);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
return TRUE;
}int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
} while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}LRESULT CALLBACK MainProc(HWND hWnd,UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
.
.
.
case WM_CLOSE:
EndDialog(hWnd, LOWORD(wParam));
PostQuitMessage(0);
return TRUE;
default:
//return DefWindowProc(hWnd, msg, wParam, lParam);
break;
}
return 0;
}
{
WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)MainProc;//WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCSTR)IDI_ICON1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCSTR)IDI_ICON1); return RegisterClassEx(&wcex);
}BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
HICON hIcon;
RECT rc;
DWORD dwWidth, dwHeight;
hInst = hInstance;
hWnd=CreateDialog(hInst, MAKEINTRESOURCE(IDD_MAINFRAME),NULL,(DLGPROC)MainProc);
if (!hWnd)
{
return FALSE;
}
hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
mhWnd = hWnd;
SetClassLong(hWnd, GCL_HICON, (long)hIcon);
GetWindowRect(hWnd, &rc);
dwWidth = rc.right - rc.left + 1;
dwHeight = rc.bottom - rc.top + 1;
SetWindowPos(hWnd, HWND_NOTOPMOST,GetSystemMetrics(SM_CXSCREEN) / 2 - dwWidth / 2, GetSystemMetrics(SM_CYSCREEN) / 2 - dwHeight / 2, dwWidth, dwHeight, SWP_SHOWWINDOW);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
return TRUE;
}int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
} while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}LRESULT CALLBACK MainProc(HWND hWnd,UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
.
.
.
case WM_CLOSE:
EndDialog(hWnd, LOWORD(wParam));
PostQuitMessage(0);
return TRUE;
default:
//return DefWindowProc(hWnd, msg, wParam, lParam);
break;
}
return 0;
}
因为CreateDialog创建的是一个系统预定义Dialog类,系统已定义好了Dialog类的,其中调用了MainProc(),在MainProc()中可以处理一些消息,返回TRUE,不处理的消息,返回FALSE。窗口处理函数调用了DefWindowProc()处理MainProc()没有处理的消息。
CreateDialog创建Dialog时,我不是指定了它使用我的MainProc()吗,MainProc确实只处理一部分的事,我本来也是想把其它消息留给DefWindowProc,可是在MainProc 里返回调用DefWindowProc时就会出现上面说的好像在不断的刷新界面的现象,就是整个完整的界面出不出,窗口不停的闪。
不知我的解释是否和理,如果是这样的话要怎么解决呢?