有谁可以改变button的颜色,改为前景色,或改scoll bar 的颜色,型状

babam 2002-03-13 08:10:18
不够的话我可以再加,最好有原代码!
在线等待!email __babam8321156@hotmail.com
...全文
84 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
40Star 2002-03-14
  • 打赏
  • 举报
回复
SetWindowRgn
这个可以改变Button的形状
bluecrest 2002-03-14
  • 打赏
  • 举报
回复
In the derived class, you do not override OnCtrlColor(), you
override CtrColor() as Brandon showed.

If you use class wizard to add message maps, look for "=WM_CTLCOLOR"
(notice the = sign). It will be near the top.

If you do not use class wizard, add the following to the message map :



ON_WM_CTLCOLOR_REFLECT()

这是在CODEGRU论坛上别人说的
为什么我派生一个类时不需要ONCTLCOLOR,而需要用CTLCOLOR?
蒋晟 2002-03-14
  • 打赏
  • 举报
回复
自己去看为什么要用drawitem
http://www.csdn.net/Develop/read_article.asp?id=9603
bluecrest 2002-03-14
  • 打赏
  • 举报
回复
我在问一下
为什么我用ctlcolor()就可以改变control的背景色了
而大家却用onctlcolor()
有什么不一样??????????????????????????????
????????????????????????????????????

只换个背景色何必用drawitem呢?杀鸡用牛刀。
babam 2002-03-14
  • 打赏
  • 举报
回复
谢谢大家支持!见者有分!
game2000 2002-03-14
  • 打赏
  • 举报
回复
to bluecrest(朝着自己的目标前进)

我觉得重载drawitem()并不比onctlcolor()麻烦多少,
而且很容易把控件扩充成其它的效果(比如背景图),
"杀鸡用牛刀"多少带点讽刺的意味,如果是我我就不这样对别人发表评价
greenhope 2002-03-13
  • 打赏
  • 举报
回复
有两种方法。其一,可以在父类中指定控件的颜色,或者利用MFC4.0新的消息反射在控件类中指定颜色。 当控件需要重新着色时,工作框调用父窗口(通常是对话框)的CWnd: : OnCrtlColor,可以在父窗口类中重置该函数并指定控件的新的绘画属性。例如,下述代码将对话中的所有编辑控件文本颜色改为红色:
HBRUSH CAboutDig : : OnCtlColor (CDC * pDCM , CWnd * pWnd , UINT nCtlColor)

{
HBRUSH hbr = CDialog : : OnCtlColor (pDC, pWnd , nCtlColor )

//Draw red text for all edit controls .
if (nCtlColor= = CTLCOLOR_EDIT )
pDC —> SetTextColor (RGB (255, 0 , 0 , ) )

return hbr
}

然而,由于每个父窗口必须处理通知消息并指定每个控件的绘画属性,所以,这种方法不是完全的面向对象的方法。控件处理该消息并指定绘画属性更合情合理。消息反射允许用户这样做。通知消息首先发送给父窗口,如果父窗口没有处理则发送给控件。创建一个定制彩色列表框控件必须遵循下述步骤。

首先,使用ClassWizard 创建一个CListBox 的派生类并为该类添加下述数据成员。
class CMyListBox publilc CListBox
{

private
COLORREF m_clrFor // foreground color
COLORREF m_clrBack //background color
Cbrush m_brush //background brush

}
其次,在类的构造函数中,初始化数据中。
CMyListBox : : CMyListBox ()
{
//Initialize data members .
m_clrFore =RGB (255 , 255 , 0) //yellow text
m_clrBack=RGB (0 , 0 , 255) // blue background
m_brush . CreateSolidBrush (m _clrBack )
}

最后,使用ClassWizard处理反射的WM_CTLCOLOR(=WM_CTLCOLOR)消息并指定新的绘画属性。
HBRUSH CMyListBox : : CtlColor (CDC* pDC, UINT nCtlColor )
{
pDC—>SetTextColor (m_clrFore)
pDC—>SetBkColor (m_clrBack)

return (HBRUSH) m_brush.GetSafeHandle ()
}
现在,控件可以自己决定如何绘画,与父窗口无关。
game2000 2002-03-13
  • 打赏
  • 举报
回复
重载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);
}

CButton Overview | Class Members | Hierarchy Chart

See Also CButton::SetButtonStyle,WM_DRAWITEM


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
DRAWITEMSTRUCT Structure
The DRAWITEMSTRUCT structure has the following form:

typedef struct tagDRAWITEMSTRUCT {
UINT CtlType;
UINT CtlID;
UINT itemID;
UINT itemAction;
UINT itemState;
HWND hwndItem;
HDC hDC;
RECT rcItem;
DWORD itemData;
} DRAWITEMSTRUCT;

The DRAWITEMSTRUCT structure provides information the owner window must have to determine how to paint an owner-drawn control or menu item. The owner window of the owner-drawn control or menu item receives a pointer to this structure as the lParam parameter of the WM_DRAWITEM message.

Members

CtlType

The control type. The values for control types are as follows:

ODT_BUTTON Owner-drawn button


ODT_COMBOBOX Owner-drawn combo box


ODT_LISTBOX Owner-drawn list box


ODT_MENU Owner-drawn menu


ODT_LISTVIEW List view control


ODT_STATIC Owner-drawn static control


ODT_TAB Tab control
CtlID

The control ID for a combo box, list box, or button. This member is not used for a menu.

itemID

The menu-item ID for a menu or the index of the item in a list box or combo box. For an empty list box or combo box, this member is a negative value, which allows the application to draw only the focus rectangle at the coordinates specified by the rcItem member even though there are no items in the control. The user can thus be shown whether the list box or combo box has the input focus. The setting of the bits in the itemAction member determines whether the rectangle is to be drawn as though the list box or combo box has input focus.

itemAction

Defines the drawing action required. This will be one or more of the following bits:

ODA_DRAWENTIRE This bit is set when the entire control needs to be drawn.


ODA_FOCUS This bit is set when the control gains or loses input focus. The itemState member should be checked to determine whether the control has focus.


ODA_SELECT This bit is set when only the selection status has changed. The itemState member should be checked to determine the new selection state.
itemState

Specifies the visual state of the item after the current drawing action takes place. That is, if a menu item is to be dimmed, the state flag ODS_GRAYED will be set. The state flags are as follows:

ODS_CHECKED This bit is set if the menu item is to be checked. This bit is used only in a menu.


ODS_DISABLED This bit is set if the item is to be drawn as disabled.


ODS_FOCUS This bit is set if the item has input focus.


ODS_GRAYED This bit is set if the item is to be dimmed. This bit is used only in a menu.


ODS_SELECTED This bit is set if the item’s status is selected.


ODS_COMBOBOXEDIT The drawing takes place in the selection field (edit control) of an ownerdrawn combo box.


ODS_DEFAULT The item is the default item.
hwndItem

Specifies the window handle of the control for combo boxes, list boxes, and buttons. Specifies the handle of the menu (HMENU) that contains the item for menus.

hDC

Identifies a device context. This device context must be used when performing drawing operations on the control.

rcItem

A rectangle in the device context specified by the hDC member that defines the boundaries of the control to be drawn. Windows automatically clips anything the owner draws in the device context for combo boxes, list boxes, and buttons, but it does not clip menu items. When drawing menu items, the owner must not draw outside the boundaries of the rectangle defined by the rcItem member.

itemData

For a combo box or list box, this member contains the value that was passed to the list box by one of the following:

CComboBox::AddString


CComboBox::InsertString


CListBox::AddString


CListBox::InsertString
For a menu, this member contains the value that was passed to the menu by one of the following:

CMenu::AppendMenu


CMenu::InsertMenu


CMenu::ModifyMenu
See Also CWnd::OnDrawItem


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.






gstan 2002-03-13
  • 打赏
  • 举报
回复
肯定重载OnDrawItem
在这个函数里面你想怎么画你的控件都可以
茂纲 2002-03-13
  • 打赏
  • 举报
回复
如过你英文不错的话,去研究这个代码

http://www.codeproject.com/buttonctrl/cxskinbutton.asp
蒋晟 2002-03-13
  • 打赏
  • 举报
回复
http://www.csdn.net/Develop/read_article.asp?id=9603
bluecrest 2002-03-13
  • 打赏
  • 举报
回复
1、ONWER DRAW
2、=WM_CTLCOLOR
dct1999 2002-03-13
  • 打赏
  • 举报
回复
重载onctlcolor函数,返回你需要的画刷

16,551

社区成员

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

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

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