mfc里面关于创建窗口控件的问题

CodeZero 发布于 2013/05/19 11:52
阅读 369
收藏 0

遇到的问题是使用在WM_CREATE消息中使用CreateWindow()创建控件,可是运行后并没有显示出来,不知道问题所在,求解!!!下面是代码:

#include<windows.h>
#include<stdio.h>
#include<string.h>
long WINAPI WndProc(HWND hwnd,UINT message,UINT wParam,LONG lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
HWND h;


HINSTANCE hinst;
HWND hwndGroupBox,hwndPush,hwndDefPush,hwndRadio,hwndAutoRadio,hwndCheckBox,hwndAutoCheckBox;
int show;


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG message;
show=nCmdShow;
if(!InitWindowsClass(hInstance)) return FALSE;
if(!InitWindows(hInstance,nCmdShow)) return FALSE;
while(GetMessage(&message,0,0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
long WINAPI WndProc(HWND hwnd,UINT message,UINT wParam,LONG lParam)
{
int x=50,y=30;
switch(message)
{
case WM_CREATE:
hwndPush=CreateWindow("WinFill",
"push button",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
x,y,
240,25,
h,
(HMENU)20,
hinst,
NULL);
hwndDefPush=CreateWindow("WinFill",
"defpush button",
BS_DEFPUSHBUTTON|WS_CHILD|WS_VISIBLE,
x,y+30,
240,25,
h,
(HMENU)21,
hinst,
NULL);
hwndRadio=CreateWindow("WinFill",
"circle button",
BS_RADIOBUTTON|WS_CHILD|WS_VISIBLE,
x,y+60,
240,25,
h,
(HMENU)22,
hinst,
NULL);
hwndAutoRadio=CreateWindow("WinFill",
"auto circle button",
BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE,
x,y+90,
240,25,
h,
(HMENU)23,
hinst,
NULL);
hwndCheckBox=CreateWindow("WinFill",
"check box",
BS_CHECKBOX|WS_CHILD|WS_VISIBLE,
x,y+120,
240,25,
h,
(HMENU)24,
hinst,
NULL);
hwndAutoCheckBox=CreateWindow("WinFill",
"check",
BS_AUTOCHECKBOX|WS_CHILD|WS_VISIBLE,
x,y+150,
240,24,
h,
(HMENU)25,
hinst,
NULL);
hwndGroupBox=CreateWindow("WinFill",
"group box",
BS_GROUPBOX|WS_CHILD|WS_VISIBLE,
x+290,y+30,
100,140,
h,
(HMENU)26,
hinst,
NULL);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case 20:
SetWindowText(hwndPush,"push down");
break;
case 21:
SetWindowText(hwndPush,"defpush down");
break;
case 22:
SendMessage(hwndRadio,BM_SETCHECK,1,0);
break;
case 23:
break;
case 24:
break;
case 25:
break;
}
return 0;
case WM_PAINT:
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,"END");
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName="WinFill";
wndclass.lpszMenuName=NULL;
wndclass.style=0;
return RegisterClass(&wndclass);
}
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("WinFill",
"Example",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
return FALSE;
h=hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
hinst=hInstance;
return true;
}

加载中
0
平原君
平原君
接受到WM_CREATE消息的时候,父窗口还没有创建好,你这个时候创建子控件,当然就不成功了。
0
CodeZero
CodeZero

引用来自“平原君”的答案

接受到WM_CREATE消息的时候,父窗口还没有创建好,你这个时候创建子控件,当然就不成功了。
后面我看了好多遍,发现原来是WM_PAINT消息这里影响了,由于控件都是WM_CREATE时候生成的,而我又在下面定义了处理WM_PAINT消息的项,结果控件生成后,由于窗口显示刷新了用户区,调用了WM_PAINT来处理,结果就把前面的控件给抹去了
返回顶部
顶部