在DIALOG下,请问如何改变按钮的颜色

Auroru 2005-10-19 02:56:42
在DIALOG下,想改变控件的颜色或者其他风格,例如改变按钮、滚动条、EDIT框这些的颜色以及字体,如何在命令中实现。谢谢
...全文
1639 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Auroru 2005-10-20
  • 打赏
  • 举报
回复
谢谢回复
这是我的程序,只能显示EDIT的第一行背景,请看看怎么修改
HBRUSH CMy1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if( nCtlColor == CTLCOLOR_EDIT )
{
if(pWnd->GetDlgCtrlID()==IDC_EDIT1)
{
pDC->SetTextColor(RGB(0,0,255));
pDC->SetBkColor(RGB(0,252,0));
}

}
return hbr;
}
Auroru 2005-10-20
  • 打赏
  • 举报
回复
如果不只改变颜色呢 比如大小我也想改变 能实现吗?
goodboyws 2005-10-19
  • 打赏
  • 举报
回复
错了,是CTLCOLOR_EDIT 和CTLCOLOR_MSGBOX
goodboyws 2005-10-19
  • 打赏
  • 举报
回复
MSDN上说,如果是单行的CEDIT,要同时处理CTLCOLOR_EDIT和CTLCOLOR_STATIC
fyx010641 2005-10-19
  • 打赏
  • 举报
回复
你全部用空格填写就OK了!
goodboyws 2005-10-19
  • 打赏
  • 举报
回复
在EDIT框中总是只有第一行有背景颜色,不会吧
kelinwang19 2005-10-19
  • 打赏
  • 举报
回复
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO: Change any attributes of the DC here
if(nCtlColor == CTLCOLOR_BTN )
{
pDC->SetBkColor(RGB(255,0,0));
pDC->SetTextColor(RGB(0,255,0));
return m_hBrush;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
在OnInitDialog里面添加m_hBrush = CreateSolidBrush(RGB(255,0,0));就可以了改变你的按钮的颜色了



在initial
Auroru 2005-10-19
  • 打赏
  • 举报
回复
谢谢 回复
在EDIT框中总是只有第一行有背景颜色 请问是怎么回事?
还有按钮中的自绘是不是选OWNER DRAW?我选了但是没有用
ww51xh 2005-10-19
  • 打赏
  • 举报
回复
OnCtrlColor是不能改变按钮颜色的
必须自己实现一个按纽类,在DrawItem里面实现

CButton::DrawItem
virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );

Parameters

lpDrawItemStruct

A long pointer to a DRAWITEMSTRUCT structure. The structure contains information about the item to be drawn and the type of drawing required.

Remarks

Called by the framework when a visual aspect of an owner-drawn button has changed. An owner-drawn button has the BS_OWNERDRAW style set. Override this member function to implement drawing for an owner-drawn CButton object. The application should restore all graphics device interface (GDI) objects selected for the display context supplied in lpDrawItemStruct before the member function terminates.

Also see the BS_ style values.

Example

// 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);
}

goodboyws 2005-10-19
  • 打赏
  • 举报
回复
按钮需要设成自绘,不过那样好像边就没了,自己在DrawItem画一下
vcmute 2005-10-19
  • 打赏
  • 举报
回复
代码?

HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// Call the base class implementation first! Otherwise, it may
// undo what we are trying to accomplish here.
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// Are we painting the IDC_MYSTATIC control? We can use
// CWnd::GetDlgCtrlID() to perform the most efficient test.
if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
{
// Set the text color to red.
pDC->SetTextColor(RGB(255, 0, 0));

// Set the background mode for text to transparent
// so background will show thru.
pDC->SetBkMode(TRANSPARENT);

// Return handle to our CBrush object.
hbr = m_brush;
}

return hbr;
}
Auroru 2005-10-19
  • 打赏
  • 举报
回复
我试了一下,无法改变按钮的颜色,请问是怎么回事
platinum15 2005-10-19
  • 打赏
  • 举报
回复
响应窗体的OnCtlColor,在里面根据nCtlColor不同值,设置各种控件的颜色
Auroru 2005-10-19
  • 打赏
  • 举报
回复
谢谢回复,能不能说详细点,我刚学没多久
84830388 2005-10-19
  • 打赏
  • 举报
回复
例如改变按钮、滚动条、EDIT框这些的颜色可以响应OnCtlColor来设置。
字体可以先定义一个字体成员变量。然后设置到各个控件就可以了。

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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