关于WINMAIN函数,哪些参数可以修改呢?
<p>#include <windows.h>#include <stdio.h>
LRESULT CALLBACK WinSunProc(HWND , UINT , WPARAM, LPARAM );
</p><p>
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInst, LPSTR lpszCmdLine, int nCmdShow)
{ //定义的参数可以随意更改,要和系统宏定义和系统结构参数区分开来</p><p> //下面的也可以被改变的,如HINSTANCE hh,HINSTANCE xx,LPSTR cc,int dd</p><p> //当然改了以后在这个函数里面相对应的参数也许要改掉
HWND hwnd;
MSGmsg;
WNDCLASS wcex;
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hInstance = hInstance; //对应你上面定义的 HINSTANCE hInstance
wcex.lpfnWndProc = WinSunProc;//对应你声明的回调窗口函数,在上面: LRESULT CALLBACK WinSunProc()
wcex.lpszClassName = "winapp";
wcex.lpszMenuName = NULL;
if (!RegisterClass(&wcex))
{
MessageBeep(0);
return false;
}
hwnd = CreateWindow("winapp", "by 天天", WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;//这里的wParam,不能替换,因为它是MSG结构的成员,并不是下面那一行里面的,它接收的是MSG结构传递的参数
} //看到这个wParam还以为是下面的呢,不是的,不要试图把它改成和下面的一样,下面的可以改变但是它不能被改变</p><p>
LRESULT CALLBACK WinSunProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
{ //这里的hwnd、umsg、wParam、lParam可以随意替换为你想要更改改的值
//比如HWND hh,UINT uu,WPARAM ww,LPARAM pp </p><p> //当然改了以后在这个函数里面相对应的参数也许要改掉
switch (umsg)
{
case WM_LBUTTONDOWN:
MessageBox(hwnd, "mouse clicked", "message", 0);
HDC hdc;
hdc = GetDC(hwnd); //不能在响应WM_PAINT消息时调用
TextOut(hdc, 0, 50, "程序员之家", strlen("程序员之家"));
//ReleaseDC(hwnd,hdc);
break;
case WM_CHAR:
char szChar;
sprintf(szChar, "char code is %d", wParam);
MessageBox(hwnd, szChar, "char", 0);
break;
case WM_PAINT:
PAINTSTRUCT ps;
HDC hDC;
hDC = BeginPaint(hwnd, &ps);
TextOut(hDC,10,10, "有没有人曾告诉你!", strlen("有没有人曾告诉你"));
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);
break;
}
return 0;
}</p>
看到里面的参数都是一个名字还以为都是同一个呢,自己改了以后发现并不是的,这样对于加深你的理解会有帮助的{:5_92:} 修改了一下对照着看看吧
#include <Windows.h>
#include <stdio.h>
LRESULT CALLBACK WinHeProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE dyg, HINSTANCE deg, LPSTR mlh, int xs)
{
WNDCLASS cklx;
cklx.style = CS_HREDRAW | CS_VREDRAW;
cklx.cbClsExtra = 0;
cklx.cbWndExtra = 0;
cklx.hbrBackground = (HBRUSH)(GetStockObject(BLACK_BRUSH));
cklx.hCursor = LoadCursor(NULL, IDC_APPSTARTING);
cklx.hIcon = LoadIcon(NULL, IDI_HAND);
cklx.hInstance = dyg;
cklx.lpfnWndProc = WinHeProc;
cklx.lpszClassName = "dygcx";
cklx.lpszMenuName = NULL;
if (!RegisterClass(&cklx))
{
MessageBeep(0);
return false;
}
HWND ckjb;
ckjb = CreateWindow("dygcx", "第一个程序", WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, dyg, NULL);
ShowWindow(ckjb, SW_SHOWNORMAL);
UpdateWindow(ckjb);
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WinHeProc(HWND ckjb, UINT umsg, WPARAM cdcs, LPARAM cscs)
{
switch (umsg)
{
case WM_PAINT:
HDC ht;
PAINTSTRUCT tu;
ht = BeginPaint(ckjb, &tu);
EndPaint(ckjb, &tu);
break;
case WM_CHAR:
char zf;
sprintf(zf, "按下了%c键", cdcs);
MessageBox(ckjb, zf,"键盘消息", 0);
break;
case WM_LBUTTONDOWN:
MessageBox(ckjb, "鼠标左键按下了。。。。", "鼠标消息", MB_YESNO);
break;
case WM_CLOSE:
if (IDYES == MessageBox(ckjb, "是否退出程序?", "退出程序", MB_YESNO))
{
DestroyWindow(ckjb);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(ckjb, umsg, cdcs, cscs);
break;
}
return 0;
}
页:
[1]