📄 enummetafile.cpp
字号:
// enummetafile.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
HENHMETAFILE g_hMetaFile = NULL;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
void ExitInstance();
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
HENHMETAFILE CreateEnhMetaFile(HWND hWnd);
void OnPaint(HWND hWnd);
int CALLBACK PlayMetaFileRecordProc(HDC hDC, HANDLETABLE* phTable,
const ENHMETARECORD* pRecord, int numObjects, LPARAM lpClientData);
//
// WinMain: Entry point for the application
//
int APIENTRY WinMain
(
HINSTANCE hInstance
,HINSTANCE /*hPrevInstance*/
,LPSTR /*lpCmdLine*/
,int nCmdShow
)
{
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_ENUMMETAFILE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ENUMMETAFILE);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
ExitInstance();
return msg.wParam;
}
//
// MyRegisterClass: registers the window class
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_ENUMMETAFILE);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_ENUMMETAFILE;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// InitInstance: creates the window
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
BOOL bReturn(FALSE);
// Store instance handle in our global variable
hInst = hInstance;
HWND hWnd = CreateWindow(szWindowClass, szTitle,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT,
0, NULL, NULL, hInstance, NULL);
if (hWnd)
{
g_hMetaFile = CreateEnhMetaFile(hWnd);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
bReturn = TRUE;
}
return bReturn;
}
//
// ExitInstance: cleans up the resources created
//
void ExitInstance()
{
if (g_hMetaFile != NULL)
{
DeleteEnhMetaFile(g_hMetaFile);
}
}
//
// WndProc: main window window procedure
//
LRESULT CALLBACK WndProc
(
HWND hWnd
,UINT message
,WPARAM wParam
,LPARAM lParam
)
{
int wmId, wmEvent;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX,
hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(
hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
OnPaint(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//
// CreateEnhMetaFile: creates a metafile
//
HENHMETAFILE CreateEnhMetaFile(HWND hWnd)
{
HENHMETAFILE hMetaFile(NULL);
// get a dc for the window
HDC hDC = GetDC(hWnd);
// set the mapping mode to himetric since metafiles
// are created in .01 mm units
int mapmodeOld = SetMapMode(hDC, MM_HIMETRIC);
// get the client rect and convert to logical units
RECT rectClientDC;
GetClientRect(hWnd, &rectClientDC);
DPtoLP(hDC, (LPPOINT)&rectClientDC, 2);
// normalize rect, the bottom coordinate will be negative
// in the himetric mapping mode
rectClientDC.bottom *= -1;
// restore the mapping mode
SetMapMode(hDC, mapmodeOld);
// create the enhanced metafile
HDC hDCMeta = CreateEnhMetaFile(hDC, NULL, &rectClientDC, NULL);
if (hDCMeta != NULL)
{
// get the metafile client window
RECT rectClientMeta;
GetClientRect(hWnd, &rectClientMeta);
DPtoLP(hDCMeta, (LPPOINT)&rectClientMeta, 2);
// draw a red line around the border
HPEN hPenRedDot = CreatePen(PS_DOT, 1, RGB(255,0,0));
HPEN hPenOld = (HPEN)SelectObject(hDCMeta, hPenRedDot);
InflateRect(&rectClientMeta, -10, -10);
Rectangle(hDCMeta, rectClientMeta.left, rectClientMeta.top,
rectClientMeta.right, rectClientMeta.bottom);
// draw an ellipse just inside the border with a dark
// gray border and filled with a light gray brush
InflateRect(&rectClientMeta, -10, -10);
HPEN hPenGrey = CreatePen(PS_SOLID, 3, RGB(128,128,128));
HBRUSH hBrush = (HBRUSH)GetStockObject(DC_BRUSH);
SetDCBrushColor(hDCMeta, RGB(172,172,172));
// select objects
SelectObject(hDCMeta, hPenGrey);
HBRUSH hBrushOld = (HBRUSH)SelectObject(hDCMeta, hBrush);
// draw the ellipse
Ellipse(hDCMeta, rectClientMeta.left, rectClientMeta.top,
rectClientMeta.right, rectClientMeta.bottom);
// select original objects
SelectObject(hDCMeta, hBrushOld);
SelectObject(hDCMeta, hPenOld);
// close the metafile
hMetaFile = CloseEnhMetaFile(hDCMeta);
}
ReleaseDC(hWnd, hDC);
return hMetaFile;
}
//
// OnPaint: handles the painting for the window
//
void OnPaint(HWND hWnd)
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
if (g_hMetaFile != NULL)
{
RECT rectClient;
GetClientRect(hWnd, &rectClient);
EnumEnhMetaFile(hDC, g_hMetaFile,
&PlayMetaFileRecordProc, NULL, &rectClient);
}
EndPaint(hWnd, &ps);
}
//
// PlayMetaFileRecordProc: play the metafile record
//
int CALLBACK PlayMetaFileRecordProc
(
HDC hDC
,HANDLETABLE* phTable
,const ENHMETARECORD* pRecord
,int numObjects
,LPARAM /*lpClientData*/
)
{
switch (pRecord->iType)
{
case EMR_ELLIPSE:
case EMR_RECTANGLE:
case EMR_CREATEPEN:
case EMR_CREATEBRUSHINDIRECT:
default:
PlayEnhMetaFileRecord(
hDC, phTable, pRecord, numObjects);
break;
}
return 1;
}
//
// Mesage handler for about box.
//
LRESULT CALLBACK About
(
HWND hDlg
,UINT message
,WPARAM wParam
,LPARAM /*lParam*/
)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK ||
LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -