windows编程中如何建一个子窗口并显示它啊。下面这样没有显示。求大神帮忙。。
#include<windows.h>LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);LRESULTCALLBACKChi...
#include<windows.h>
LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);LRESULT CALLBACK ChildWndProc(HWND,UINT,WPARAM,LPARAM);
TCHAR szChildclass[]=TEXT("dsjjkjkj");
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow){
static TCHAR szAppName[]=TEXT("HelloIn"); HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style =CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc =WndProc;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hInstance =hInstance;
wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL,TEXT("This program requires windows NT!"),
szAppName,MB_ICONERROR);
return 0;
}
wndclass.lpfnWndProc=ChildWndProc;
wndclass.lpszClassName=szChildclass;
wndclass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
RegisterClass(&wndclass);
hwnd=CreateWindow(szAppName,
TEXT("The hello 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;}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HWND hwndchild;
int x,y;
int iCmdShow;
switch(message)
{
case WM_CREATE:
x=LOWORD(lParam)/2;
y=HIWORD(lParam)/3;
hwndchild=CreateWindow(szChildclass,NULL,WS_CHILDWINDOW | WS_VISIBLE,x/2,y/2,x,y,hwnd,"zyh",
(HINSTANCE) GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
ShowWindow(hwndchild,iCmdShow);
UpdateWindow(hwndchild);
return 0;
case WM_DESTROY: PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
LRESULT CALLBACK ChildWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
return DefWindowProc(hwnd,message,wParam,lParam);
} 展开
LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);LRESULT CALLBACK ChildWndProc(HWND,UINT,WPARAM,LPARAM);
TCHAR szChildclass[]=TEXT("dsjjkjkj");
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow){
static TCHAR szAppName[]=TEXT("HelloIn"); HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style =CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc =WndProc;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hInstance =hInstance;
wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL,TEXT("This program requires windows NT!"),
szAppName,MB_ICONERROR);
return 0;
}
wndclass.lpfnWndProc=ChildWndProc;
wndclass.lpszClassName=szChildclass;
wndclass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
RegisterClass(&wndclass);
hwnd=CreateWindow(szAppName,
TEXT("The hello 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;}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HWND hwndchild;
int x,y;
int iCmdShow;
switch(message)
{
case WM_CREATE:
x=LOWORD(lParam)/2;
y=HIWORD(lParam)/3;
hwndchild=CreateWindow(szChildclass,NULL,WS_CHILDWINDOW | WS_VISIBLE,x/2,y/2,x,y,hwnd,"zyh",
(HINSTANCE) GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
ShowWindow(hwndchild,iCmdShow);
UpdateWindow(hwndchild);
return 0;
case WM_DESTROY: PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
LRESULT CALLBACK ChildWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
return DefWindowProc(hwnd,message,wParam,lParam);
} 展开
2个回答
展开全部
在WndProc中WM_CREATE中的CreateWindow的下面一行你为什么要ShowWindow和UpdateWindow呢?CreateWindow中已经指定了WS_VISIBLE了,况且iCmdShow在WndProc中作为局部变量未经初始化,结果当然不可控了。把那个ShowWindow和UpdateWindow去了就行了。看到你的“This program requires windows NT!”相信你肯定是看的Windows程序设计吧,可以再认真看看子窗口控件部分
展开全部
1.子窗口的ShowWindow(hwndchild,SW_SHOWNORMAL);第二个参数有问题 ,定义了但没初始化
2.
x=((CREATESTRUCT*)lParam)->x; y=((CREATESTRUCT*)lParam)->y;
hwndchild=CreateWindow(szChildclass,NULL,
WS_CHILDWINDOW | WS_VISIBLE,x-100,y-100,x,y,hwnd,NULL,
(HINSTANCE) GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
倒数第3个参数,与sdk的有异,HIWORD 和LOWORD用错,在wm_size时用
建议把x,y换成一个指定值,如果想用lparam,父窗口的 CW_USEDEFAULT最好换成指定值,特别是后2个CW_USEDEFAULT
2.
x=((CREATESTRUCT*)lParam)->x; y=((CREATESTRUCT*)lParam)->y;
hwndchild=CreateWindow(szChildclass,NULL,
WS_CHILDWINDOW | WS_VISIBLE,x-100,y-100,x,y,hwnd,NULL,
(HINSTANCE) GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
倒数第3个参数,与sdk的有异,HIWORD 和LOWORD用错,在wm_size时用
建议把x,y换成一个指定值,如果想用lparam,父窗口的 CW_USEDEFAULT最好换成指定值,特别是后2个CW_USEDEFAULT
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询