读书人

为何子窗口建立失败啊

发布时间: 2013-03-12 11:19:35 作者: rapoo

为什么子窗口建立失败啊???
刚接触这东西,小白一个,为什么子视窗建立不了啊。。。。

#include<windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,PSTR szCmdLine,int iCmdShow)
{
static TCHARszAppName[]=TEXT("ProgramName");
HWNDhwnd;
MSGmsg;
WNDCLASSwndclass;

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.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;

if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("注册视窗失败!"),szAppName,MB_ICONERROR);
return 0;
}

hwnd=CreateWindow(szAppName,TEXT("Program Name"),
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)
{
HDC hdc;
PAINTSTRUCT ps;
HWND hChild;
int X,Y;
RECT rc;

switch(message)
{
case WM_CREATE:
X=LOWORD(lParam);
Y=HIWORD(lParam);
hChild=CreateWindow(TEXT("CHILDWINDOW"),NULL,
WS_CHILD | WS_VISIBLE ,
0,0,80,80,
hwnd,(HMENU)1,(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);

return 0;
case WM_SIZE:

return 0;
case WM_PAINT:
GetWindowRect(hChild,&rc);
hdc=BeginPaint(hwnd,&ps);
Rectangle(hdc,rc.left,rc.top,rc.right,rc.bottom);
MoveToEx(hdc,rc.left,rc.top,NULL);
LineTo(hdc,rc.right,rc.bottom);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}


求解啊啊啊。。。 winapi c
[解决办法]
我把我写得给你参考一下
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndcls;

wndcls.cbClsExtra=0;


wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(0);
wndcls.hCursor=NULL;
wndcls.hIcon=NULL;
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WindowProc;
wndcls.lpszClassName="kevin";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW
[解决办法]
CS_VREDRAW;

RegisterClass(&wndcls);

HWND hwnd;

hwnd=CreateWindow("kevin","sunxin",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

MSG msg;

while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char code is %c",wParam);
MessageBox(hwnd,szChar,"char",0);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
}


[解决办法]
hChild=CreateWindow(TEXT("CHILDWINDOW"),NULL,
WS_CHILD
[解决办法]
WS_VISIBLE ,
0,0,80,80,
hwnd,(HMENU)1,(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);

执行这一句的时候,跟到代码里面去看看。

应该是父窗口这个时候还没有被创建传来,也就是hwnd还是NULL

读书人网 >C++

热点推荐