📄 text.cpp
字号:
// Text.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <math.h>
#include <stdio.h>
#define MAX_LOADSTRING 100
#define TEXT_FORMAT (DT_SINGLELINE|DT_VCENTER|DT_CENTER)
#define PI 3.14159
#define TWOPI (2 * PI)
// 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);
HFONT CreatePointFont (int nPoints, char *szFace, HDC hdc);
HFONT CreatePointFontIndirect (LOGFONT *lf, HDC hdc);
int GetSizeInPicas (int nPicas, HDC hdc);
void DoJustify (HDC hdc, HWND hWnd);
void DoTabbedText (HDC hdc, HWND hWnd);
int JustifyLine (HDC hdc, char *szText, SIZE & sz, RECT *rc = NULL);
void DoRotateText (HDC hdc, HWND hWnd);
void DoPieChart (HWND hWnd, HDC hdc);
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_TEXT, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TEXT);
// 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_TEXT);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_TEXT;
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_FILE_CLEAR;
RECT rc;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
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_TEXT_JUSTIFY:
case IDM_FILE_CLEAR:
case IDM_TEXT_TABBED:
case IDM_FONTS_ROTATE:
case IDM_TEXT_PIECHART:
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_FILE_CLEAR:
break;
case IDM_TEXT_JUSTIFY:
DoJustify (hdc, hWnd);
break;
case IDM_TEXT_TABBED:
DoTabbedText (hdc, hWnd);
break;
case IDM_FONTS_ROTATE:
DoRotateText (hdc, hWnd);
break;
case IDM_TEXT_PIECHART:
DoPieChart (hWnd, hdc);
break;
}
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;
}
#define TESTSTRING "Now is the time for all good men to come to the aid of their Teletype."
#define TESTSTRING1 "Nowisthetimeforallgoodmento come to the aid of their Teletype."
void DoJustify (HDC hdc, HWND hWnd)
{
HFONT hFont;
RECT rcClient;
int nLineWidth;
char *szTextOut;
int nLeftMargin;
char *szDup = new char [strlen(TESTSTRING) + 1];
strcpy (szDup, TESTSTRING);
szTextOut = szDup;
GetClientRect (hWnd, &rcClient);
nLineWidth = GetSizeInPicas (120, hdc);
nLeftMargin = 0;
hFont = CreatePointFont (120, "Times New Roman", hdc);
HFONT hOldFont = (HFONT) SelectObject (hdc, hFont);
MoveToEx (hdc, nLineWidth, 0, NULL);
LineTo (hdc, nLineWidth, rcClient.bottom);
RECT rcText;
rcText.left = nLeftMargin;
rcText.right = rcText.left + nLineWidth;
rcText.top = 10;
rcText.bottom = rcText.top;
DrawText (hdc, TESTSTRING, strlen (TESTSTRING), &rcText, DT_EDITCONTROL | DT_WORDBREAK | DT_CALCRECT);
DrawText (hdc, TESTSTRING, strlen (TESTSTRING), &rcText, DT_EDITCONTROL | DT_WORDBREAK);
rcText.left = nLeftMargin;
rcText.right = rcText.left + nLineWidth;
rcText.top = rcText.bottom + 25;
rcText.bottom = 0;
// MoveToEx (hdc, rc.right / 2, rc.bottom / 2, NULL);
// int nError = GDI_ERROR;
// int nAlign = SetTextAlign (hdc, TA_UPDATECP | TA_CENTER);
while (*szTextOut)
{
SIZE sz;
char *s;
int nFit;
int nPoints;
GetTextExtentExPoint (hdc, szTextOut, strlen (szTextOut), nLineWidth, &nFit, NULL, &sz);
nPoints = sz.cy; // sz.cy contains the rectangle height
char *szLine = new char [nFit + 1];
memset (szLine, '\0', nFit + 1);
strncpy (szLine, szTextOut, nFit);
//
// Two special cases:
// 1. The next character is a space.
// 2. It's the last part of the text
//
if (strlen (szLine) == strlen (szTextOut))
{
TextOut (hdc, nLeftMargin, rcText.top, szLine, strlen (szLine));
break;
}
sz.cx = nLineWidth;
sz.cy = rcText.top;
if (szTextOut[nFit] == ' ')
{
szTextOut += strlen (szLine);
szTextOut += 1;
}
else
{
if ((s = strrchr (szLine, ' ')) == NULL) // No spaces?
{
szTextOut += nFit;
}
else
{
*s = '\0';
szTextOut += strlen (szLine);
++szTextOut;
}
}
// rcText.top = nPos;
rcText.bottom = rcText.top + nPoints;
JustifyLine (hdc, szLine, sz, &rcText);
delete [] szLine;
rcText.top += nPoints;
}
SelectObject (hdc, hOldFont);
DeleteObject (hFont);
delete [] szDup;
}
BOOL JustifyLine (HDC hdc, char *szText, SIZE & sz, RECT *rc)
{
char *s;
int nSpaces;
int nWidth;
nWidth = sz.cx;
s = szText;
nSpaces = 0;
while (*s)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -