以前写过,不明不白地,现在算是有所了解了。不看书手写了一遍代码:
/*------------------------------------------------------------------
APP.cpp -- Testing Application function.
Muais, 2014
QQ:848506517
--------------------------------------------------------------------*/
#include <windows.h>
LRESULT CALLBACK WndProc( HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
HWND hwnd;
HINSTANCE hInst;
int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
static TCHAR szAppName[]=TEXT("APP");
WNDCLASS wndcls;
MSG msg;
//初始化
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WndProc;
wndcls.lpszClassName=szAppName;
wndcls.lpszMenuName=NULL;
wndcls.style=CS_VREDRAW|CS_HREDRAW;
//注册
if (0==RegisterClass(&wndcls))
{
MessageBox(NULL,TEXT("注册窗口失败!"),TEXT("App Error"),MB_ICONERROR|MB_OK);
return false;
}
hInst=hInstance;
//创建
hwnd=CreateWindow(szAppName,szAppName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,NULL,NULL);
if (NULL==(hwnd))
{
MessageBox(NULL,TEXT("创建窗口失败!"),TEXT("App Error"),MB_ICONERROR|MB_OK);
return false;
}
//显示
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
//消息循环
BOOL bRet;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
MessageBox(hwnd,TEXT("Error"),szAppName,MB_ICONERROR|MB_OK);
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
LRESULT CALLBACK WndProc( HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rt;
static TCHAR info[]=TEXT("App is Runing");
switch (uMsg)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rt);
if (!DrawText(hdc,info,wcslen(info),&rt,DT_VCENTER|DT_SINGLELINE|DT_CENTER))
{
MessageBox(hwnd,TEXT("显示文本出错!"),TEXT("APP ERROR"),MB_ICONERROR|MB_OK);
return false;
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
VS2012 程序运行结果:
Mark:
GetClientRect 函数用于获得窗口显示的矩形区域.