最近,正在联系做一个自绘窗口的按钮类,希望将按钮上的文本按照原来的缺省的输出——即效果与普通的按钮一样,支持上下对齐、左右对齐等。如:一个BS_CENTER|BS_VCENTER、多行的文本(很长),在DC中如何实现——DrawText,效果与普通按钮上一样?
解决方案 »
- 作为大四的学生要去找关于C/C++程序员的实习工作了,请各位大哥提些建议
- cfile 的使用
- CDialog::OnKeyDown总不被执行?
- 工厂方法 的Product基类,可以不用设为抽象类吗?
- 请问我的应用程序的一个子对话框窗口的HWND已知,我怎么检测它是否存在或者已被关掉了
- MSFGrid 控件 成员函数及属性 介绍?!!?
- 高分求读取mpeg2,mpeg1的源码,分不够再加。
- 请教:使用ODBC API操作数据库的时候,如何通过数据库操作句柄hDbc来判断数据库连接是否成功,判断数据库连接仍然有效(没有断开)?谢谢!
- 关于图像的合成
- 从 IE 中拖放一个超链接到我的窗口中,如何得到此超链接的内容?[100分]
- 急求ethereal源代码
- 关于模态对话框,非模态对话框显示问题
DT_CALCRECT Determines the width and height of the rectangle. If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by lpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text, but does not draw the text.
DT_CENTER Centers text horizontally.
DT_END_ELLIPSIS or DT_PATH_ELLIPSIS Replaces part of the given string with ellipses, if necessary, so that the result fits in the specified rectangle. The given string is not modified unless the DT_MODIFYSTRING flag is specified.
You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash.DT_EXPANDTABS Expands tab characters. The default number of characters per tab is eight.
DT_EXTERNALLEADING Includes the font’s external leading in the line height. Normally, external leading is not included in the height of a line of text.
DT_LEFT Aligns text flush-left.
DT_MODIFYSTRING Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified.
Note Some uFormat flag combinations can cause the passed string to be modified. Using DT_MODIFYSTRING with either DT_END_ELLIPSIS or DT_PATH_ELLIPSIS may cause the string to be modified, causing an assertion in the CString override.
DT_NOCLIP Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used.
DT_NOPREFIX Turns off processing of prefix characters. Normally, DrawText interprets the ampersand (&) mnemonic-prefix character as a directive to underscore the character that follows, and the two-ampersand (&&) mnemonic-prefix characters as a directive to print a single ampersand. By specifying DT_NOPREFIX, this processing is turned off.
DT_PATH_ELLIPSIS
DT_RIGHT Aligns text flush-right.
DT_SINGLELINE Specifies single line only. Carriage returns and linefeeds do not break the line.
DT_TABSTOP Sets tab stops. The high-order byte of nFormat is the number of characters for each tab. The default number of characters per tab is eight.
DT_TOP Specifies top-justified text (single line only).
DT_VCENTER Specifies vertically centered text (single line only).
DT_WORDBREAK Specifies word-breaking. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by lpRect. A carriage return–linefeed sequence will also break the line
CFont *font=GetFont();//获得的是按扭里的字体
dc.SelectObject(font);
/****************************************************************************************************************
* 功 能:显示按钮上的文本
*
* 描 述:对齐方式按照按钮的属性进行——保证效果与缺省的一样
*
* 输 入:HDC hDC,Handle to the device context.详见MSDN:DrawText
* LPCTSTR lpString,Pointer to the string to be drawn. 详见MSDN:DrawText
* int nCount,Specifies the number of characters in the string. 详见MSDN:DrawText
* LPRECT lpRect,Pointer to a RECT structure.详见MSDN:DrawText
* UINT nStyle,窗口风格,可通过GetWindowLong(m_hWnd,GWL_STYLE)获得,其中的对齐方式有效
*
* 返 回 值:
*
* 修改记录:
*
* 编 写 人:ZRJ, 2006.10.17,
*
****************************************************************************************************************/
int CBmpButton::DrawButtonText(HDC hDC, LPCTSTR lpString, int nCount, LPRECT lpRect, UINT nStyle)
{
if(lpRect == NULL) return 0;
RECT rtResult = *lpRect;
UINT nFormat = DT_NOCLIP | DT_EDITCONTROL | DT_LEFT | DT_TOP;
if((nStyle&BS_MULTILINE) == BS_MULTILINE)
nFormat |= DT_WORDBREAK;
else
nFormat |= DT_SINGLELINE;
DrawText(hDC, lpString, nCount, &rtResult, nFormat | DT_CALCRECT);
if((nStyle&BS_CENTER) == BS_CENTER)
OffsetRect(&rtResult, (lpRect->right - rtResult.right)/2, 0);
else if((nStyle&BS_LEFT) == BS_LEFT)
OffsetRect(&rtResult, 0, 0);
else if((nStyle&BS_RIGHT) == BS_RIGHT)
OffsetRect(&rtResult, (lpRect->right - rtResult.right), 0);
else//缺省设置->居中
OffsetRect(&rtResult, (lpRect->right - rtResult.right)/2, 0);
if((nStyle&BS_VCENTER) == BS_VCENTER)
OffsetRect(&rtResult, 0, (lpRect->bottom - rtResult.bottom)/2);
else if((nStyle&BS_TOP) == BS_TOP)
OffsetRect(&rtResult, 0, 0);
else if((nStyle&BS_BOTTOM) == BS_BOTTOM)
OffsetRect(&rtResult, 0, (lpRect->bottom - rtResult.bottom));
else//缺省设置->居中
OffsetRect(&rtResult, 0, (lpRect->bottom - rtResult.bottom)/2);
return DrawText(hDC, lpString, nCount, &rtResult, nFormat);
}