⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textdemo.cpp

📁 Windows 图形编程 书籍
💻 CPP
字号:
//-----------------------------------------------------------------------------------//
//              Windows Graphics Programming: Win32 GDI and DirectDraw               //
//                             ISBN  0-13-086985-6                                   //
//                                                                                   //
//  Written            by  Yuan, Feng                             www.fengyuan.com   //
//  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
//  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
//                                                                                   //
//  FileName   : textdemo.cpp					                                     //
//  Description: Text demo routines                                                  //
//  Version    : 1.00.000, May 31, 2000                                              //
//-----------------------------------------------------------------------------------//

#define STRICT
#define _WIN32_WINNT 0x0500
#define NOCRYPT
#define WINVER 0x0500

#pragma pack(push, 4)
#include <windows.h>
#pragma pack(pop)

#include <assert.h>
#include <tchar.h>
#include <math.h>
#include <stdio.h>

#include "..\..\include\gdiobject.h"
#include "..\..\include\fonttext.h"
#include "..\..\include\color.h"
#include "..\..\include\axis.h"
#include "..\..\include\curve.h"
#include "..\..\include\fonttext.h"
#include "..\..\include\OutlineMetric.h"
#include "..\..\include\win.h"

#include "TextDemo.h"
#include "resource.h"

void ZoomRect(HDC hDC, int x0, int y0, int x1, int y1, int dx, int dy, int zoom)
{
	// use black pen for border
	HGDIOBJ hOld = SelectObject(hDC, GetStockObject(BLACK_PEN));

	for (int y=y0; y<y1; y++)
	for (int x=x0; x<x1; x++)
	{
		COLORREF c = GetPixel(hDC, x, y);

		HBRUSH  hBrush = CreateSolidBrush(c);
		HGDIOBJ hOld   = SelectObject(hDC, hBrush);

		Rectangle(hDC, dx+(x-x0)*(zoom+1), dy+(y-y0)*(zoom+1), 
			dx+(x-x0)*(zoom+1)+zoom+2, 
			dy+(y-y0)*(zoom+1)+zoom+2);
		SelectObject(hDC, hOld);
		DeleteObject(hBrush);
	}

	SelectObject(hDC, hOld);
}

//////// GLYPH View ///////////

void Line(HDC hDC, int x0, int y0, int x1, int y1)
{
	MoveToEx(hDC, x0, y0, NULL);
	LineTo(hDC, x1, y1);
}


void Box(HDC hDC, int x0, int y0, int x1, int y1)
{
	MoveToEx(hDC, x0, y0, NULL);
	LineTo(hDC, x1, y0);
	LineTo(hDC, x1, y1);
	LineTo(hDC, x0, y1);
	LineTo(hDC, x0, y0);
}

void disp(HDC hDC, int x, int y, const TCHAR * mess, int val)
{
	TCHAR temp[MAX_PATH];

	wsprintf(temp, mess, val);
	TextOut(hDC, x, y, temp, _tcslen(temp));
}

void disp(HDC hDC, int x, int y, const TCHAR * mess)
{
	TextOut(hDC, x, y, mess, _tcslen(mess));
}


int Demo_Font(HDC hDC, int x, int y, HFONT hFont, const TCHAR * name)
{
	HGDIOBJ hOld  = SelectObject(hDC, hFont);

	EXTLOGFONT elf;
	int size = GetObject(hFont, sizeof(elf), & elf);

	LOGFONT lf;
	GetObject(hFont, sizeof(lf), & lf);
	
	TEXTMETRIC tm;
	GetTextMetrics(hDC, & tm);
	int height = tm.tmHeight;

	disp(hDC, x, y, name); y+= height;

//	TCHAR face[MAX_PATH];

//	GetTextFace(hDC, MAX_PATH, face);

	TCHAR mess[MAX_PATH];
	wsprintf(mess, "{%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, """"%s""""}", 
		lf.lfHeight, lf.lfWidth, lf.lfEscapement, lf.lfOrientation, lf.lfWeight,
		lf.lfItalic, lf.lfUnderline, lf.lfStrikeOut, lf.lfCharSet, lf.lfOutPrecision,
		lf.lfClipPrecision, lf.lfQuality, lf.lfPitchAndFamily, lf.lfFaceName);

	disp(hDC, x, y, mess); y+= height;
	
//	wsprintf(mess, "%d %d %d %d", size, sizeof(LOGFONT), sizeof(ENUMLOGFONTEX), sizeof(ENUMLOGFONTEXDV));
//	disp(hDC, x, y, mess); y+= height;
//	disp(hDC, x, y, face); y+= height;

	SelectObject(hDC, hOld);
 
	return y;
}

int Demo_Font(HDC hDC, int x, int y, int stockid, const TCHAR * name)
{
	return Demo_Font(hDC, x, y, (HFONT) GetStockObject(stockid), name);
}


void Demo_StockFonts(HDC hDC, const RECT * rcPaint)
{
	int y = 10;

	int base = GetDialogBaseUnits();
	TCHAR temp[64];
	wsprintf(temp, "DialogBaseUnits: baseunixX=%d, baseunitY=%d", LOWORD(base), HIWORD(base));
	TextOut(hDC, 10, y, temp, _tcslen(temp));
	y+= 30;

	wsprintf(temp, "GetDeviceCaps(LOGPIXELSX)=%d, GetDeviceCaps(LOGPIXELSX)=%d",
		GetDeviceCaps(hDC, LOGPIXELSX), GetDeviceCaps(hDC, LOGPIXELSY));
	TextOut(hDC, 10, y, temp, _tcslen(temp));

	y += 30;

	y = Demo_Font(hDC, 10, y, DEFAULT_GUI_FONT,	   "GetStockObject(DEFAULT_GUI_FONT)")    + 5;
	y = Demo_Font(hDC, 10, y, OEM_FIXED_FONT,	   "GetStockObject(OEM_FIXED_FONT)")      + 5;
	y = Demo_Font(hDC, 10, y, ANSI_FIXED_FONT,	   "GetStockObject(ANSI_FIXED_FONT)")     + 5;
	y = Demo_Font(hDC, 10, y, ANSI_VAR_FONT,	   "GetStockObject(ANSI_VAR_FONT)")       + 5;
	y = Demo_Font(hDC, 10, y, SYSTEM_FONT,		   "GetStockObject(SYSTEM_FONT)")         + 5;
	y = Demo_Font(hDC, 10, y, DEVICE_DEFAULT_FONT, "GetStockObject(DEVICE_DEFAULT_FONT)") + 5;
	y = Demo_Font(hDC, 10, y, SYSTEM_FIXED_FONT,   "GetStockObject(SYSTEM_FIXED_FONT)")   + 5;


/*	{
		KLogFont lf(- PointSizetoLogical(hDC, 36), "Tahoma");
		
		lf.m_lf.lfItalic = TRUE;
		lf.m_lf.lfCharSet = GB2312_CHARSET;
		HFONT hFont = lf.CreateFont();

		SelectObject(hDC, hFont);

		TCHAR temp[32];
		GetTextFace(hDC, 32, temp);
		TextOut(hDC, 400, 10, temp, _tcslen(temp));

		wsprintf(temp, "%d %d", GB2312_CHARSET, GetTextCharset(hDC));
		TextOut(hDC, 400, 60, temp, _tcslen(temp));

		SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
		DeleteObject(hFont);
	}
*/
}

// Terminology and Metrics


/*  int    otmsCharSlopeRise; 
    int    otmsCharSlopeRun; 
    int    otmItalicAngle; 
    UINT   otmEMSquare; 
    int    otmAscent; 
    int    otmDescent; 
    UINT   otmLineGap; 
    UINT   otmsCapEmHeight; 
    UINT   otmsXHeight; 
    RECT   otmrcFontBox; 
    UINT   otmusMinimumPPEM; 
*/

void Demo_Term(HDC hDC, const RECT * rcPaint)
{
	int point = 216;

	HFONT hFont = CreateFont( point * GetDeviceCaps(hDC, LOGPIXELSY) / 72,
						 0, 0, 0, FW_NORMAL, TRUE, FALSE, FALSE, 
						 ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, 
						 ANTIALIASED_QUALITY, VARIABLE_PITCH, "Times New Roman");
	SetTextAlign(hDC, TA_BASELINE | TA_LEFT);
	SetBkMode(hDC, TRANSPARENT);

	SelectObject(hDC, hFont);

	{
		KOutlineTextMetric otm(hDC);

		TEXTMETRIC tm;

		GetTextMetrics(hDC, & tm);
		int x = 300;
		int y = 450;

		// glyph bounding box
		Rectangle(hDC, x + otm.m_pOtm->otmrcFontBox.left,
			           y - otm.m_pOtm->otmrcFontBox.top,
					   x + otm.m_pOtm->otmrcFontBox.right,
					   y - otm.m_pOtm->otmrcFontBox.bottom);

		// draw the character(s)
		TextOut(hDC, x, y, "@", 1);

		SIZE size;

		GetTextExtentPoint(hDC, "@" /*"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -