本节包含一个代码示例,使用户可以选择特定点大小的字体(通过使用“选择字体”对话框),选择一个剪辑路径(使用此字体绘制的文本),然后查看剪辑结果到文本。
此代码示例用于创建出现在剪辑路径中的图示。
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,/ *窗口句柄* /
UINT消息,/ *消息类型* /
WPARAM wParam,/ *附加信息* /
LPARAM lParam)/ *附加信息* /
{
PAINTSTRUCT ps;
switch(message){
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;
case WM_COMMAND: /* command from app's menu */
开关(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:
/ *检索窗口的缓存DC。*/
hdc = GetDC(hwnd);
/*
*使用用户请求的字体
*选择“字体”对话框以创建逻辑字体,
*然后将该字体选择到设备上下文中。
*/
hfont = CreateFontIndirect(cf.lpLogFont);
hfontOld = SelectObject(hdc, hfont);
/*
*检索矩形的尺寸
*围绕文字。
*/
GetTextExtentPoint32(hdc, "Clip Path", 9, &sz);
/*
*使用rect设置裁剪区域
*围绕文字。
*/
hrgn = CreateRectRgn(nXStart, nYStart,
nXStart + sz.cx,
nYStart + sz.cy);
SelectClipRgn(hdc, hrgn);
/*
*使用文字绘制创建剪辑路径
*用户请求的字体。
*/
BeginPath(hdc);
TextOut(hdc, nXStart, nYStart, "Clip Path", 9);
EndPath(hdc);
SelectClipPath(hdc, iMode);
/ *计算0,1,...的正弦值90度。*/
for (i = 0; i < 90; i++) {
aflSin[i] = sin( (((double)i) / 180.0)
* 3.14159);
}
/ *计算0,1,...的余弦值90度。*/
for (i = 0; i < 90; i++) {
aflCos[i] = cos( (((double)i) / 180.0)
* 3.14159);
}
/ *设置半径值。*/
flRadius = (double)(2 * sz.cx);
/*
*画出90度的光线
*半径到圆的边缘。
*/
for (i = 0; i < 90; i++) {
MoveToEx(HDC,nXStart,新的开始,
(LPPOINT) NULL);
LineTo(hdc,nXStart +((int)(flRadius
[i] * aflCos)),
新起点+((INT)(flRadius
* aflSin[i])));
}
/ *将原始字体重新选择到DC中。*/
SelectObject(hdc, hfontOld);
/ *删除用户的字体。*/
DeleteObject(hfont);
/ *释放DC。*/
ReleaseDC(hwnd, hdc);
break;
case IDM_FONT:
/ *初始化必要的成员。*/
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;
/*
*显示字体对话框,允许用户
*选择一个字体,并在其中呈现文字
*窗口与该选择。
*/
if(ChooseFont(& cf)){
hdc = GetDC(hwnd);
hfont = CreateFontIndirect(cf.lpLogFont);
hfontOld = SelectObject(hdc, hfont);
crOld = SetTextColor(hdc, cf.rgbColors);
的TextOut(HDC,nXStart,新的开始,
"Clip Path", 9);
SetTextColor(hdc, crOld);
SelectObject(hdc, hfontOld);
DeleteObject(hfont);
ReleaseDC(hwnd, hdc);
}
break;
默认:
返回DefWindowProc(hwnd,message,wParam,
lParam);
}
break;
case WM_DESTROY: /* window is being destroyed */
PostQuitMessage(0);
break;
默认值:/ *如果未处理...*/
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}