学完画刷自己写了一个画图小程序
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
HDC hdc;
PAINTSTRUCT ps;
POINT star;
POINT starcenter;
int r,i;
HBRUSHhRedBrush,hOldBrush,hBrush,hBlueBrush;
HPEN hRedPen, hOldPen;
static intcxClient, cyClient;
switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
//确定五角星半径
if (cxClient >= cyClient)
{
r = cyClient / 5;
}
else
{
r = cxClient / 5;
}
//绘制中心五角心
starcenter.x = cxClient / 2;
starcenter.y = cyClient / 2;
//左上点
star.x = starcenter.x - (int)(r*sin(2 * PI / 5));
star.y = starcenter.y - (int)(r*cos(2 * PI / 5));
//右上点
star.x = starcenter.x + (int)(r*sin(2 * PI / 5));
star.y = starcenter.y - (int)(r*cos(2 * PI / 5));
//左下点
star.x = starcenter.x - (int)(r*sin(PI / 5));
star.y = starcenter.y + (int)(r*cos(PI / 5));
//顶点
star.x = starcenter.x;
star.y = starcenter.y - r;
//右下点
star.x = starcenter.x + (int)(r*sin(PI / 5));
star.y = starcenter.y + (int)(r*cos(PI / 5));
if (cxClient >= cyClient)
{
//画外层红圈
hRedBrush = CreateSolidBrush(RGB(255, 0, 0));
hOldBrush = SelectObject(hdc, hRedBrush);
Ellipse(hdc, (cxClient - cyClient) / 2, 0, (cxClient + cyClient) / 2, cyClient);
SelectObject(hdc, hOldBrush);
//画内存白圈
hBrush = GetStockObject(DC_BRUSH);
hOldBrush = SelectObject(hdc, hBrush);
Ellipse(hdc, (cxClient - cyClient) / 2 + cyClient / 10, cyClient / 10, (cxClient + cyClient) / 2 - cyClient / 10, cyClient * 9 / 10);
SelectObject(hdc, hOldBrush);
//画最内层红圈
hOldBrush = SelectObject(hdc, hRedBrush);
Ellipse(hdc, (cxClient - cyClient) / 2 + cyClient / 5, cyClient / 5, (cxClient + cyClient) / 2 - cyClient / 5, cyClient * 4 / 5);
SelectObject(hdc, hRedBrush);
//画五角星蓝色背景
hBlueBrush = CreateSolidBrush(RGB(0, 0, 255));
hOldBrush = SelectObject(hdc, hBlueBrush);
Ellipse(hdc, (cxClient - cyClient) / 2 + cyClient * 3 / 10, cyClient * 3 / 10, (cxClient + cyClient) / 2 - cyClient * 3 / 10, cyClient * 7 / 10);
DeleteObject(SelectObject(hdc, hOldBrush));
//画中心五角星
SelectObject(hdc, hRedBrush);
SetPolyFillMode(hdc,WINDING);
Polygon(hdc, star, 5);
DeleteObject(SelectObject(hdc, hRedBrush));
//覆盖五角星内部多余的线
hRedPen = CreatePen(PS_SOLID, 0, RGB(255, 0, 0));
hOldPen = SelectObject(hdc, hRedPen);
MoveToEx(hdc, star.x, star.y, NULL);
for (i = 1; i < 5; i++)
{
LineTo(hdc, star.x, star.y);
}
LineTo(hdc, star.x, star.y);
DeleteObject(SelectObject(hdc, hOldPen));
}
else
{
hRedBrush = CreateSolidBrush(RGB(255, 0, 0));
hOldBrush = SelectObject(hdc, hRedBrush);
Ellipse(hdc, 0, (cyClient - cxClient) / 2, cxClient, (cyClient + cxClient) / 2);
SelectObject(hdc, hRedBrush);
hBrush = GetStockObject(DC_BRUSH);
hOldBrush = SelectObject(hdc, hBrush);
Ellipse(hdc, cxClient / 10, (cyClient - cxClient) / 2 + cxClient / 10, cxClient - cxClient / 10, (cyClient + cxClient) / 2 - cxClient / 10);
SelectObject(hdc, hOldBrush);
hOldBrush = SelectObject(hdc, hRedBrush);
Ellipse(hdc, cxClient / 5, (cyClient - cxClient) / 2 + cxClient / 5, cxClient - cxClient * 2 / 10, (cyClient + cxClient) / 2 - cxClient * 2 / 10);
SelectObject(hdc, hOldBrush);
hBlueBrush = CreateSolidBrush(RGB(0, 0, 255));
hOldBrush = SelectObject(hdc, hBlueBrush);
Ellipse(hdc, cxClient * 3 / 10, (cyClient - cxClient) / 2 + cxClient * 3 / 10, cxClient - cxClient * 3 / 10, (cyClient + cxClient) / 2 - cxClient * 3 / 10);
DeleteObject(SelectObject(hdc, hOldBrush));
hOldBrush = SelectObject(hdc, hRedBrush);
SetPolyFillMode(hdc, WINDING);
Polygon(hdc, star, 5);
DeleteObject(SelectObject(hdc, hOldBrush));
hRedPen = CreatePen(PS_SOLID, 0, RGB(255, 0, 0));
hOldPen = SelectObject(hdc, hRedPen);
MoveToEx(hdc, star.x, star.y, NULL);
for (i = 1; i < 5; i++)
{
LineTo(hdc, star.x, star.y);
}
LineTo(hdc, star.x, star.y);
DeleteObject(SelectObject(hdc, hOldPen));
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
} {:10_250:}能再给一个效果图么 1111 大牛感谢分享 运行不起来,一直报错
页:
[1]