Click to See Complete Forum and Search --> : Modifying Pushbuttons


Sharon Daniel
March 29th, 1999, 05:54 AM
Hi,


I am trying to modify the way the pushbuttons i use will show. I want my pushbutton to behave just like the desktop. I will be able to move them around and double click the for opening. My questions are :

1)how to change the color of the text and the icon to be in blue background ?

2)how to change the text of the pushbutton.


i have made it in createing the pushbutton with the icon using OWNERDRAW, i have made it to move the pushbutton around, and i do know the area of the icon and the text.


I do not want to use listview, because i am changing a behavior of a running program and i have hooked the pushbuttons of that program and now i am chnaging the way it shows on the screen.


Thanks in advance for any idea.


Sharon Daniel

Tom Puverle
May 27th, 2001, 06:27 PM
Button is just another window - when you need to display your own graphics in it, you will need to hook the WM_DRAWITEM message. In the handler you put code like if you are drawing into a normal window. You need to get a DC, then draw on it (first fill the background, then draw the icon, then the text). Then release the DC.

Indian_Techie
May 28th, 2001, 03:02 AM
Hi,

I have written a button class that takes care of changing button text, back color and fore color. I'm giving ColorButton.h and ColorButton.cpp files. All you need to do is add a member variable to your button of this class (CColorButton) and use the functionality.

//*******************************************************************
// ColorButton.h : header file

#if !defined(COLORBUTTON)
#define COLORBUTTON

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ColorButton.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CColorButton window

class CColorButton : public CButton
{
private:
COLORREF m_backColor;
COLORREF m_foreColor;
BOOL m_multiLine; //By default false
CString m_buttonText;
// Construction
public:
CColorButton();

// Attributes
public:

// Operations
public:
void SetMultiLine(BOOL multiLine = FALSE);
void SetColor(COLORREF backColor = RGB(128,128,128), COLORREF foreColor = RGB(0,0,0));
void SetText(CString buttonText);
void Refresh();

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CColorButton)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CColorButton();

// Generated message map functions
protected:
//{{AFX_MSG(CColorButton)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(COLORBUTTON)

//*******************************************************************
//
//
// ColorButton.cpp : implementation file
//

#include "stdafx.h"
#include "ColorButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CColorButton

CColorButton::CColorButton()
{
m_backColor = RGB(190,190,190);
m_foreColor = RGB(0,0,0);
m_multiLine = FALSE;
}

CColorButton::~CColorButton()
{
}


BEGIN_MESSAGE_MAP(CColorButton, CButton)
//{{AFX_MSG_MAP(CColorButton)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorButton message handlers

void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CRect rect(lpDIS->rcItem);
HBRUSH hBrush= ::CreateSolidBrush(m_backColor);

DrawEdge(lpDIS->hDC,rect,(lpDIS->itemState & ODS_SELECTED) ? EDGE_SUNKEN : EDGE_RAISED,BF_RECT);
rect.DeflateRect(2,2);
FillRect(lpDIS->hDC,rect,hBrush);
SetBkMode(lpDIS->hDC,TRANSPARENT);
SetTextColor(lpDIS->hDC,m_foreColor);
//GetWindowText(m_buttonText);
if (m_multiLine)
DrawText(lpDIS->hDC,m_buttonText,m_buttonText.GetLength(),rect,DT_WORDBREAK|DT_CENTER);
else
DrawText(lpDIS->hDC,m_buttonText,m_buttonText.GetLength(),rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
::DeleteObject(hBrush);
::ReleaseDC(lpDIS->hwndItem,lpDIS->hDC);
}

BOOL CColorButton::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= BS_OWNERDRAW;
cs.style &= ~BS_DEFPUSHBUTTON;
return CButton::PreCreateWindow(cs);
}


void CColorButton::PreSubclassWindow()
{
ModifyStyle(BS_DEFPUSHBUTTON,BS_OWNERDRAW);
CButton::PreSubclassWindow();
GetWindowText(m_buttonText);
}


void CColorButton::SetMultiLine(BOOL multiLine)
{
m_multiLine = multiLine;
Refresh();
}

void CColorButton::SetColor(COLORREF backColor, COLORREF foreColor)
{
m_backColor = backColor;
m_foreColor = foreColor;
Refresh();
}
void CColorButton::SetText(CString buttonText)
{
m_buttonText = buttonText;
Refresh();
}

void CColorButton::Refresh()
{
InvalidateRect(0,FALSE);
}
//*******************************************************************




Hope this helps.

John.

Let me know if got helped. If you got helped pls rate!!!

Jus hav a look at http://www.bobbyzone.homestead.com