//以下是程序代码,可能先要你花上你分钟度懂这个程序,确定读懂后,然后在运行,看看是否与你想的相同。
反正最后运行和我想的有区别,我想了很久都没结果,只有到论坛上请教高手解释这一现象了。。
#include <windows.h>
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
LRESULT CALLBACK BuProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
void PopMes(LPCSTR sText)
{
MessageBox(0,sText,"Attension",MB_OK);
}
HWND hButton;
LONG hBuProc;
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
LRESULT CALLBACK BuProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
void PopMes(LPCSTR sText)
{
MessageBox(0,sText,"Attension",MB_OK);
}
HWND hButton;
LONG hBuProc;
WNDCLASS wnd_class(HINSTANCE hInstance,LPCSTR classname,WNDPROC wndproc)
{
WNDCLASS wc;
wc.style=CS_HREDRAW |CS_VREDRAW;
wc.lpfnWndProc = wndproc;
wc.cbClsExtra= 0;
wc.cbWndExtra=0;
wc.hInstance = hInstance;
wc.hIcon=LoadIcon (NULL, IDI_ASTERISK);
wc.hCursor=LoadCursor (NULL, IDC_APPSTARTING) ;
wc.hbrBackground= (HBRUSH)COLOR_WINDOW;
wc.lpszMenuName=0;
wc.lpszClassName=classname;
return wc;
}
{
WNDCLASS wc;
wc.style=CS_HREDRAW |CS_VREDRAW;
wc.lpfnWndProc = wndproc;
wc.cbClsExtra= 0;
wc.cbWndExtra=0;
wc.hInstance = hInstance;
wc.hIcon=LoadIcon (NULL, IDI_ASTERISK);
wc.hCursor=LoadCursor (NULL, IDC_APPSTARTING) ;
wc.hbrBackground= (HBRUSH)COLOR_WINDOW;
wc.lpszMenuName=0;
wc.lpszClassName=classname;
return wc;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPreInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
LPCSTR CLname="yhy";
if(!RegisterClass(& wnd_class(hInstance,CLname,WndProc)))//注册窗口类型
{
MessageBox(NULL ,"注册失败","提示框",MB_OK);
return 0;
}
HINSTANCE hPreInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
LPCSTR CLname="yhy";
if(!RegisterClass(& wnd_class(hInstance,CLname,WndProc)))//注册窗口类型
{
MessageBox(NULL ,"注册失败","提示框",MB_OK);
return 0;
}
HWND hwnd =CreateWindow(CLname,"My Try Window",WS_OVERLAPPEDWINDOW,
100,300,400,300,NULL,NULL,hInstance,0);//创建窗口
100,300,400,300,NULL,NULL,hInstance,0);//创建窗口
ShowWindow(hwnd, nCmdShow);//显示窗口
UpdateWindow(hwnd);//更新窗口
UpdateWindow(hwnd);//更新窗口
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))//消息循环
{
while (GetMessage (&msg, NULL, 0, 0))//消息循环
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
DispatchMessage (&msg) ;
}
return 0;
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
static bool pt=true;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
switch(uMsg)
{
case WM_CREATE:
hButton =CreateWindow("button","My Button",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0,0,100,30,hwnd,0,((LPCREATESTRUCT)lParam)->hInstance,0);
hBuProc = SetWindowLong(hButton,GWL_WNDPROC,(LONG)BuProc);
break;
case WM_LBUTTONDOWN:
PopMes("lbuttondown");
break;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
static bool pt=true;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
switch(uMsg)
{
case WM_CREATE:
hButton =CreateWindow("button","My Button",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0,0,100,30,hwnd,0,((LPCREATESTRUCT)lParam)->hInstance,0);
hBuProc = SetWindowLong(hButton,GWL_WNDPROC,(LONG)BuProc);
break;
case WM_LBUTTONDOWN:
PopMes("lbuttondown");
break;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
EndPaint (hwnd, &ps) ;
break;
case WM_COMMAND:
PopMes("window");
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
case WM_COMMAND:
PopMes("window");
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
LRESULT CALLBACK BuProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_LBUTTONDOWN:
PopMes("Button");
//SetWindowText(hwnd,"change");
break;
}
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_LBUTTONDOWN:
PopMes("Button");
//SetWindowText(hwnd,"change");
break;
}
return CallWindowProc((WNDPROC)hBuProc,hwnd,uMsg,wParam,lParam);
}
}