Windows 2D静态图形编程的框架程序(GDI或GDI+)
acmilan2016/08/15软件综合 IP:四川

这个程序的特点:

  1. 使用静态绘制,CPU消耗低
  2. 支持GDI和GDI+绘制方法
  3. 使用简单

使用方法:创建Win32项目,点上空项目,去掉SDL安全检查,然后新建一个cpp文件,贴里边就行了。

<code class="language-cpp">// GDIstatic.cpp - 使用GDI/GDI+进行静态绘制

#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
#include <math.h>

HINSTANCE hInst;
HWND hMainWnd;

static float locx = 320, locy = 240;

// 窗口消息函数,在这里处理窗口消息
static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	if (msg == WM_CREATE) // 窗口创建
	{
		return 0;
	}
	if (msg == WM_KEYDOWN) // 按下键
	{
		if (wParam == VK_LEFT)
		{
			locx -= 5.0f;
			InvalidateRect(hWnd, NULL, FALSE);
			return 0;
		}
		if (wParam == VK_RIGHT)
		{
			locx += 5.0f;
			InvalidateRect(hWnd, NULL, FALSE);
			return 0;
		}
		if (wParam == VK_UP)
		{
			locy -= 5.0f;
			InvalidateRect(hWnd, NULL, FALSE);
			return 0;
		}
		if (wParam == VK_DOWN)
		{
			locy += 5.0f;
			InvalidateRect(hWnd, NULL, FALSE);
			return 0;
		}
	}
	if (msg == WM_PAINT) // 窗口需要重绘
	{
		PAINTSTRUCT ps;
		HDC hdc = BeginPaint(hWnd, &ps);
		
		if (hdc) // 进行图形绘制(这里使用GDI+)
		{
			using namespace Gdiplus;
			Graphics g(hdc);
			g.Clear(Color::White);

			Pen blackpen(Color::Black, 1);

			g.DrawLine(&blackpen, PointF(locx, locy - 100), PointF(locx, locy + 100));
			g.DrawLine(&blackpen, PointF(locx - 100, locy), PointF(locx + 100, locy));
			g.DrawEllipse(&blackpen, RectF(locx - 100, locy - 100, 200, 200));
		}

		EndPaint(hWnd, &ps);
		return 0;
	}
	if (msg == WM_DESTROY) // 窗口被销毁
	{
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

// 程序入口点
int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
	hInst = hInstance;

#ifdef _GDIPLUS_H
	ULONG_PTR gdip_token = 0;
	Gdiplus::Status gdip_status = Gdiplus::GdiplusStartup(&gdip_token, &Gdiplus::GdiplusStartupInput(), NULL);
	if (gdip_status != Gdiplus::Ok)
	{
		MessageBox(NULL, TEXT("GDI+ initialization failed."), TEXT("Error"), MB_ICONHAND);
		return 0;
	}
#endif

	WNDCLASSEX wcex = {
		sizeof wcex,
		CS_VREDRAW|CS_HREDRAW,
		WndProc,
		0, 0,
		hInstance,
		LoadIcon(NULL, IDI_APPLICATION),
		LoadCursor(NULL, IDC_ARROW),
		NULL, // 我们将自己清空窗口,因此不需要背景画刷
		NULL,
		TEXT("MainWndClass"),
		LoadIcon(NULL, IDI_APPLICATION),
	};

	if (!RegisterClassEx(&wcex))
	{
		MessageBox(NULL, TEXT("This program cannot be run in Windows 95/98/Me."), TEXT("Error"), MB_ICONHAND);
		return 0;
	}

	RECT rcWindow = { 0, 0, 640, 480 };
	AdjustWindowRectEx(&rcWindow, WS_OVERLAPPEDWINDOW, FALSE, 0); // 将客户区大小转换为窗口大小
	hMainWnd = CreateWindowEx(
		0, TEXT("MainWndClass"), TEXT("Main Window"), WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top,
		NULL, NULL, hInstance, NULL);

	if (hMainWnd == NULL)
	{
		MessageBox(NULL, TEXT("CreateWindowEx failed."), TEXT("Error"), MB_ICONHAND);
		return 0;
	}

	ShowWindow(hMainWnd, nShowCmd);
	UpdateWindow(hMainWnd);

	MSG msg = { 0 };
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

#ifdef _GDIPLUS_H
	Gdiplus::GdiplusShutdown(gdip_token);
#endif

	return (int)msg.wParam;
}
</math.h></gdiplus.h></windows.h></code>

[修改于 7年7个月前 - 2016/10/25 10:10:09]

来自:计算机科学 / 软件综合
0
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
acmilan
进士 学者 笔友
文章
461
回复
2934
学术分
4
2009/05/30注册,5年3个月前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}