GDI的应用
简单画图软件,实现了画笔、直线、圆以及贴图的功能。
解决方案如下图所示:
// Paint.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include <commdlg.h>
#include <Windowsx.h>
#include "DrawDemo.h"
#define MAX_LOADSTRING 100
typedef enum tagDrawType
{
DRAW_NULL,
DRAW_PEN,
DRAW_LINE,
DRAW_ROUND,
DRAW_PIC
} DrawType;
DrawType g_drawType = DRAW_NULL;
BOOL g_bPen = FALSE;
POINT g_ptPenPoint;
BOOL g_bLine = FALSE;
POINT g_ptLineStartPoint, g_ptLineEndPoint;
BOOL g_bRound = FALSE;
POINT g_ptRoundStartPoint, g_ptRoundEndPoint;
BOOL g_bPic = FALSE;
POINT g_ptPicStartPoint, g_ptPicEndPoint;
wchar_t g_wsrePicPath[MAX_PATH] = { 0 };
// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_DRAWDEMO, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DRAWDEMO));
MSG msg;
// 主消息循环:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDC_DRAWDEMO));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_DRAWDEMO);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case ID_PEN:
g_drawType = DRAW_PEN;
break;
case ID_LINE:
g_drawType = DRAW_LINE;
break;
case ID_ROUND:
g_drawType = DRAW_ROUND;
break;
case ID_PICTURE:
{
g_drawType = DRAW_PIC;
OPENFILENAME ofn = { 0 };
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFile = g_wsrePicPath;
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = L"位图(*.bmp)\0*.bmp\0";
ofn.nFilterIndex = 1;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
ofn.nMaxFile = MAX_PATH;
GetOpenFileName(&ofn);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
{
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
switch (g_drawType)
{
case DRAW_PEN:
g_bPen = TRUE;
g_ptPenPoint.x = x;
g_ptPenPoint.y = y;
break;
case DRAW_LINE:
g_bLine = TRUE;
g_ptLineStartPoint.x = x;
g_ptLineStartPoint.y = y;
g_ptLineEndPoint.x = x;
g_ptLineEndPoint.y = y;
break;
case DRAW_ROUND:
g_bRound = TRUE;
g_ptRoundStartPoint.x = x;
g_ptRoundStartPoint.y = y;
g_ptRoundEndPoint.x = x;
g_ptRoundEndPoint.y = y;
break;
case DRAW_PIC:
g_bPic = TRUE;
g_ptPicStartPoint.x = x;
g_ptPicStartPoint.y = y;
g_ptPicEndPoint.x = x;
g_ptPicEndPoint.y = y;
break;
default:
break;
}
}
break;
case WM_MOUSEMOVE:
{
switch (g_drawType)
{
case DRAW_PEN:
if (g_bPen)
{
HDC hdc = GetDC(hWnd);
MoveToEx(hdc, g_ptPenPoint.x, g_ptPenPoint.y, nullptr);
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
LineTo(hdc, x, y);
g_ptPenPoint.x = x;
g_ptPenPoint.y = y;
ReleaseDC(hWnd, hdc);
}
break;
case DRAW_LINE:
if (g_bLine)
{
HDC hdc = GetDC(hWnd);
MoveToEx(hdc, g_ptLineStartPoint.x, g_ptLineStartPoint.y, nullptr);
SelectObject(hdc, GetStockObject(WHITE_PEN));
LineTo(hdc, g_ptLineEndPoint.x, g_ptLineEndPoint.y);
SelectObject(hdc, GetStockObject(BLACK_PEN));
MoveToEx(hdc, g_ptLineStartPoint.x, g_ptLineStartPoint.y, nullptr);
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
LineTo(hdc, x, y);
g_ptLineEndPoint.x = x;
g_ptLineEndPoint.y = y;
ReleaseDC(hWnd, hdc);
}
break;
case DRAW_ROUND:
if (g_bRound)
{
HDC hdc = GetDC(hWnd);
HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, GetStockObject(HOLLOW_BRUSH));
HPEN oldPen = (HPEN)SelectObject(hdc, GetStockObject(WHITE_PEN));
Ellipse(hdc, g_ptRoundStartPoint.x, g_ptRoundStartPoint.y, g_ptRoundEndPoint.x, g_ptRoundEndPoint.y);
SelectObject(hdc, GetStockObject(BLACK_PEN));
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
Ellipse(hdc, g_ptRoundStartPoint.x, g_ptRoundStartPoint.y, x, y);
SelectObject(hdc, oldPen);
SelectObject(hdc, oldBrush);
g_ptRoundEndPoint.x = x;
g_ptRoundEndPoint.y = y;
ReleaseDC(hWnd, hdc);
}
break;
case DRAW_PIC:
if (g_bPic)
{
HDC hdc = GetDC(hWnd);
HBITMAP hbitmap = (HBITMAP)LoadImage(nullptr, g_wsrePicPath, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
HDC hMemDc = CreateCompatibleDC(hdc);
HBITMAP hOldBitMap = (HBITMAP)SelectObject(hMemDc, hbitmap);
BITMAP bmpInfo = { 0 };
GetObject(hbitmap, sizeof(BITMAP), &bmpInfo);
SelectObject(hdc, GetStockObject(WHITE_PEN));
Rectangle(hdc, g_ptPicStartPoint.x, g_ptPicStartPoint.y, g_ptPicEndPoint.x, g_ptPicEndPoint.y);
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
StretchBlt(hdc, g_ptPicStartPoint.x, g_ptPicStartPoint.y, x - g_ptPicStartPoint.x, y - g_ptPicStartPoint.y, hMemDc, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);
g_ptPicEndPoint.x = x;
g_ptPicEndPoint.y = y;
SelectObject(hdc, GetStockObject(BLACK_PEN));
SelectObject(hMemDc, hOldBitMap);
DeleteObject(hbitmap);
DeleteObject(hMemDc);
ReleaseDC(hWnd, hdc);
}
break;
default:
break;
}
}
break;
case WM_LBUTTONUP:
g_bPen = FALSE;
g_bLine = FALSE;
g_bRound = FALSE;
g_bPic = FALSE;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
这个画图软件很简单,但是也有地方值得我们学习:
DC状态的改变
每一次需要改变DC状态时,都需要还原回去。如果不设置回去,就有可能影响下一次的绘制。
画笔的实现
画笔给我们的感觉应该是一个像素点一个像素点来绘制的,但是,实际上是达不到我们想要的效果的。当鼠标移动速度过快时,它就会出现断层,这是因为鼠标的响应速度达不到。画笔其实也是两点之间连线达到的效果。
画图
如果直接在DC上画图,就会明显的感觉到闪烁,我们需要栓缓冲来画图。
双缓存,顾名思义,就是使用两个DC来进行绘制,一个临时内存DC在把一张图画画好之后,然后直接把这一整张图赋值给要显示的DC上,这样就不会出现闪烁的现象了。具体原理网上很多,这里就不做详细介绍了。
Win32文件打开对话框的使用
我们实现了纯Win32 API来创建一个文件打开的对话框,这比在MFC中实现同样效果要复杂很多,希望对大家能在使用Win32 API打开文件对话框有所帮助。
擦除机制
鼠标在移动的时候,都需要将上一次绘制结果给擦除掉,否则就会画出一片片的直线啊、椭圆啊、图片啊等等。