Home   Index   About
Ultimate Pack


Custom Search

This section contains a code sample that enables the user to select a font of a particular point size (by using the Choose Font dialog box), select a clip path (by using text drawn with this font), and then view the result of clipping to the text.

This code sample was used to create the illustration that appears in Clip Paths.

CHOOSEFONT cf; /* common dialog box font structure */

LOGFONT lf; /* logical font structure */

HFONT hfont; /* new logical font handle */

HFONT hfontOld; /* original logical font handle */

HDC hdc; /* display DC handle */

int nXStart, nYStart; /* drawing coordinates */

RECT rc; /* rect structure for painting window */

SIZE sz; /* structure that receives text extents */

double aflSin[90]; /* sine of 0-90 degrees */

double aflCos[90]; /* cosine of 0-90 degrees */

double flRadius,a; /* radius of circle */

int iMode; /* clipping mode */

HRGN hrgn; /* clip region handle */

LRESULT APIENTRY MainWndProc(

HWND hwnd, /* window handle */

UINT message, /* type of message */

WPARAM wParam, /* additional information */

LPARAM lParam) /* additional information */

{

PAINTSTRUCT ps;

switch (message) {

case WM_PAINT:

hdc = BeginPaint(hwnd, &ps);

EndPaint(hwnd, &ps);

break;

case WM_COMMAND: /* command from app's menu */

switch (wParam) {

case IDM_VANISH: /* erases client area */

hdc = GetDC(hwnd);

GetClientRect(hwnd, &rc);

FillRect(hdc, &rc, GetStockObject(WHITE_BRUSH));

ReleaseDC(hwnd, hdc);

break;

case IDM_AND: /* sets clip mode to RGN_AND */

iMode = RGN_AND;

break;

case IDM_COPY: /* sets clip mode to RGN_COPY */

iMode = RGN_COPY;

break;

case IDM_DIFF: /* sets clip mode to RGN_DIFF */

iMode = RGN_DIFF;

break;

case IDM_OR: /* sets clip mode to RGN_OR */

iMode = RGN_OR;

break;

case IDM_XOR: /* sets clip mode to RGN_XOR */

iMode = RGN_XOR;

break;

case IDM_CLIP_PATH:

/* Retrieve a cached DC for the window. */

hdc = GetDC(hwnd);

/*

* Use the font requested by the user in the

* Choose Font dialog box to create a logical font,

* then select that font into the device context.

*/

hfont = CreateFontIndirect(cf.lpLogFont);

hfontOld = SelectObject(hdc, hfont);

/*

* Retrieve the dimensions of the rectangle

* that surrounds the text.

*/

GetTextExtentPoint32(hdc, "Clip Path", 9, &sz);

/*

* Set a clipping region using the rect that

* surrounds the text.

*/

hrgn = CreateRectRgn(nXStart, nYStart,

nXStart + sz.cx,

nYStart + sz.cy);

SelectClipRgn(hdc, hrgn);

/*

* Create a clip path using text drawn with

* the user's requested font.

*/

BeginPath(hdc);

TextOut(hdc, nXStart, nYStart, "Clip Path", 9);

EndPath(hdc);

SelectClipPath(hdc, iMode);

/* Compute the sine of 0, 1, ... 90 degrees. */

for (i = 0; i < 90; i++) {

aflSin[i] = sin( (((double)i) / 180.0)

* 3.14159);

}

/* Compute the cosine of 0, 1,... 90 degrees. */

for (i = 0; i < 90; i++) {

aflCos[i] = cos( (((double)i) / 180.0)

* 3.14159);

}

/* Set the radius value. */

flRadius = (double)(2 * sz.cx);

/*

* Draw the 90 rays extending from the

* radius to the edge of the circle.

*/

for (i = 0; i < 90; i++) {

MoveToEx(hdc, nXStart, nYStart,

(LPPOINT) NULL);

LineTo(hdc, nXStart + ((int) (flRadius

* aflCos[i])),

nYStart + ((int) (flRadius

* aflSin[i])));

}

/* Reselect the original font into the DC. */

SelectObject(hdc, hfontOld);

/* Delete the user's font. */

DeleteObject(hfont);

/* Release the DC. */

ReleaseDC(hwnd, hdc);

break;

case IDM_FONT:

/* Initialize necessary members. */

cf.lStructSize = sizeof (CHOOSEFONT);

cf.hwndOwner = hwnd;

cf.lpLogFont = &lf;

cf.Flags = CF_SCREENFONTS | CF_EFFECTS;

cf.rgbColors = RGB(0, 255, 255);

cf.nFontType = SCREEN_FONTTYPE;

/*

* Display the Font dialog box, allow the user

* to choose a font, and render text in the

* window with that selection.

*/

if (ChooseFont(&cf)) {

hdc = GetDC(hwnd);

hfont = CreateFontIndirect(cf.lpLogFont);

hfontOld = SelectObject(hdc, hfont);

crOld = SetTextColor(hdc, cf.rgbColors);

TextOut(hdc, nXStart, nYStart,

"Clip Path", 9);

SetTextColor(hdc, crOld);

SelectObject(hdc, hfontOld);

DeleteObject(hfont);

ReleaseDC(hwnd, hdc);

}

break;

default:

return DefWindowProc(hwnd, message, wParam,

lParam);

}

break;

case WM_DESTROY: /* window is being destroyed */

PostQuitMessage(0);

break;

default: /* if unprocessed... */

return DefWindowProc(hwnd, message, wParam, lParam);

}

return 0;

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun