Added new scaled text demo from the Windows 2000 graphics API blackbook

svn path=/trunk/; revision=5782
This commit is contained in:
Steven Edwards 2003-08-23 06:41:54 +00:00
parent 90b7f43de7
commit 5c77637a9b
4 changed files with 406 additions and 0 deletions

View file

@ -0,0 +1,32 @@
PATH_TO_TOP = ../../..
TARGET_TYPE = program
TARGET_APPTYPE = windows
TARGET_NAME = txtscale
TARGET_NORC = yes
TARGET_CFLAGS = -fexceptions -g -O0 -DWIN32 -D_DEBUG -D_WINDOWS -D_MBCS -W -D__USE_W32API
TARGET_CPPFLAGS = -fexceptions -g -O0 -DWIN32 -D_DEBUG -D_WINDOWS -D_MBCS -W -D__USE_W32API
TARGET_SDKLIBS = \
kernel32.a \
user32.a \
gdi32.a
TARGET_OBJECTS = \
txtscale.o \
mk_font.o
include $(PATH_TO_TOP)/rules.mak
include $(TOOLS_PATH)/helper.mk
# overide LD_CC to use g++ for linking of the executable
LD_CC = $(CXX)
# EOF

View file

@ -0,0 +1,69 @@
// ------------------------------------------------------------------
// Windows 2000 Graphics API Black Book
// Chapter 4 - Utility functions
//
// Created by Damon Chandler <dmc27@ee.cornell.edu>
// Updates can be downloaded at: <www.coriolis.com>
//
// Please do not hesistate to e-mail me at dmc27@ee.cornell.edu
// if you have any questions about this code.
// ------------------------------------------------------------------
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <windows.h>
#include <cassert>
#include "mk_font.h"
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
namespace font {
// creates a logical font
HFONT MakeFont(
IN HDC hDestDC, // handle to target DC
IN LPCSTR typeface_name, // font's typeface name
IN int point_size, // font's point size
IN const BYTE charset, // font's character set
IN const DWORD style // font's styles
)
{
//
// NOTE: On Windows 9x/Me, GetWorldTransform is not
// supported. For compatibility with these platforms you
// should initialize the XFORM::eM22 data member to 1.0.
//
XFORM xf = {0, 0, 0, 1.0};
GetWorldTransform(hDestDC, &xf);
int pixels_per_inch = GetDeviceCaps(hDestDC, LOGPIXELSY);
POINT PSize = {
0,
-MulDiv(static_cast<int>(xf.eM22 * point_size + 0.5),
pixels_per_inch, 72)
};
HFONT hResult = NULL;
if (DPtoLP(hDestDC, &PSize, 1))
{
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfHeight = PSize.y;
lf.lfCharSet = charset;
lstrcpyn(reinterpret_cast<LPTSTR>(&lf.lfFaceName),
typeface_name, LF_FACESIZE);
lf.lfWeight = (style & FS_BOLD) ? FW_BOLD : FW_DONTCARE;
lf.lfItalic = (style & FS_ITALIC) ? true : false;
lf.lfUnderline = (style & FS_UNDERLINE) ? true : false;
lf.lfStrikeOut = (style & FS_STRIKEOUT) ? true : false;
// create the logical font
hResult = CreateFontIndirect(&lf);
}
return hResult;
}
//-------------------------------------------------------------------------
} // namespace font

View file

@ -0,0 +1,39 @@
// ------------------------------------------------------------------
// Windows 2000 Graphics API Black Book
// Chapter 4 - Utility functions
//
// Created by Damon Chandler <dmc27@ee.cornell.edu>
// Updates can be downloaded at: <www.coriolis.com>
//
// Please do not hesistate to e-mail me at dmc27@ee.cornell.edu
// if you have any questions about this code.
// ------------------------------------------------------------------
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#ifndef CH4_UTILS_H
#define CH4_UTILS_H
#include <windows.h>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// change namespace name appropriately to suit your needs
namespace font {
// font options
static const ULONG FS_NONE = 0x00000000;
static const ULONG FS_BOLD = 0x00000001;
static const ULONG FS_ITALIC = 0x00000002;
static const ULONG FS_UNDERLINE = 0x00000004;
static const ULONG FS_STRIKEOUT = 0x00000008;
// creates a logical font
HFONT MakeFont(IN HDC hDestDC, IN LPCSTR typeface_name,
IN int point_size, IN const BYTE charset = ANSI_CHARSET,
IN const DWORD style = FS_NONE);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#endif // CH4_UTILS_H
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

View file

@ -0,0 +1,266 @@
// ------------------------------------------------------------------
// Windows 2000 Graphics API Black Book
// Chapter 8 - Listing 8.1 (Scaled Text Demo)
//
// Created by Damon Chandler <dmc27@ee.cornell.edu>
// Updates can be downloaded at: <www.coriolis.com>
//
// Please do not hesistate to e-mail me at dmc27@ee.cornell.edu
// if you have any questions about this code.
// ------------------------------------------------------------------
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <windows.h>
#include <commctrl.h>
#include <cassert>
// for the MakeFont() function...
#include "mk_font.h"
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
HINSTANCE hInst;
const char* WndClassName = "GMainWnd";
LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam,
LPARAM LParam);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR,
int nCmdShow)
{
hInst = hInstance;
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName = WndClassName;
wc.lpfnWndProc = MainWndProc;
wc.hInstance = hInst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = reinterpret_cast<HBRUSH>(
COLOR_BTNFACE + 1
);
if (RegisterClass(&wc))
{
HWND hWnd =
CreateWindow(
WndClassName, TEXT("Scaled Text Demo"),
WS_OVERLAPPEDWINDOW | WS_CAPTION |
WS_VISIBLE | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 300,
NULL, NULL, hInst, NULL
);
if (hWnd)
{
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
return 0;
}
//-------------------------------------------------------------------------
HWND hTrackBar = NULL;
HFONT hTTFont = NULL;
double scale = 0.0;
LPCSTR pText = TEXT("The Scaled Text!");
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam,
LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
INITCOMMONCONTROLSEX icx;
icx.dwSize = sizeof(INITCOMMONCONTROLSEX);
icx.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx(&icx);
hTrackBar =
CreateWindow(
TRACKBAR_CLASS, "",
TBS_HORZ | TBS_BOTH | TBS_AUTOTICKS |
TBS_FIXEDLENGTH | TBS_ENABLESELRANGE |
WS_CHILD | WS_VISIBLE,
10, 260, 375, 40,
hWnd, NULL, hInst, NULL
);
assert(hTrackBar != NULL);
SNDMSG(hTrackBar, TBM_SETTHUMBLENGTH, 20, 0);
SNDMSG(hTrackBar, TBM_SETRANGEMAX, TRUE, 100);
// create the TrueType (scalable) font
HDC hDC = GetDC(hWnd);
try
{
// see Chapter 4 for the definition of MakeFont
hTTFont = font::MakeFont(hDC, "Impact", 72);
if (!hTTFont) throw;
}
catch (...)
{
ReleaseDC(hWnd, hDC);
}
ReleaseDC(hWnd, hDC);
break;
}
case WM_HSCROLL:
{
if (reinterpret_cast<HWND>(lParam) == hTrackBar)
{
//
// adjust the scaling factor according to
// the position of the trackbar's slider
//
scale = static_cast<double>(
(SNDMSG(hTrackBar, TBM_GETPOS, 0, 0) + 1) / 50.0
);
InvalidateRect(hWnd, NULL, true);
}
break;
}
case WM_ERASEBKGND:
{
LRESULT res = DefWindowProc(hWnd, msg, wParam, lParam);
HDC hDC = reinterpret_cast<HDC>(wParam);
HFONT hOldFont = static_cast<HFONT>(
SelectObject(hDC, hTTFont)
);
try
{
SetBkMode(hDC, TRANSPARENT);
// open a path bracket
if (!BeginPath(hDC)) throw;
// record the text to the path
TextOut(hDC, 10, 10, pText, lstrlen(pText));
// close the path bracket and
// select the path into hDC
EndPath(hDC);
// determine the number of endpoints in the path
const int num_points = GetPath(hDC, NULL, NULL, 0);
if (num_points > 0)
{
// make room for the POINTs and vertex types
POINT* pPEnds = new POINT[num_points];
unsigned char* pTypes = new unsigned char[num_points];
try
{
// get the path's description
int num_got = GetPath(hDC, pPEnds, pTypes, num_points);
if (num_got > 0)
{
// start a new path bracket
if (!BeginPath(hDC)) throw;
// scale each point in the description
int iPoint;
for (iPoint = 0; iPoint < num_got; ++iPoint)
{
pPEnds[iPoint].x = static_cast<LONG>(
scale * pPEnds[iPoint].x + 0.5
);
pPEnds[iPoint].y = static_cast<LONG>(
scale * pPEnds[iPoint].y + 0.5
);
}
for (iPoint = 0; iPoint < num_points; ++iPoint)
{
// handle the MoveToEx case
if (pTypes[iPoint] == PT_MOVETO)
{
MoveToEx(
hDC, pPEnds[iPoint].x, pPEnds[iPoint].y, NULL
);
}
// handle the LineTo case
else if (
pTypes[iPoint] == PT_LINETO ||
pTypes[iPoint] == (PT_LINETO | PT_CLOSEFIGURE)
)
{
LineTo(hDC, pPEnds[iPoint].x, pPEnds[iPoint].y);
}
// handle the PolyBezierTo case
else if (
pTypes[iPoint] == PT_BEZIERTO ||
pTypes[iPoint] == (PT_BEZIERTO | PT_CLOSEFIGURE)
)
{
PolyBezierTo(hDC, pPEnds + iPoint, 3);
iPoint += 2;
}
}
// close the new path bracket
EndPath(hDC);
// stroke and fill the new path
StrokeAndFillPath(hDC);
}
}
catch (...)
{
// clean up
delete [] pTypes;
delete [] pPEnds;
throw;
}
// clean up
delete [] pTypes;
delete [] pPEnds;
}
// ...
}
catch (...)
{
SelectObject(hDC, hOldFont);
}
SelectObject(hDC, hOldFont);
return res;
}
case WM_SIZE:
{
MoveWindow(
hTrackBar,
0, HIWORD(lParam) - 40, LOWORD(lParam), 40,
false
);
break;
}
case WM_DESTROY:
{
// clean up
DeleteObject(hTTFont);
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//-------------------------------------------------------------------------