//最简单最简洁的一个程序了,请直接看WM_PAINT消息里的代码
#include
#include
const TCHAR szAppName[]=TEXT("HelloCE");
HINSTANCE hInst;
LRESULT CALLBACK MainWndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
HPEN oldPen,newPen;
RECT rt;
LPPAINTSTRUCT ps;
switch(wMsg)
{
case WM_CREATE:
GetClientRect(hWnd,&rt);
break;
case WM_PAINT://这里就是显示一个圆和一行字,但是我这个程序需要点一下开始菜单再关一下才打的开,可以测试一下.本来应
//该是不用这么多hdc,我这里得这样写才行,什么原因啊,请大侠指定一下.
hdc=GetDC(hWnd);//得写2个hdc等于,晕 不这样写点开始菜单也没用无语。
hdc=BeginPaint(hWnd,ps);
newPen=CreatePen(PS_SOLID,1,RGB(255,0,0));
oldPen=(HPEN)SelectObject(hdc,newPen);
Ellipse(hdc,10,10,30,30);
DrawText(hdc,L"ABCDE",strlen("ABCDE"),&rt,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
SelectObject(hdc,oldPen);
EndPaint(hWnd,ps);
ReleaseDC(hWnd,hdc);
break;
case WM_DESTROY:
DeleteObject(oldPen);
DeleteObject(newPen);
break;
}
return DefWindowProc(hWnd,wMsg,wParam,lParam) ;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevIntance,LPWSTR lpCmdLine,int nCmdShow)
{
MSG msg;
int rc=0;
HWND hwndMain;
WNDCLASS wc;
wc.style=0;
wc.lpfnWndProc=MainWndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=NULL;
wc.hCursor=NULL;
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=szAppName;
if (!RegisterClass (&wc))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;
return 0 ;
}
hwndMain=CreateWindow(szAppName,TEXT("Hello"),WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwndMain,nCmdShow);
UpdateWindow(hwndMain);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
点一下开始再关一下开始键,会执行刷新操作。
Invalidate(true) 试试。
- LRESULT CALLBACK MainWndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
- {
- HDC hdc;
- HPEN oldPen,newPen;
- RECT rt;
- PAINTSTRUCT ps;
- switch(wMsg)
- {
- case WM_CREATE:
- GetClientRect(hWnd,&rt);
- newPen=CreatePen(PS_SOLID,1,RGB(255,0,0));
- return 0;
- case WM_PAINT:
- hdc=BeginPaint(hWnd, &ps);
- oldPen=(HPEN)SelectObject(hdc,newPen);
- Ellipse(hdc,10,10,30,30);
- DrawText(hdc, L"ABCDE",strlen("ABCDE"),&rt,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
- SelectObject(hdc,oldPen);
- EndPaint(hWnd, &ps);
- return 0;
- case WM_DESTROY:
- DeleteObject(newPen);
- return 0;
- }
- return DefWindowProc(hWnd,wMsg,wParam,lParam) ;
- }
不用每次都创建pen, 另外将LPPAINTSTRUCT ps ---->>>> PAINTSTRUCT ps