- #include<Windows.h>
- #include<iostream>
- #include<cmath>
- using namespace std;
- const int ScreenWidth = 500;
- const int ScreenHeight = 500;
- LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message) {
- case WM_CLOSE:
- DestroyWindow(hWnd);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- break;
- }
- return 0;
- }
- void CirclePoints(HDC hdc, int x, int y,int offx,int offy)//利用对称性画整圆
- {
- SetPixel(hdc, x + offx, y + offy, RGB(0, 0, 0));
- SetPixel(hdc, y + offx, x + offy, RGB(0, 0, 0));
- SetPixel(hdc, x + offx, -y + offy, RGB(0, 0, 0));
- SetPixel(hdc, -y + offx, x + offy, RGB(0, 0, 0));
- SetPixel(hdc, -x + offx, y + offy, RGB(0, 0, 0));
- SetPixel(hdc, y + offx, -x + offy, RGB(0, 0, 0));
- SetPixel(hdc, -x + offx, -y + offy, RGB(0, 0, 0));
- SetPixel(hdc, -y + offx, -x + offy, RGB(0, 0, 0));
- }
- void MidPointCircle(HDC hdc, int x1, int y1, int r)
- {
- int x, y, e;
- x = 0; y = r; e = 1 - r;
- CirclePoints(hdc, x, y, x1, y1);
- while (x <= y)
- {
- if (e < 0)
- e += 2 * x + 3;
- else
- {
- e += 2 * (x - y) + 5;
- y--;
- }
- x++;
- CirclePoints(hdc, x, y, x1, y1);
- }
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nShowCmd)
- {
- WNDCLASS wcs;
- wcs.cbClsExtra = 0; // 窗口类附加参数
- wcs.cbWndExtra = 0; // 窗口附加参数
- wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // 窗口DC背景
- wcs.hCursor = LoadCursor(hInstance, IDC_CROSS); // 鼠标样式
- wcs.hIcon = LoadIcon(NULL, IDI_WINLOGO); // 窗口icon
- wcs.hInstance = hInstance; // 应用程序实例
- wcs.lpfnWndProc = (WNDPROC)WinProc;
- wcs.lpszClassName = "CG";
- wcs.lpszMenuName = NULL;
- wcs.style = CS_VREDRAW | CS_HREDRAW;
- RegisterClass(&wcs);
- HWND hWnd;
- hWnd = CreateWindow("CG", "DrawCircle", WS_OVERLAPPEDWINDOW, 200, 200, ScreenWidth, ScreenHeight, NULL, NULL, hInstance, NULL);
- ShowWindow(hWnd, nShowCmd);
- UpdateWindow(hWnd);
- MSG msg;
- // hdc init
- HDC hdc = GetDC(hWnd);
- MidPointCircle(hdc, 200, 200, 150);
- // 消息循环
- while (GetMessage(&msg, 0, NULL, NULL)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- // release
- ReleaseDC(hWnd, hdc);
- return 0;
- }