📄 patterns.cpp
字号:
// Patterns.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <commdlg.h>
#include <io.h>
#define MAX_LOADSTRING 100
#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 DrawLines (HWND hWnd, HDC hdc);
void DrawShadow (HDC hdc, RECT& rc);
void DrawHatches (HWND hWnd, HDC hdc);
BOOL DrawBenDay (HWND hWnd, HDC hdc);
void CreateLogFont (HDC hdc, LOGFONT & lf, int nPoints = 10, char *szFace = "Times New Roman Bold");
void DrawJoins (HWND hWnd, HDC hdc);
void DrawEndCaps (HWND hWnd, HDC hdc);
BOOL Rectangle (HDC hdc, RECT & rc);
BOOL Ellipse (HDC hdc, RECT & rc);
void DoBitBlast (HWND hWnd, HDC hdc);
void DoBitStretch (HWND hWnd, HDC hdc);
void DoLoadBitmapFromFile (HWND hWnd, HDC hdc);
bool exist (const char *path);
bool exist (char *path);
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_PATTERNS, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_PATTERNS);
// 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_PATTERNS);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_PATTERNS;
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_PATTERN_CLEAR;
RECT rc;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
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_PATTERN_CLEAR:
case IDM_PATTERN_LINES:
case IDM_PATTERN_HATCH:
case IDM_PATTERN_JOINS:
case IDM_PATTERN_ENDCAPS:
case IDM_PATTERN_BENDAY: // Draw benday
case IDM_BITMAPS_BITBLAST:
case IDM_BITMAPS_STRETCHTRANSFER:
case IDM_BITMAPS_LOADFROMFILE:
nMode = wmId;
GetClientRect(hWnd, &rc);
InvalidateRect (hWnd, &rc, TRUE);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
switch (nMode)
{
default:
case IDM_PATTERN_CLEAR: // Clear screen
break;
case IDM_PATTERN_LINES: // Draw lines
DrawLines (hWnd, hdc);
break;
case IDM_PATTERN_HATCH: // Draw hatches
DrawHatches (hWnd, hdc);
break;
case IDM_PATTERN_JOINS:
DrawJoins (hWnd, hdc);
break;
case IDM_PATTERN_ENDCAPS:
DrawEndCaps (hWnd, hdc);
break;
case IDM_PATTERN_BENDAY: // Draw benday
if (!DrawBenDay (hWnd, hdc))
nMode = IDM_PATTERN_CLEAR;
break;
case IDM_BITMAPS_BITBLAST:
DoBitBlast (hWnd, hdc);
break;
case IDM_BITMAPS_STRETCHTRANSFER:
DoBitStretch (hWnd, hdc);
break;
case IDM_BITMAPS_LOADFROMFILE:
DoLoadBitmapFromFile (hWnd, hdc);
}
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 DrawLines (HWND hWnd, HDC hdc)
{
RECT rc[8];
RECT rcClient;
int nXoffset, nYoffset;
int nSide, nWide;
LOGBRUSH lb;
LOGFONT lf;
UINT uiStyle[] = {PS_SOLID, PS_DASH, PS_DOT, PS_DASHDOT,
PS_DASHDOTDOT, PS_NULL, PS_INSIDEFRAME,
PS_INSIDEFRAME };
char *szLabels[] =
{
"PS_SOLID",
"PS_DASH",
"PS_DOT",
"PS_DASHDOT",
"PS_DASHDOTDOT",
"PS_NULL",
"PS_INSIDEFRAME",
"PS_INSIDEFRAME"
};
char szText[512];
GetClientRect(hWnd, &rcClient);
nXoffset = 12;
nYoffset = 12;
nSide = rcClient.bottom / 4 - nYoffset;
nWide = rcClient.right / 2 - nXoffset - 4;
rc[0].left = nXoffset;
rc[0].top = nYoffset;
rc[0].right = rcClient.right / 2 - 4;
rc[0].bottom = rcClient.bottom / 4 - nYoffset;
rc[1].left = rc[0].right + 8;
rc[1].top = nYoffset;
rc[1].right = rc[1].left + rcClient.right / 2 - 12;
rc[1].bottom = rc[0].bottom;
rc[2].left = rc[0].left;
rc[2].top = rc[0].bottom + nYoffset;
rc[2].right = rc[0].right;
rc[2].bottom = rc[2].top + rcClient.bottom / 4 - nYoffset;;
rc[3].left = rc[1].left;
rc[3].top = rc[2].top;
rc[3].right = rc[1].right;
rc[3].bottom = rc[2].bottom;
rc[4].left = rc[2].left;
rc[4].top = rc[2].bottom + nYoffset;
rc[4].right = rc[2].right;
rc[4].bottom = rc[4].top + rcClient.bottom / 4 - nYoffset;;
rc[5].left = rc[3].left;
rc[5].top = rc[4].top;
rc[5].right = rc[3].right;
rc[5].bottom = rc[4].bottom;
rc[6].left = rc[4].left;
rc[6].top = rc[4].bottom + nYoffset;
rc[6].right = rc[4].right;
rc[6].bottom = rc[6].top + rcClient.bottom / 4 - nYoffset;;
rc[7].left = rc[5].left;
rc[7].top = rc[6].top;
rc[7].right = rc[5].right;
rc[7].bottom = rc[6].bottom;
//
// Uncomment the following lines to produce a lightly shaded
// backbround for aligning the image.
// HBRUSH hBrush = CreateSolidBrush (RGB(0xf7, 0xf7, 0xf7));
// FillRect (hdc, &rc[0], hBrush);
// FillRect (hdc, &rc[1], hBrush);
// FillRect (hdc, &rc[2], hBrush);
// FillRect (hdc, &rc[3], hBrush);
// FillRect (hdc, &rc[4], hBrush);
// FillRect (hdc, &rc[5], hBrush);
// FillRect (hdc, &rc[6], hBrush);
// FillRect (hdc, &rc[7], hBrush);
// DeleteObject (hBrush);
CreateLogFont (hdc, lf);
lf.lfHeight = -lf.lfHeight;
HFONT hFont = CreateFontIndirect(&lf);
SelectObject (hdc, hFont);
lb.lbColor = (RGB(0x00, 0x00, 0x00));
lb.lbStyle = BS_SOLID;
lb.lbHatch = 0;
SetBkMode (hdc, TRANSPARENT);
for (int i = 0; i < 6; ++i)
{
RECT rcText;
HPEN hPen = ExtCreatePen (PS_GEOMETRIC | PS_ENDCAP_FLAT | uiStyle[i], 10,
&lb, 0, NULL);
SelectObject (hdc, hPen);
MoveToEx (hdc, rc[i].left, rc[i].top + nSide / 2, NULL);
LineTo (hdc, rc[i].right, rc[i].top + nSide / 2);
DeleteObject (hPen);
rcText.left = rc[i].left;
rcText.right = rc[i].right;
rcText.top = rc[i].bottom + lf.lfHeight - 2;
rcText.bottom = rc[i].bottom;
DrawText (hdc, szLabels[i], strlen (szLabels[i]),
&rcText, TEXT_FORMAT);
}
RECT rcNormal, rcInside;
rcNormal.top = rc[6].top;
rcNormal.left = rc[6].left;
rcNormal.bottom = rc[6].bottom;
rcNormal.right = rc[6].left + (rc[6].right - rc[6].left) / 2;
rcInside.top = rc[7].top;
rcInside.left = rc[7].left;
rcInside.bottom = rc[7].bottom;
rcInside.right = rc[7].left + (rc[7].right - rc[7].left) / 2;
HPEN hPen = CreatePen (PS_SOLID, 1, RGB(0x00, 0x00, 0x00));
SelectObject (hdc, hPen);
Rectangle (hdc, rcNormal);
Rectangle (hdc, rcInside);
DeleteObject (hPen);
hPen = ExtCreatePen (PS_GEOMETRIC | PS_ENDCAP_FLAT | PS_SOLID, 10,
&lb, 0, NULL);
SelectObject (hdc, hPen);
Ellipse (hdc, rcNormal);
DeleteObject (hPen);
hPen = ExtCreatePen (PS_GEOMETRIC | PS_ENDCAP_FLAT | PS_INSIDEFRAME, 10,
&lb, 0, NULL);
SelectObject (hdc, hPen);
Ellipse (hdc, rcInside);
DeleteObject (hPen);
rcNormal.top += -lf.lfHeight;
rcNormal.left = rcNormal.right + 10;
rcNormal.right = rc[6].right - 10;
rcInside.top = rcNormal.top;
rcInside.left = rcInside.right + 10;
rcInside.right = rc[7].right - 10;
strcpy (szText, "Ellipse drawn without using PS_INSIDEFRAME");
DrawText (hdc, szText, strlen (szText),
&rcNormal, DT_EDITCONTROL | DT_WORDBREAK);
strcpy (szText, "Ellipse drawn using PS_INSIDEFRAME");
DrawText (hdc, szText, strlen (szText),
&rcInside, DT_EDITCONTROL | DT_WORDBREAK);
DeleteObject (hFont);
}
BOOL Rectangle (HDC hdc, RECT & rc)
{
return (Rectangle (hdc, rc.left,
rc.top,
rc.right,
rc.bottom));
}
BOOL Ellipse (HDC hdc, RECT & rc)
{
return (Ellipse (hdc, rc.left,
rc.top,
rc.right,
rc.bottom));
}
void DrawHatches (HWND hWnd, HDC hdc)
{
RECT rc[6];
RECT rcClient;
int nXoffset, nYoffset;
int nSide;
LOGBRUSH lb;
LOGFONT lf;
long lHatch[] = {HS_BDIAGONAL, HS_CROSS, HS_DIAGCROSS,
HS_FDIAGONAL, HS_HORIZONTAL, HS_VERTICAL};
char *szLabels[] =
{
"HS_BDIAGONAL",
"HS_CROSS",
"HS_DIAGCROSS",
"HS_FDIAGONAL",
"HS_HORIZONTAL",
"HS_VERTICAL"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -