第5章文本的输出
董本志
文字输出函数 与文字输出相关的API函数共有49个。
一、常用的文字输出函数
1、int DrawText( HDC hDC, // handle to DC
LPCTSTR lpString, // text to draw
int nCount, // text length
LPRECT lpRect, // formatting dimensions
UINT uFormat // text-drawing options
);
uFormat
DT_TOP 、DT_VCENTER 等
返回值:函数执行成功,则返回值是文本的高度,如果 DT_VCENTER 或者DT_BOTTOM 被设定, 返回值是 lpRect->top 到绘制的文本的 bottom的距离;如果函数执行失败,返回值为0。
2、BOOL TextOut( HDC hdc, // 设备上下文句柄
int nXStart, // 开始位置的X坐标
int nYStart, //开始位置的Y坐标
LPCTSTR lpString, // 输出字符串
int cbString //输出字符个数
);
返回值:函数执行成功返回非0值,否则返回0。
例:
RECT r;
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd, &r);
TextOut(hdc, , , "Defenestration can be hazardous", 4);
EndPaint(hwnd,&ps);
return 0;
}
程序运行结果:
二、文字的设备上下文属性
1、文字颜色的设置
COLORREF SetTextColor( HDC hdc, // handle to DC
COLORREF crColor // text color
);
返回值:
函数执行成功,则返回值为设置前的文字颜色。否则返回值为CLR_INVALID。
例:
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
SetTextColor( hdc,RGB(255,0,0) );
TextOut(hdc, , , “hello!", 4);
EndPaint(hwnd,&ps);
return 0;
程序执行结果:
2、文字背景颜色的设置
COLORREF SetBkColor( HDC hdc, // handle to DC
COLORREF crColor // background color value
);
返回值:
函数执行成功,则返回值为设置前的文字背景颜色。否则返回值为CLR_INVALID。
例:
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
SetTextColor( hdc,RGB(255,0,0) );
SetBkColor( hdc,RGB(0,255,0) );
TextOut(hdc, , , “hello!", 4);
EndPaint(hwnd,&ps);
return 0;
3、更改背景模式
int SetBkMode( HDC hdc, // handle to DC
int iBkMode // background mode
);
其中iMode的值为OPAQUE或TRANSPARENT。内定的背景模式为OPAQUE,它表示Windows使用背景颜色来填充矩形的背景。
TRANSPARENT(透明)表示Windows不使用背景颜色来填充矩形的背景。例:
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
SetTextColor( hdc,RGB(255,0,0) );
SetBkColor( hdc,RGB(0,255,0) );
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, , , “hello!", 4);
EndPaint(hwnd,&ps);
return 0;
Windows程序设计-9-课件·PPT 来自淘豆网www.taodocs.com转载请标明出处.