Solved

CBitmapButton OnMouseOver

Posted on 2000-04-01
9
1,186 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:lamer
  • 4
  • 3
  • 2
9 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 2676635
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;
}
0
 

Author Comment

by:lamer
ID: 2677172
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.
0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 2677200
I really hope some body does give you a better answer, because I would be interested in an easier method myself.
I don't understand why the makers of MFC didn't think to add this feature to CBitmapButton.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2678587
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).
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Axter
ID: 2686128
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.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2686505
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).

0
 
LVL 30

Expert Comment

by:Axter
ID: 2691855
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.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2691876
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)

0
 

Author Comment

by:lamer
ID: 2692790
Guess i'll be using ur codes after all
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now