请看下头简单的window 代码
请看下面简单的window 代码
#include<windows.h>
#include<stdio.h>
LRESULT CALLBACK WinLinProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lpCmdLine
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_WARNING);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinLinProc;
wndcls.lpszClassName="linzhenjiang";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow("linzhenjiang","第一个Window程序",WS_OVERLAPPEDWINDOW,300,500,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
BOOL bRet;
while(bRet=GetMessageA(&msg,hwnd,0,0!=0)){
if(bRet==-1){
return -1;
}
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
LRESULT CALLBACK WinLinProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char str[20];
sprintf_s(str,"输入的是%d",wParam);
MessageBox(hwnd,str,"键值",MB_OK);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"单机","左单机",MB_OK);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,0,"我的第一个Window程序",strlen("我的第一个Window程序"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDc;
PAINTSTRUCT ps;
hDc=BeginPaint(hwnd,&ps);
TextOut(hDc,100,200,"paint输出的字符串",strlen("paint输出的字符串"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"确定要关闭吗?","提示",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default :
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
我就是想问问 在 这一段
while(bRet=GetMessageA(&msg,hwnd,0,0!=0)){
if(bRet==-1){
return -1;
}
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
中的return -1 是返回哪儿??返回winmain?还是哪儿 为什么可以处理hwnd无效而导致的死循环?请解释清楚。另外 return 可以返回其他的非零值么
------解决方案--------------------
你没有理解什么是callback函数。自己再想下。
#include<windows.h>
#include<stdio.h>
LRESULT CALLBACK WinLinProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lpCmdLine
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_WARNING);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinLinProc;
wndcls.lpszClassName="linzhenjiang";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow("linzhenjiang","第一个Window程序",WS_OVERLAPPEDWINDOW,300,500,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
BOOL bRet;
while(bRet=GetMessageA(&msg,hwnd,0,0!=0)){
if(bRet==-1){
return -1;
}
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
LRESULT CALLBACK WinLinProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char str[20];
sprintf_s(str,"输入的是%d",wParam);
MessageBox(hwnd,str,"键值",MB_OK);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"单机","左单机",MB_OK);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,0,"我的第一个Window程序",strlen("我的第一个Window程序"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDc;
PAINTSTRUCT ps;
hDc=BeginPaint(hwnd,&ps);
TextOut(hDc,100,200,"paint输出的字符串",strlen("paint输出的字符串"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"确定要关闭吗?","提示",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default :
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
我就是想问问 在 这一段
while(bRet=GetMessageA(&msg,hwnd,0,0!=0)){
if(bRet==-1){
return -1;
}
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
中的return -1 是返回哪儿??返回winmain?还是哪儿 为什么可以处理hwnd无效而导致的死循环?请解释清楚。另外 return 可以返回其他的非零值么
------解决方案--------------------
你没有理解什么是callback函数。自己再想下。