16,471
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void PThanshu(HWND);//普通画图函数
void DHChanshu(HWND);//双缓冲画图函数
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName [] = TEXT ("GDI") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("GDI TEST"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
static int cxClient, cyClient;
static int radius=1;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
HINSTANCE hInstance ;
static int pt,gdi;//PT--普通画图,GDI--双缓冲画图
PAINTSTRUCT ps ;
RECT rect;
TCHAR szAppName[]="GDI";
switch (message)
{
case WM_CREATE:
SetTimer(hwnd,1,1000,NULL);
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
return 0 ;
case WM_TIMER:
//GetClientRect(hwnd,&rect);//本打算在这里触发WM_PAINT消息的
// InvalidateRect(hwnd,&rect,TRUE);
if(radius<16)
{radius++;}
else
{radius=0;}
if(pt)
{
PThanshu(hwnd);//普通画图
}
else if(gdi)
{
DHChanshu(hwnd);//双缓冲画图
}
else
return 0;
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_PAINT:
/*if(pt==1)
{
hdc = BeginPaint (hwnd, &ps) ;
for(int x=0;x<cxClient;x+=16)
for(int y=0;y<cyClient;y=+16)
{
Ellipse(hdc,x,y,x+radius,y+radius);
}
EndPaint (hwnd, &ps) ;
}
else
{
hdc=BeginPaint (hwnd, &ps) ;
DrawText(hdc,szAppName,-1,&rect,DT_CENTER);
EndPaint(hwnd,&ps);
}*/
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,szAppName,-1,&rect,DT_CENTER);
EndPaint(hwnd,&ps);
return 0 ;
case WM_COMMAND:
if(lParam==0)
{
switch(wParam)
{
case IDM_PT://普通画图菜单项ID
pt=1;
gdi=0;
return 0;
case IDM_DHC://双缓冲画图菜单项ID
gdi=1;
pt=0;
return 0;
default:
return 0;
}
}
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
void PThanshu(HWND hwnd)
{
HDC hdc=GetDC(hwnd);
RECT rect;
GetClientRect(hwnd,&rect);
for(int x=0;x<cxClient;x+=16)
for(int y=0;y<cyClient;y=+16)
{
Ellipse(hdc,x,y,x+radius,y+radius);
}
ReleaseDC(hwnd,hdc);
}
void DHChanshu(HWND hwnd)//暂且没实现
{
MessageBox(hwnd,TEXT("zhaoxujing"),TEXT("yiruirui"),MB_OK);
}
void DHChanshu(HWND hwnd)//暂且没实现
{
//MessageBox(hwnd,TEXT("zhaoxujing"),TEXT("yiruirui"),MB_OK);
HDC hdc=GetDC(hwnd);
RECT rect;
GetClientRect(hwnd,&rect);
cxClient=rect.right;
cyClient=rect.bottom;
HDC hdcmem=CreateCompatibleDC(hdc);
HBITMAP hbitmap=CreateCompatibleBitmap(hdc,cxClient,cyClient);
SelectObject(hdcmem,hbitmap);
FillRect(hdc,&rect,NULL);
for(int x=0;x<cxClient;x+=16)
for(int y=0;y<cyClient;y+=16)
{
Ellipse(hdcmem,x,y,x+radius,y+radius);
}
BitBlt(hdc,0,0,cxClient,cyClient,hdcmem,0,0,SRCCOPY);
ReleaseDC(hwnd,hdc);
}
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
void PThanshu(HWND hwnd)
{
HDC hdc=GetDC(hwnd);
RECT rect;
GetClientRect(hwnd,&rect);
cxClient = rect.right;//添加这2个就行了
cyClient = rect.bottom;
...
}