|
1鱼币
}@小甲鱼#include <windows.h>
#include<iostream>
typedef struct _QPNode
{
int whiteflag;
int blackflag;
}*QPNode;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstantce,LPSTR lpCmdLine,int nShowCmd)
{
static TCHAR zifu[]=TEXT("我是字符串");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=TEXT("wodechuangkou2");
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.hInstance=hInstance;
wndclass.lpszMenuName=NULL;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("buxing"),TEXT("zhenbuxing"),MB_YESNO);
return 0;
}
hwnd=CreateWindow(
TEXT("wodechuangkou2"),
TEXT("我是个标题"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
int i,j,x,y,h;
HBRUSH brush,oldbrush;
HPEN pen,oldpen;
int xbutton,ybutton;
static int cxClient,cyClient,whofirst;
static _QPNode Qipan[15][15];
switch(message)
{case 1:
for (i=0;i<15;i++)
{
for (j=0;j<15;j++)
{
Qipan[i][j].whiteflag=0;
Qipan[i][j].blackflag=0;
}
}
return 0;
case WM_PAINT:
hdc=BeginPaint (hwnd,&ps);
GetClientRect (hwnd,&rect);
//设置原点为中心和窗口的比值,
SetMapMode(hdc,MM_ISOTROPIC);
SetWindowExtEx(hdc,80,80,NULL);
SetViewportExtEx(hdc,cxClient/2,cyClient/2,NULL);
SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
//花棋盘
x=-80;
y=-80;
for(i=1;i<16;i++)
{
x=x+10;
MoveToEx(hdc,x,-70,NULL);
LineTo(hdc,x,70);
}
for(i=1;i<16;i++)
{
y=y+10;
MoveToEx(hdc,-70,y,NULL);
LineTo(hdc,70,y);
}
//画棋盘小黑点
brush=CreateSolidBrush(RGB(0,0,0));
oldbrush=(HBRUSH)SelectObject(hdc,brush);
Ellipse(hdc,39,39,41,41);
Ellipse(hdc,-39,-39,-41,-41);
Ellipse(hdc,-39,39,-41,41);
Ellipse(hdc,39,-39,41,-41);
Ellipse(hdc,-1,-1,1,1);
SelectObject(hdc,oldbrush);
//画棋子
for (i=0;i<15;i++)
{
for (j=0;j<15;j++)
{
if (Qipan[i][j].blackflag==1)
{
brush=CreateSolidBrush(RGB(0,0,0));
oldbrush=(HBRUSH)SelectObject(hdc,brush);
Ellipse(hdc,-70+(i*10)-3,-70+(j*10)-3,-70+(i*10)+3,-70+(j*10)+3);
SelectObject(hdc,oldbrush);
}
if (Qipan[i][j].whiteflag==1)
{
brush=CreateSolidBrush(RGB(255,255,255));
oldbrush=(HBRUSH)SelectObject(hdc,brush);
Ellipse(hdc,-70+(i*10)-3,-70+(j*10)-3,-70+(i*10)+3,-70+(j*10)+3);
SelectObject(hdc,oldbrush);
}
}
}
EndPaint (hwnd,&ps);
return 0;
case WM_LBUTTONDOWN:
xbutton=LOWORD(lParam);
ybutton=HIWORD(lParam);
if (cyClient>cxClient)
{
ybutton=(((double)ybutton-(double)((cyClient-cxClient)/2))/(double)(cxClient/16))+0.5-1;
xbutton=xbutton/(double)(cxClient/16)+0.5-1;
}else
{
ybutton=(double)ybutton/(double)(cyClient/16)+0.5-1;
xbutton=((double)xbutton-(double)((cxClient-cyClient)/2))/(double)(cyClient/16)+0.5-1;
}
if (whofirst==0)
{
if (xbutton>=0&&ybutton>=0)
{
if (Qipan[xbutton][ybutton].whiteflag!=1&&Qipan[xbutton][ybutton].blackflag!=1)
{
Qipan[xbutton][ybutton].whiteflag=1;
whofirst=1;
}else{break;}
}}else
{
if (xbutton>=0&&ybutton>=0)
{
if (Qipan[xbutton][ybutton].blackflag!=1&&Qipan[xbutton][ybutton].whiteflag!=1)
{
Qipan[xbutton][ybutton].blackflag=1;
whofirst=0;
}else{
break;
}
}
}
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
//case WM_PAINT:
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
求科普!不知道问题所在 |
-
最佳答案
查看完整内容
OK,找到原因了!
原因是每次调用 brush = CreateSolidBrush(RGB(0, 0, 0)); 都会创建一个新的画刷,但在程序结束前并没有被释放掉……因此,多次触发 WM_PAINT 消息调用 CreateSolidBrush() 创建新画刷的结果便是占用了所有的资源(内存泄漏),brush 存放最后一次成功创建的画刷,也就是后边的 brush = CreateSolidBrush(RGB(255, 255, 255)); 因此全部都变成白色的了……
修改方案:
望采纳!
|