#include "windows.h"
#include "stdio.h"
//定义窗口过程函数;
LRESULT CALLBACK MyWndProc(
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, // pointer to command line
int nCmdShow // show state of window
)
{
//设计一个窗口类;
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_INFORMATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=MyWndProc;
wndcls.lpszClassName="Bacoo";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_VREDRAW | CS_HREDRAW;
//注册窗口类;
RegisterClass(&wndcls);
//创建窗口;
HWND hwnd;
hwnd=CreateWindow("Bacoo","A Simple SingleDocument derived from own code",
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就是把WM_KEYDOWN和WM_KEYUP翻译成WM_CHAR消息,没有该函数就不能产生WM_CHAR消息。
TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK MyWndProc(
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 buf[20];
sprintf(buf,"char is %d",wParam);
MessageBox(hwnd,buf,"bacoo",0);
break;
case WM_LBUTTONDOWN:
//GetDC和ReleaseDC是一对,只要不是响应WM_PAINT消息,都用这一对函数。
MessageBox(hwnd,"mouse clicked","bacoo",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"I'm Bacoo",strlen("I'm Bacoo"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
//BeginPaint和EndPaint也是一对,它们只用在响应WM_PAINT消息中。
HDC hdc2;
PAINTSTRUCT ps;
hdc2=BeginPaint(hwnd,&ps);
TextOut(hdc2,0,0,"Hello",strlen("Hello"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
//把常量放在前面可以防止误写为“=”时产生的错误,这是一个很不错的小技巧
if(IDYES==MessageBox(hwnd,"Are you ready to close this window?","Bacoo",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
//注意:这里必须使用return DefWindowProc(hwnd,uMsg,wParam,lParam);而不能仅仅使用DefWindowProc(hwnd,uMsg,wParam,lParam);
//否则窗口不能正确显示,其实该回调函数的返回值表征了消息相应的结果,因此是有意义的,不能一概返回0。
LRESULT lr=DefWindowProc(hwnd,uMsg,wParam,lParam);
// if(lr!=NULL)
// {
// MessageBox(NULL,"NULL!=DefWindowProc(hwnd,uMsg,wParam,lParam)","Try",0);
// PostQuitMessage(0);
// }
return lr;
}
return 0;
}