📄 regions.cpp
字号:
// Regions.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <math.h>
#define MAX_LOADSTRING 100
#define PI 3.14159
#define TEXT_FORMAT (DT_SINGLELINE|DT_VCENTER|DT_CENTER)
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void DoRectRegion (HWND hWnd, HDC hdc, bool bInvert = false);
void DoRoundRectRegion (HWND hWnd, HDC hdc, bool bInvert = false);
void DoCircleRegion (HWND hWnd, HDC hdc, bool bInvert = false);
void DoEllipseRegion (HWND hWnd, HDC hdc, bool bInvert = false);
void DoPolyRegion (HWND hWnd, HDC hdc, bool bStar = false, bool bInvert = false);
void DoCloverRegion (HWND hWnd, HDC hdc, bool bInvert = false);
void DrawRadials (HDC hdc, HRGN hRgn, RECT & rc, int nShadow = 4, int nStep = 1);
void DoMappingModes (HWND hWnd, HDC hdc);
void DrawRect (HDC hdc, int x, int y, int width, int depth);
void DoRectanglePath (HWND hWnd, HDC hdc);
void DoTextPath (HWND hWnd, HDC hdc);
void CreateLogFont (HDC hdc, LOGFONT & lf, int nPoints = 10, char *szFace = "Times New Roman Bold");
void DoMouseEvent (HWND hWnd, UINT message, UINT nFlags, POINT & point);
void DoMouseLine (HDC hdc, POINT & ptLineStart, POINT & ptLineEnd);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_REGIONS, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_REGIONS);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
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_REGIONS);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_REGIONS;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int nMode = IDM_REGION_CLEAR;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static bool bInvert = false;
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;
case IDM_REGION_INVERT:
bInvert = bInvert ? false : true;
RECT rc;
GetClientRect(hWnd, &rc);
InvalidateRect (hWnd, &rc, TRUE);
break;
case IDM_REGION_ELLIPSE:
case IDM_REGION_CLEAR:
case IDM_REGION_RECT:
case IDM_REGION_ROUNDRECT:
case IDM_REGION_CIRCLE:
case IDM_REGION_POLY:
case IDM_REGION_STAR:
case IDM_REGION_CLOVER:
case IDM_MAPPING_MODES:
case IDM_PATHS_RECTANGLE:
case IDM_PATHS_TEXT:
bInvert = false;
nMode = wmId;
GetClientRect(hWnd, &rc);
// rc.right /= 2;
InvalidateRect (hWnd, &rc, TRUE);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_MOUSEMOVE:
POINT point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
DoMouseEvent (hWnd, message, wParam, point);
break;
case WM_SIZE:
break;
case WM_CREATE:
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
switch (nMode)
{
default:
case IDM_REGION_CLEAR: // Clear screen
break;
case IDM_REGION_RECT:
DoRectRegion (hWnd, hdc, bInvert);
break;
case IDM_REGION_ROUNDRECT:
DoRoundRectRegion (hWnd, hdc, bInvert);
break;
case IDM_REGION_CIRCLE:
DoCircleRegion (hWnd, hdc, bInvert);
break;
case IDM_REGION_ELLIPSE:
DoEllipseRegion (hWnd, hdc, bInvert);
break;
case IDM_REGION_POLY:
DoPolyRegion (hWnd, hdc, false, bInvert);
break;
case IDM_REGION_STAR:
DoPolyRegion (hWnd, hdc, true, bInvert);
break;
case IDM_REGION_CLOVER:
DoCloverRegion (hWnd, hdc, bInvert);
break;
case IDM_MAPPING_MODES:
DoMappingModes (hWnd, hdc);
break;
case IDM_PATHS_RECTANGLE:
DoRectanglePath (hWnd, hdc);
break;
case IDM_PATHS_TEXT:
DoTextPath (hWnd, hdc);
break;
}
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// 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;
}
void DoRectRegion (HWND hWnd, HDC hdc, bool bInvert)
{
RECT rcRegion, rcClient;
HRGN hRgn;
GetClientRect (hWnd, &rcClient);
rcRegion.top = rcClient.bottom / 4;
rcRegion.bottom = rcRegion.top * 3;
rcRegion.left = rcClient.right / 4;
rcRegion.right = rcRegion.left * 3;
hRgn = CreateRectRgnIndirect (&rcRegion);
DrawRadials (hdc, hRgn, rcClient);
if (bInvert)
InvertRgn (hdc, hRgn);
DeleteObject (hRgn);
}
void DoRoundRectRegion (HWND hWnd, HDC hdc, bool bInvert)
{
RECT rcRegion, rcClient;
HRGN hRgn;
GetClientRect (hWnd, &rcClient);
rcRegion.top = rcClient.bottom / 4;
rcRegion.bottom = rcRegion.top * 3;
rcRegion.left = rcClient.right / 4;
rcRegion.right = rcRegion.left * 3;
hRgn = CreateRoundRectRgn (rcRegion.left, rcRegion.top,
rcRegion.right, rcRegion.bottom, 10, 10);
DrawRadials (hdc, hRgn, rcClient);
if (bInvert)
InvertRgn (hdc, hRgn);
DeleteObject (hRgn);
}
void DoCircleRegion (HWND hWnd, HDC hdc, bool bInvert)
{
RECT rcRegion, rcClient;
HRGN hRgn;
GetClientRect (hWnd, &rcClient);
int nSide = rcClient.bottom / 2;
rcRegion.top = (rcClient.bottom - nSide) / 2 ;
rcRegion.bottom = rcRegion.top + nSide;
rcRegion.left = (rcClient.right - nSide) / 2;
rcRegion.right = rcRegion.left + nSide;
hRgn = CreateEllipticRgnIndirect (&rcRegion);
DrawRadials (hdc, hRgn, rcClient);
if (bInvert)
InvertRgn (hdc, hRgn);
DeleteObject (hRgn);
}
void DoEllipseRegion (HWND hWnd, HDC hdc, bool bInvert)
{
RECT rcRegion, rcClient;
HRGN hRgn;
GetClientRect (hWnd, &rcClient);
rcRegion.top = rcClient.bottom / 4;
rcRegion.bottom = rcRegion.top * 3;
rcRegion.left = rcClient.right / 4;
rcRegion.right = rcRegion.left * 3;
hRgn = CreateEllipticRgnIndirect (&rcRegion);
DrawRadials (hdc, hRgn, rcClient);
if (bInvert)
InvertRgn (hdc, hRgn);
DeleteObject (hRgn);
}
void DoPolyRegion (HWND hWnd, HDC hdc, bool bStar, bool bInvert)
{
RECT rcClient;
POINT point[5];
double fAngle;
int i;
HRGN hRgn;
//2 * PI / 360
GetClientRect (hWnd, &rcClient);
int nArm = rcClient.bottom / 3;
for (fAngle = PI / 2, i = 0; i < 5; fAngle += 2 * PI / 5, ++i)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -