Solved

CBitmapButton OnMouseOver

Posted on 2000-04-01
9
1,199 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 120
Error on moodle after upgrade 3 120
countEvens challenge 2 74
MaxSpan challenge 9 79
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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

19 Experts available now in Live!

Get 1:1 Help Now