CreateWindow的出错解决
CreateWindow返回NULL,而且GetLastError()也返回0,代码如下:
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, NULL/*gWndProc 注意这里直接把它写成 NULL ,贪方便啊.*/,
0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
classname, NULL
};
RegisterClassEx( &wc );
HWND hWnd = CreateWindow( classname, wndname,
WS_DLGFRAME | WS_SYSMENU, 0, 0,m_ScreenWidth, m_ScreenHeight,
::GetDesktopWindow(), NULL,wc.hInstance, NULL );
把窗口回调函数写成了 NULL ,发现窗口没有显示出来。。,CreateWindow 函数调用 失败...
笨蛋,窗口回调函数都没有.那么消息流向呢.. 这样当然不行的啊..
1 #include <windows.h>
2 #include <stdio.h>
3
4 LRESULT CALLBACK WinSunProc(
5 HWND hwnd, // handle to window
6 UINT uMsg, // message identifier
7 WPARAM wParam, // first message parameter
8 LPARAM lParam // second message parameter
9 );
10
11 int WINAPI WinMain(
12 HINSTANCE hInstance, // handle to current instance
13 HINSTANCE hPrevInstance, // handle to previous instance
14 LPSTR lpCmdLine, // command line
15 int nCmdShow // show state
16 )
17 {
18 WNDCLASS wndcls;
19 wndcls.cbClsExtra=0;
20 wndcls.cbWndExtra=0;
21 wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
22 wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
23 wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
24 wndcls.hInstance=hInstance;
25 wndcls.lpfnWndProc=WinSunProc;
26 wndcls.lpszClassName="sunxin2006";
27 wndcls.lpszMenuName=NULL;
28 wndcls.style=CS_HREDRAW | CS_VREDRAW;
29 RegisterClass(&wndcls);
30
31 HWND hwnd;
32 hwnd=CreateWindow("sunxin2006","http://www.sunxin.org",WS_OVERLAPPEDWINDOW,
33 0,0,600,400,NULL,NULL,hInstance,NULL);
34
35 ShowWindow(hwnd,SW_SHOWNORMAL);
36 UpdateWindow(hwnd);
37
38 MSG msg;
39 while(GetMessage(&msg,NULL,0,0))
40 {
41 TranslateMessage(&msg);
42 DispatchMessage(&msg);
43 }
44 return msg.wParam;
45 }
46
47 LRESULT CALLBACK WinSunProc(
48 HWND hwnd, // handle to window
49 UINT uMsg, // message identifier
50 WPARAM wParam, // first message parameter
51 LPARAM lParam // second message parameter
52 )
53 {
54 switch(uMsg)
55 {
56 case WM_CHAR:
57 char szChar[20];
58 sprintf(szChar,"char code is %d",wParam);
59 MessageBox(hwnd,szChar,"char",0);
60 break;
61 case WM_LBUTTONDOWN:
62 MessageBox(hwnd,"mouse clicked","message",0);
63 HDC hdc;
64 hdc=GetDC(hwnd);
65 TextOut(hdc,0,50,"程序员之家",strlen("程序员之家"));
66 //ReleaseDC(hwnd,hdc);
67 break;
68 case WM_PAINT:
69 HDC hDC;
70 PAINTSTRUCT ps;
71 hDC=BeginPaint(hwnd,&ps);
72 TextOut(hDC,0,0,"http://www.sunxin.org",strlen("http://www.sunxin.org"));
73 EndPaint(hwnd,&ps);
74 break;
75 case WM_CLOSE:
76 if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
77 {
78 DestroyWindow(hwnd);
79 }
80 break;
81 case WM_DESTROY:
82 PostQuitMessage(0);
83 break;
84 default:
85 return DefWindowProc(hwnd,uMsg,wParam,lParam);
86 }
87 return 0;
88 }