Windows程序設計——畫圓

代碼以下:windows

#include<windows.h>
#include<stdlib.h>
#include<string.h>
long WINAPI WndProc
(
	HWND hWnd,
	UINT iMessage,
	UINT wParam,
	LONG lParam
);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);
HWND hWndMain;
int WINAPI WinMain                //主函數
(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow
)
{
	MSG Message;
	if (!InitWindowsClass(hInstance))        return FALSE;
	if (!InitWindows(hInstance, nCmdShow))    return FALSE;
	while (GetMessage(&Message, 0, 0, 0))    //消息循環
	{
		TranslateMessage(&Message);
		DispatchMessage(&Message);
	}
	return Message.wParam;
}
long WINAPI WndProc(HWND hWnd, UINT iMessage,
	UINT wParam, LONG lParam) {
	HDC hDC;          //定義指向設備的句柄
	HBRUSH hBrush;      //定義指向畫刷的句柄
	HPEN hPen;        //定義指向畫筆的句柄
	PAINTSTRUCT PtStr;//定義指向包含繪圖信息的結構體變量
	switch (iMessage)                      //處理消息
	{
	case WM_PAINT:                    //處理繪圖消息
		hDC = BeginPaint(hWnd, &PtStr);
		SetMapMode(hDC, MM_ANISOTROPIC);      //設置映像模式
		hPen = (HPEN)GetStockObject(BLACK_PEN);    //黑色畫筆
		hBrush = (HBRUSH)GetStockObject(DKGRAY_BRUSH); //畫刷
		SelectObject(hDC, hBrush);  //選擇畫刷
		SelectObject(hDC, hPen);      //選擇畫筆
		RoundRect(hDC, 50, 120, 100, 200, 15, 15); //繪製圓角矩形
		hBrush = (HBRUSH)GetStockObject(LTGRAY_BRUSH);  //採用亮灰色畫刷
		SelectObject(hDC, hBrush);        //選擇畫刷
		Ellipse(hDC, 150, 50, 200, 150);       //繪製橢圓
		hBrush = (HBRUSH)GetStockObject(HOLLOW_BRUSH); //虛畫刷
		SelectObject(hDC, hBrush);        //選擇畫刷
		Pie(hDC, 250, 50, 300, 100, 250, 50, 300, 50);      //繪製餅形
		EndPaint(hWnd, &PtStr);      //結束繪圖
		return 0;
	case WM_DESTROY:                 //結束應用程序
		PostQuitMessage(0);    return 0;
	default:                    //其餘消息處理程序
		return(DefWindowProc(hWnd, iMessage, wParam, lParam));
	}
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)//初始化窗口
{
	HWND hWnd;
	hWnd = CreateWindow("WinFill",  //生成窗口
		"填充示例程序",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		0,
		CW_USEDEFAULT,
		0,
		NULL,
		NULL,
		hInstance,
		NULL);
	if (!hWnd)    return FALSE;
	hWndMain = hWnd;
	ShowWindow(hWnd, nCmdShow);    //顯示窗口
	UpdateWindow(hWnd);
	return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)    //定義窗口類
{
	WNDCLASS WndClass;
	WndClass.cbClsExtra = 0;
	WndClass.cbWndExtra = 0;
	WndClass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH));
	WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	WndClass.hIcon = LoadIcon(NULL, "END");
	WndClass.hInstance = hInstance;
	WndClass.lpfnWndProc = WndProc;
	WndClass.lpszClassName = "WinFill";
	WndClass.lpszMenuName = NULL;
	WndClass.style = CS_HREDRAW | CS_VREDRAW;
	return RegisterClass(&WndClass);
	WndClass.hIcon = LoadIcon(NULL, "END");
	WndClass.hInstance = hInstance;
	WndClass.lpfnWndProc = WndProc;
	WndClass.lpszClassName = "WinFill";
	WndClass.lpszMenuName = NULL;
	WndClass.style = CS_HREDRAW | CS_VREDRAW;
	return RegisterClass(&WndClass);
}
相關文章
相關標籤/搜索