本节包含示例代码,显示如何生成由字符串组成的剪辑路径。该示例创建一个逻辑字体,并使用它在剪辑路径中绘制一个字符串,然后通过绘制水平和垂直线来填充路径。
/*
* DoClipPat - 使用指定的字符串绘制剪辑路径
* Return value - TRUE if successful; FALSE otherwise
* lplf - 定义字体的LOGFONT结构的地址
*用于绘制剪辑路径
* lpsz - 用于剪辑路径的字符串的地址
*/
BOOL DoClipPath(LPLOGFONT lplf, LPSTR lpsz)
{
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; /* rectangle structure for painting window */
SIZE sz; /* size structure that receives text extents */
int nStrLen; /* length of the string */
int i; /* loop counter */
/ *检索窗口的缓存DC。*/
hdc = GetDC(hwnd);
/ *擦除当前窗口的内容。*/
GetClientRect(hwnd, &rc);
FillRect(hdc, &rc, GetStockObject(WHITE_BRUSH));
/*
*使用指定的字体创建逻辑字体并选择它
*进入DC。
*/
hfont = CreateFontIndirect(lplf);
if (hfont == NULL)
return FALSE;
hfontOld = SelectObject(hdc, hfont);
/ *创建剪辑路径。*/
nStrLen = lstrlen(lpsz);
BeginPath(hdc);
TextOut(hdc, nXStart, nYStart, lpsz, nStrLen);
EndPath(hdc);
SelectClipPath(hdc, RGN_DIFF);
/*
*检索周围矩形的尺寸
* 文本。
*/
GetTextExtentPoint32(hdc, lpsz, nStrLen, &sz);
/ *通过剪辑路径绘制水平线。*/
for (i = nYStart + 1; i < (nYStart + sz.cy); i += 3) {
MoveToEx(hdc, nXStart, i, (LPPOINT) NULL);
LineTo(hdc, (nXStart + sz.cx), i);
}
/ *通过剪辑路径绘制垂直线。*/
for (i = nXStart + 1; i < (nXStart + sz.cx); i += 3){
MoveToEx(hdc, i, nYStart, (LPPOINT) NULL);
LineTo(hdc, i, (nYStart + sz.cy));
}
/ *将原始字体选择到DC中并释放DC。*/
SelectObject(hdc, hfontOld);
DeleteObject(hfont);
ReleaseDC(hwnd, hdc);
return TRUE;
}
有关演示应用程序如何创建矩形裁剪区域的示例,请参阅地区.