关于孙鑫视频教程Lesson1中返回值的有关问题
关于孙鑫视频教程Lesson1中返回值的问题
请教下各位大侠,本人初学VC,想问下以下代码(为孙鑫视频教程中的例程)中标红处的返回值具体各代表什么含义啊?尤其是第一个返回值
return msg.wParam ,为啥不是return msg.lParam呢?求高手正解
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
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(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance; //指定包含窗口过程的程序的实例句柄
wndcls.lpfnWndProc=WinSunProc; //定义回调函数
wndcls.lpszClassName="sunxin2006";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow("sunxin2006","http://www.suRLAPPEnxin.org",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;[b][/b]
}
LRESULT CALLBACK WinSunProc(
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 %d",wParam);
MessageBox(hwnd,szChar,"char",MB_OK);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"程序员之家",strlen("程序员之家"));
//ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"http://www.sunxin.org",strlen("http://www.sunxin.org"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam); //除去,则窗口无法显示,只有进程显示。
}
return 0;[b][/b]
}
------解决方案--------------------
第一个作为WinMain的返回值,第二个为窗口回调函数的返回值。
------解决方案--------------------
都是返回值,通过返回值可以告知该函数的执行结果。return msg.wParam 而不是return msg.lParam则是不同的参数代表不同的信息啊
------解决方案--------------------
WM_QUIT
WPARAM wParam
LPARAM lParam;
Parameters
wParam
Specifies the exit code given in the PostQuitMessage function.
lParam
This parameter is not used.
那是因为窗口销毁的消息lParam没用。
------解决方案--------------------
MSG
The MSG structure contains message information from a thread's message queue.
typedef struct tagMSG {
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
请教下各位大侠,本人初学VC,想问下以下代码(为孙鑫视频教程中的例程)中标红处的返回值具体各代表什么含义啊?尤其是第一个返回值
return msg.wParam ,为啥不是return msg.lParam呢?求高手正解
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
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(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance; //指定包含窗口过程的程序的实例句柄
wndcls.lpfnWndProc=WinSunProc; //定义回调函数
wndcls.lpszClassName="sunxin2006";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow("sunxin2006","http://www.suRLAPPEnxin.org",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;[b][/b]
}
LRESULT CALLBACK WinSunProc(
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 %d",wParam);
MessageBox(hwnd,szChar,"char",MB_OK);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"程序员之家",strlen("程序员之家"));
//ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"http://www.sunxin.org",strlen("http://www.sunxin.org"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam); //除去,则窗口无法显示,只有进程显示。
}
return 0;[b][/b]
}
------解决方案--------------------
第一个作为WinMain的返回值,第二个为窗口回调函数的返回值。
------解决方案--------------------
都是返回值,通过返回值可以告知该函数的执行结果。return msg.wParam 而不是return msg.lParam则是不同的参数代表不同的信息啊
------解决方案--------------------
WM_QUIT
WPARAM wParam
LPARAM lParam;
Parameters
wParam
Specifies the exit code given in the PostQuitMessage function.
lParam
This parameter is not used.
那是因为窗口销毁的消息lParam没用。
------解决方案--------------------
MSG
The MSG structure contains message information from a thread's message queue.
typedef struct tagMSG {
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;