设置按钮的文本颜色

tianxingzhe37 2010-05-21 09:56:14
在SDK编程中,如何设置按钮的文本颜色呢?
HDC hdc;
hdc = GetDC(GetDlgItem(hwnd,IDC_BTN_ADD));
SetBkColor(hdc,RGB(255,0,0));
ReleaseDC(GetDlgItem(hwnd,IDC_BTN_ADD),hdc);
我这样做行不通啊!
望知道怎么做的帮帮忙,谢谢了。(不要用MFC)
...全文
269 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
向立天 2010-05-29
  • 打赏
  • 举报
回复
您好
我是本版版主
此帖已多日无人关注
请您及时结帖
如您认为问题没有解决可按无满意结帖处理
另外本版设置了疑难问题汇总帖
并已在版面置顶
相关规定其帖子中有说明
您可以根据规定提交您帖子的链接
如您目前不想结帖只需回帖说明
我们会删除此结帖通知

见此回复三日内无回应
我们将强制结帖
相关规定详见界面界面版关于版主结帖工作的具体办法
Eleven 2010-05-21
  • 打赏
  • 举报
回复
重载CButton类,添加DrawItem虚函数,BS_OWERDRAW风格,MSDN里有个例子:

// NOTE: CMyButton is a class derived from CButton. The CMyButton
// object was created as follows:
//
// CMyButton myButton;
// myButton.Create(_T("My button"),
// WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW,
// CRect(10,10,100,30), pParentWnd, 1);
//

// This example implements the DrawItem method for a CButton-derived
// class that draws the button's text using the color red.
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
UINT uStyle = DFCS_BUTTONPUSH;

// This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);

// If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;

// Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
DFC_BUTTON, uStyle);

// Get the button's text.
CString strText;
GetWindowText(strText);

// Draw the button text using the text color red.
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}

finder_zhang 2010-05-21
  • 打赏
  • 举报
回复
WM_DRAWITEM
idCtl = (UINT) wParam; // control identifier
lpdis = (LPDRAWITEMSTRUCT) lParam; // item-drawing information
这个是SDK的说明.
只要加上几句

LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
然后在代码里用这个 lpdis 就行了.

上面的代码里面用了个MFC 的CString,再改一下.


if (lpDrawItemStruct->CtlID == IDC_BTN_READ)
{
//先求出按钮的状态
UINT uStyle = DFCS_BUTTONPUSH;
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
uStyle |= DFCS_PUSHED;
}
//以按钮的状态,画出默认的框框
::DrawFrameControl(lpDrawItemStruct->hDC,
&lpDrawItemStruct->rcItem,
DFC_BUTTON,
uStyle);
//求按钮的文字
TCHAR strBtn[200];
::GetDlgItemText(m_hWnd,lpDrawItemStruct->CtlID,strBtn,200);
//设颜色
COLORREF crOld = ::SetTextColor(lpDrawItemStruct->hDC,
RGB(0,0,255)); //设蓝色
//画字
::DrawText(lpDrawItemStruct->hDC,strBtn,_tcslen(strBtn),
&lpDrawItemStruct->rcItem,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
//设回原来的颜色
::SetTextColor(lpDrawItemStruct->hDC,crOld);
}
finder_zhang 2010-05-21
  • 打赏
  • 举报
回复

void CBmpConvertDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
if (lpDrawItemStruct->CtlID == IDC_BTN_READ)
{
//先求出按钮的状态
UINT uStyle = DFCS_BUTTONPUSH;
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
uStyle |= DFCS_PUSHED;
}
//以按钮的状态,画出默认的框框
::DrawFrameControl(lpDrawItemStruct->hDC,
&lpDrawItemStruct->rcItem,
DFC_BUTTON,
uStyle);
//求按钮的文字
CString strBtn;
GetDlgItemText(lpDrawItemStruct->CtlID,strBtn);
//设颜色
COLORREF crOld = ::SetTextColor(lpDrawItemStruct->hDC,
RGB(0,0,255)); //设蓝色
//画字
::DrawText(lpDrawItemStruct->hDC,strBtn,strBtn.GetLength(),
&lpDrawItemStruct->rcItem,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
//设回原来的颜色
::SetTextColor(lpDrawItemStruct->hDC,crOld);
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}


这个是在MFC里面成功把按钮改成蓝字的代码,当然,里面都是纯API的.
finder_zhang 2010-05-21
  • 打赏
  • 举报
回复
按钮跟其他控件不一样,不可以用 SetTextColor 来设的,

处理窗口的 WM_DRAWITEM
yanjingxiong203512 2010-05-21
  • 打赏
  • 举报
回复
看看是不是控件的风格的问题,如果是按钮的话,可能需要设置BS_OWNERDRAW 风格。
tianxingzhe37 2010-05-21
  • 打赏
  • 举报
回复
不好意思,之前用的是SetTextColor函数,忘了改过来了。用SetTextColor也行不通...
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sjdev 的回复:]
SetTextColor(hdc, RGB(xxxx));
[/Quote]

学习!
zealand_1 2010-05-21
  • 打赏
  • 举报
回复
用SetTextColor()
soswaidao 2010-05-21
  • 打赏
  • 举报
回复
同上!!!!
sjdev 2010-05-21
  • 打赏
  • 举报
回复
SetTextColor(hdc, RGB(xxxx));

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧