Link to home
Start Free TrialLog in
Avatar of lamer
lamer

asked on

CBitmapButton OnMouseOver

Theres an image for each of the four states

MouseUp
MouseDown
Selected
Execute

hence the four differenet Bitmaps u need for CBitmapButton... but how come theres no Bitmap for when the mouse is over the button???

How do i make the CBItmapButton change to the appropriate bitmap OnMouseOVer ???

thanks
Avatar of Axter
Axter
Flag of United States of America image

You can try the following:
Create a CButton class like the following:

Header file:

#if !defined(AFX_BITMAPMOUSEOVERBUTTON_H__0B183F55_B341_4718_8919_408EBB1C6731__INCLUDED_)
#define AFX_BITMAPMOUSEOVERBUTTON_H__0B183F55_B341_4718_8919_408EBB1C6731__INCLUDED_

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

/////////////////////////////////////////////////////////////////////////////
// CBitmapMouseOverButton window

class CBitmapMouseOverButton : public CBitmapButton
{
// Construction
public:
      CBitmapMouseOverButton();

// Attributes
public:
protected:
      bool HasInitiatedTme;
      TRACKMOUSEEVENT tme;
// Operations
public:

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CBitmapMouseOverButton)
      //}}AFX_VIRTUAL

// Implementation
public:
      virtual ~CBitmapMouseOverButton();

      // Generated message map functions
protected:
      //{{AFX_MSG(CBitmapMouseOverButton)
      afx_msg void OnMouseMove(UINT nFlags, CPoint point);
      afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
      //}}AFX_MSG

      DECLARE_MESSAGE_MAP()
};

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

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

#endif // !defined(AFX_BITMAPMOUSEOVERBUTTON_H__0B183F55_B341_4718_8919_408EBB1C6731__INCLUDED_)




****************************************
CPP file:
// BitmapMouseOverButton.cpp : implementation file
//

#include "stdafx.h"
#include "Test.h"
#include "BitmapMouseOverButton.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBitmapMouseOverButton

CBitmapMouseOverButton::CBitmapMouseOverButton()
{
      HasInitiatedTme=false;
      LoadBitmaps(IDB_BITMAP1,IDB_BITMAP2,IDB_BITMAP3,IDB_BITMAP4);
}

CBitmapMouseOverButton::~CBitmapMouseOverButton()
{
      tme.cbSize = sizeof(TRACKMOUSEEVENT);
      tme.dwFlags = TME_LEAVE|TME_CANCEL;
      tme.hwndTrack = this->m_hWnd;
      ::_TrackMouseEvent(&tme);
      tme.dwFlags = TME_HOVER|TME_CANCEL;
      tme.hwndTrack = this->m_hWnd;
      ::_TrackMouseEvent(&tme);
}


BEGIN_MESSAGE_MAP(CBitmapMouseOverButton, CBitmapButton)
      //{{AFX_MSG_MAP(CBitmapMouseOverButton)
      ON_WM_MOUSEMOVE()
      ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBitmapMouseOverButton message handlers

void CBitmapMouseOverButton::OnMouseMove(UINT nFlags, CPoint point)
{
      if (!HasInitiatedTme)
      {
            HasInitiatedTme=true;
            LoadBitmaps(IDB_BITMAP5,IDB_BITMAP2,IDB_BITMAP5,IDB_BITMAP5);
            Invalidate(true);
            tme.cbSize = sizeof(TRACKMOUSEEVENT);
            tme.dwFlags = TME_LEAVE;
            tme.hwndTrack = this->m_hWnd;
            ::_TrackMouseEvent(&tme);
      }
      
      CBitmapButton::OnMouseMove(nFlags, point);
}

LRESULT CBitmapMouseOverButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
      HasInitiatedTme=false;
      LoadBitmaps(IDB_BITMAP1,IDB_BITMAP2,IDB_BITMAP3,IDB_BITMAP4);
      Invalidate(true);
      return TRUE;
}
Avatar of lamer
lamer

ASKER

err... well, not exactly what i wanted but its ok nontheless....

the thing is, ive been usign a whole bunch of my own derivition of the normal MFC class I was hoping that theres a really easy solution to this..maybe something that ive missed in CBitmapButton.. rather than creating another class derived from CBitmapButton which can handle mousemove and mouseleave...

i was actually afraid that this will be the answer...

thanks anyway, i'll keep this around to see if anyone knows anything else..

regards.
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use a toolbar with a single button in it.  This gives you the flat button with mouse-over support.

NOTE: buttons predate mouse support by a long shot.  Adding support for mouseover to the MFC class would need to be done in a way that is transparent to existing code.  If done at all, it would probably be as yet another wrapper for CButton.

PS: I like your code .. I do something similar in my own classes .. only I implement the flat-look for buttons (among other things).
RONSLOW,
I tried using the Toolbar, but I couldn't get it to work.
Could you please give more information as to how you use if for a mouse-over.
If you have some code, could you please post it.

Thanks.
I can create a toolbar control on a dialog like this... (where I have a CToolBarCtrl m_toolbar in my dialog class and a ON_COMMAND(IDC_BUTTON,OnButtonPushed)

    m_toolbar.Create(
        WS_VISIBLE | CCS_NOPARENTALIGN | CCS_NORESIZE | CCS_NODIVIDER,
        CRect(50,50,150,100), this, IDC_BUTTON);
    TBBUTTON button;
    button.iBitmap = NULL;
    button.idCommand = IDC_BUTTON; // command to be sent when button pressed
    button.fsState = TBSTATE_ENABLED; // button state
    button.fsStyle = BTNS_BUTTON | BTNS_SHOWTEXT; // button style
    button.dwData = 0; // application-defined value (unused)
    button.iString = m_toolbar.AddStrings("Button\0"); // zero-based index of button label string
    m_toolbar.SetStyle(
        m_toolbar.GetStyle() | TBSTYLE_FLAT | TBSTYLE_TRANSPARENT);
    m_toolbar.AddButtons(1,&button);
    m_toolbar.SetBitmapSize(CSize(0,0));

You would need to add to this code to create image lists for the various states and assign them to the toolbareg. m_toolbar.SetHotImageList(&m_hotimagelist);

This should all be wrapped up in a class.

By using a toolbar you get extra options (like mixing text and graphics, flat look etc).

RONSLOW,
I got your method to work.  Interesting approach.  However, I think for using bitmaps, my class is less complicated.

I was hoping to get your approach to work with the tooltip popping out at the bottom. But I didn't have much success.

Thanks for posting the code.  I'm adding it to my toolbox for future text requirements.
I might add it myself .. hadn't done that before.

Obviously, one would need to add some pictures etc.  Should be quite powerful when nicely wrapped up in a class.

Also, one could put all ones button images in a single (set of) image list(s) and tell each button which images to use.  Could be very cool.

I think I'll withdraw mine as it sounds like you are going to use Axter's suggestion (unless you want to do some point splitting or wait for another solution)

Avatar of lamer

ASKER

Guess i'll be using ur codes after all