Windows应用程序-图形绘制

利用绘图函数创建填充区。共有三个填充图形,第一个是用深灰色画刷填充带圆角的矩形,第二个是采用亮灰色画刷填充一个椭圆型图,第三个是用虚画刷填充饼形图。

然后将上述三个填充图形顺时针旋转90度,显示在原来图形的正下方,边框为蓝色点划线,用绿色水平阴影填充。

MFC绘图

#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 IpCmdLine, 
                   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); // 绘制饼型

		hPen = CreatePen(PS_DASHDOT, 3, RGB(0, 0, 255)); // 蓝色点划线
		hBrush = CreateHatchBrush(HS_HORIZONTAL, RGB(0, 255, 0)); // 绿色水平阴影画刷
		SelectObject(hDC, hBrush); // 选择画刷
		SelectObject(hDC, hPen); // 选择画笔
		RoundRect(hDC, 50, 250, 130, 300, 15, 15); // 旋转90度,绘制圆角矩形

		Ellipse(hDC, 150, 200, 250, 250); // 绘制旋转90度椭圆

		Pie(hDC, 250, 150, 300, 200, 300, 150, 300, 200); // 绘制旋转90度饼型

		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);
}
发帖时间: MFC 归档位置: