自制wn32绘画太极图
#include<windows.h>#include<windowsx.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyAppWindows");
HWND hwnd = NULL;
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
if (!RegisterClass(&wc))
{
MessageBox(NULL, TEXT("Need Windows NT!!!!"), TEXT("Error"), MB_OK);
}
hwnd = CreateWindow(szAppName, TEXT("我的窗口"),
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;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static int cxClient, cyClient;//客户区宽度和高度
static int Dbig,Rbig,Rsmall,Rmin;//太极图最大圆形的直径,半径,中间圆形的半径,最小圆形的半径
static int xBigCenter, yBigCenter;//设置大圆的中心,是客户区中心
static int xTopSmallCenter, yTopSmallCenter,xBottomSmallCenter,yBottomSmallCenter;//设置顶部小圆圆心和底部小圆圆心坐标
static int xTopMinCenter, yTopMinCenter,xBottomMinCenter,yBottomMinCenter;//设置顶部和底部最小圆圆心的坐标
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
//画出最大圆形
Ellipse(hdc, xBigCenter - Rbig, yBigCenter - Rbig, xBigCenter + Rbig, yBigCenter + Rbig);
//画出上方小圆,用黑色填充
SelectObject(hdc, GetStockObject(BLACK_BRUSH));
Ellipse(hdc, xTopSmallCenter - Rsmall, yTopSmallCenter - Rsmall, xTopSmallCenter + Rsmall, yTopSmallCenter + Rsmall);
//画出左半边半圆,用黑色填充
Pie(hdc, xBigCenter - Rbig, yBigCenter - Rbig, xBigCenter + Rbig, yBigCenter + Rbig, xBigCenter, yBigCenter - Rbig, xBigCenter, yBigCenter + Rbig);
//画出上方最小圆,用白色填充
SelectObject(hdc, GetStockObject(WHITE_BRUSH));
Ellipse(hdc, xTopMinCenter - Rmin, yTopMinCenter - Rmin, xTopMinCenter + Rmin, yTopMinCenter + Rmin);
//画出下方小半圆
Pie(hdc, xBottomSmallCenter - Rsmall, yBottomSmallCenter - Rsmall, xBottomSmallCenter + Rsmall, yBottomSmallCenter + Rsmall, xBottomSmallCenter, yBottomSmallCenter - Rsmall, xBottomSmallCenter, yBottomSmallCenter + Rsmall);;
//小半圆的直径显示出来了,再贴一条白线盖住
for (size_t i = yBottomSmallCenter - Rsmall; i <= yBottomSmallCenter + Rsmall; i++)
{
SetPixel(hdc, xBottomSmallCenter - 1, i, RGB(255, 255, 255));//发现咋也盖不住,尝试了半天,才发现减去1就可以盖住了,有哪位大佬知道这是为什么吗??
}
//画出底部最小圆,用黑色填充
SelectObject(hdc, GetStockObject(BLACK_BRUSH));
Ellipse(hdc, xBottomMinCenter - Rmin, yBottomMinCenter - Rmin, xBottomSmallCenter + Rmin, yBottomSmallCenter + Rmin);
ReleaseDC(hwnd, hdc);
return 0;
case WM_SIZE:
cxClient = LOWORD(lparam);
cyClient = HIWORD(lparam);
//设置大圆圆心坐标
Dbig = (((cxClient > cyClient)?cyClient:cxClient));//直径是客户区短的那一边
Rbig = Dbig / 2;
Rsmall = Rbig / 2;
Rmin = Rsmall / 3;
xBigCenter = cxClient / 2;
yBigCenter = cyClient / 2;
//设置上方小圆圆心坐标
xTopSmallCenter = xBigCenter;
yTopSmallCenter = yBigCenter - Rbig / 2;
//设置上方最小圆圆心坐标
xTopMinCenter = xBigCenter;
yTopMinCenter = yBigCenter - Rbig / 2;
//设置下方小圆圆心坐标(应该是半圆)
xBottomSmallCenter = xBigCenter;
yBottomSmallCenter = yBigCenter + Rbig / 2;
//设置下方最小圆半径
xBottomMinCenter = xBigCenter;
yBottomMinCenter = yBigCenter + Rbig / 2;
return 0;
case WM_DESTROY://接受WM_DESTROY消息并发送WM_QUIT消息
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
(只有我把小甲鱼的作业认真完成了吗??) {:10_256:}
页:
[1]