-
Set Focus When Mouse Over Button (MFC)
Hello.
made a bitmap button (3 states up down and focus)
default is up.
using tab I can focus on the button.
disabled TAB,
I want to focus on mouse over or on mouse hover.
why does the following not work?
Code:
void C_MAINMENU_DIALOG::OnMouseHover(UINT nFlags, CPoint point)
{
btnBmpCOD1.SetBitmap(::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_COD1_FOCUS)));
// TODO: Add your message handler code here and/or call default
// CDialog::OnMouseHover(nFlags, point);
}
regards
gerwin
-
Re: Set Focus On Mouse Hover (MFC)
Is the OnMouseHover handler getting called?
What does ::LoadBitmap return?
-
Re: Set Focus On Mouse Hover (MFC)
I used the wizard to call the WM_ message.
I will check anyway.
the statement calls the bitmap for focus.
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
Quote:
Originally Posted by
TBBW
I used the wizard to call the WM_ message.
I will check anyway.
That's fine, but it doesn't mean that an error didn't occur when the wizard generated the code (and the message map entry didn't get inserted properly).
Just put a breakpoint in the handler and run under a debugger.
Also check the return value of LoadBitmap. There could be a problem loading the bitmap.
-
Re: Set Focus On Mouse Hover (MFC)
To Arjay,
I think was not clear enough on my problem.
What I want is, when the mouse pointer enters the button area, the button should change
to a different color or picture. so basically it is not the hover message I have to use.
I can't find a message like 'mouse_over' should it should be done else.
I saw examples using mouse_move, but they are not bullit proof.
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
You need to handle the BCN_HOTITEMCHANGE message.
If you are using VC2005 or VC2008, you can add this message handler by opening the dialog in the resource editor, right clicking on the button and choose, "Add Event handler". When the Event Handler Wizard opens, select BCN_HOTITEMCHANGE under message types and choose "Add and Edit".
When the code is generated, put a break point inside the new handler, run the app in debug mode, and put the mouse over the button. The new button handler should be hit. This handler gets called when the mouse enters the button rect and also leaves it. You can determine which state by checking the dwFlags param. See NMBCHOTITEM in msdn. When you add the handler you can see how this is wired up from the generated handler.
-
Re: Set Focus On Mouse Hover (MFC)
Hi Arjay,
did as told!!
and did some add. searching.
but i'm stuck ...
this is the code:
Code:
void C_MAINMENU_DIALOG::OnBnHotItemChangeButton1(NMHDR *pNMHDR, LRESULT *pResult)
{
// This feature requires Internet Explorer 6 or greater.
// The symbol _WIN32_IE must be >= 0x0600.
LPNMBCHOTITEM pHotItem = reinterpret_cast<LPNMBCHOTITEM>(pNMHDR);
MessageBox(0,L"in",0);
if( pHotItem->dwFlags & HICF_ENTERING )
MessageBox(0,L"if",0);
else
MessageBox(0,L"else",0);
// TODO: Add your control notification handler code here
*pResult = 0;
}
the most important problem the above copied code segment is never called.
(message map is avail; ON_BN_CLICKED handler is working fine)
I'm using msvc 2008 in a win 7 environment.
do I need to include some extra parts (manifest)?
because the feature must use IE 6.0 or higher etc. ...
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
Please post the code of the MESSAGE_MAP entries.
-
Re: Set Focus On Mouse Hover (MFC)
In the .h file:
Code:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
afx_msg void OnBnHotItemChangeButton1(NMHDR *pNMHDR, LRESULT *pResult);
and in the .cpp file:
Code:
BEGIN_MESSAGE_MAP(C_MAINMENU_DIALOG, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &C_MAINMENU_DIALOG::OnBnClickedButton1)
ON_NOTIFY(BCN_HOTITEMCHANGE, IDC_BUTTON1, &C_MAINMENU_DIALOG::OnBnHotItemChangeButton1)
END_MESSAGE_MAP()
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
Arjay,
this is the 'ghost';
Code:
BOOL C_CODMENU::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
I do not understand the line Set this to include all the common ......
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
Quote:
Originally Posted by
TBBW
Arjay,
this is the 'ghost';
Code:
BOOL C_CODMENU::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
I do not understand the line Set this to include all the common ......
regards,
ger
I don't know either. Have you looked in Msdn for InitCommonControlsEx? I'll bet the documentation will explain the settings.
-
Re: Set Focus On Mouse Hover (MFC)
did some additional search work and
The lines
Code:
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
should be as it is.
I also found out, you have to check the option 'common control manifest' when the project
is created (using the MFC wizard gives you a linker add. in the stdafx.h).
as I do not get any erros (compile and link) I think I included everything (Libs/headers).
Apart from a working button the ON_NOTIFY routine is still not called.
regards,
ger
-
1 Attachment(s)
Re: Set Focus On Mouse Hover (MFC)
Start over. Create a new MFC dialog application in Visual Studio 2008. Use all the defaults (other than the application type).
Open the resource editor, drag a button on the dialog.
Right click on the button, and choose "Add event handler..."
Select "BCN_HOTITEMCHANGED".
Click "Add and edit"
When it takes you to the method, put the mouse cursor on the line that begins with LPNMBCHOITEM and press F9 (to set a breakpoint).
Build the project and press F5 to run the app in debug mode.
Run the mouse over the button. The breakpoint should be hit.
IMPORTANT: do this by creating a brand new VC++ 2008 MFC dialog application project as I've outlined above.
Attached is a working VS2008 MFC dialog project, all you have to do is put a breakpoint inthe hotitemchanged handler, build the project, and run it with F5.
Once you get it working in a new project, compare with your project to find out why your project doesn't work.
-
Re: Set Focus On Mouse Hover (MFC)
Arjay,
Thanks for the reply, will have a go.
but first I have to update, to include afxcontrolbar.h
keep you informed
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
Quote:
Originally Posted by
TBBW
but first I have to update, to include afxcontrolbar.h
Why?
Look, if you hope to figure this out, then why don't you try to create a new project or run the existing sample that I provided?
The reason being is that you need to determine if the functionality that I provided (or is default in a new project) works for you.
If you modify the program by adding afxcontrolbar.h first, you won't know what's causing the issue (i.e the project is broken on your system, or YOU broke the project by modifying it).
-
Re: Set Focus On Mouse Hover (MFC)
sorry was not clear enough have to update visual studio first. afxcontrolbar.h is not there.
I downloaded your example, compiled it and it gave on error on afx...h not avail.
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
What version of Visual Studio are you using? If it's the Express version, MFC isn't supported.
-
Re: Set Focus On Mouse Hover (MFC)
Arjay
I'm using Microsoft Visual Studio 2008 Pro Edition
Version 9.0.30729.1 SP
Loaded your hover project, THANKS!, worked super!
Made a new project called TEST, same style as your hover, worked super also.
Went back to my project, replace the bitmap button with a normal control button
(bitmap set to false) and quess what it worked.
Turned bitmap on again and owner draw to false
the project is still working (but at this time the bitmap is not visible, only the caption text)
Now the killer,
if i turn on bitmap and owner draw, both true. bitmap is visible.
but now the hotitemchange routine is not called.
so the change from standard button to bitmap button is the problem, i think ....
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
Run the Spy++ tool and look at the styles for the button and compare them with the styles for the owner drawn button. There might be a style missing that you can add back (to get the hover message to fire).
Also, if you installed the MFC source code when you installed Visual C++, you can step into the MFC source code during button creation to try to figure out the issue.
-
1 Attachment(s)
Re: Set Focus On Mouse Hover (MFC)
I will put on my sherlock holmes goggles!
in the meantime I made a quick HOVER project for you, the fault is
in this project also, see att.
regards,
ger
Please let me know if the attached file(s) is/are working!
-
Re: Set Focus On Mouse Hover (MFC)
Spy++ Output:
Normal Button Styles:
WS_CHILDWINDOW
WS_VISIBLE
WS_TABSTOP
BS_PUSHBUTTON
BS_TEXT
extended styles:
WS_EX_LEFT
WS_EX_LTRREADING
WS_EX_RIGHTSCROLLBAR
WS_EX_NOPARENTNOTIFY
the bitmap button:
WS_CHILDWINDOW
WS_VISIBLE
WS_TABSTOP
BS_OWNERDRAW
BS_BITMAP
extended styles:
WS_EX_LEFT
WS_EX_LTRREADING
WS_EX_RIGHTSCROLLBAR
WS_EX_NOPARENTNOTIFY
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
Arjay,
did you managed to download my sample?
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
Yes. It doesn't look like hover works with owner draw.
-
Re: Set Focus On Mouse Hover (MFC)
You have to handle hover yourself with _TrackMouseEvent() and handle the WM_MOUSELEAVE message.
-
Re: Set Focus On Mouse Hover (MFC)
just an idea not sure that it will work but if you where to encapsulate the button in another class such as a view class and use the OnMouseMove or Hover message handler
-
Re: Set Focus On Mouse Hover (MFC)
Hi,
I was already afraid you guys would came up with that idea….
So can’t use HOTITEMCHANGE, will revert to mouse tracking
Did some field work;
Have to use: mouse-leave, mouse-move and mouse-hover
ON_WM_MOUSEMOVE()
ON_WM_MOUSELEAVE()
ON_WM_MOUSEHOVER()
Mouse-move will house the tracking event, correct?
Mouse-hover will change the bitmap to focus
And
Mouse-leave will change it to normal again.
So I need to create a new class, let’s call it HOT.
Which type/base should I use, when I start the add class wizard?
And
How do I clue the class to the button using this new HOT class,
(I think something like select control and right click, do the add option…)
Can I use the same class (Hot) on more than one button or do
I have to make a HOT1 and HOT2 and so on.
Regards,
Ger
-
Re: Set Focus On Mouse Hover (MFC)
I would use CView since it is simple and handles mouse messages and I would use it for one button if you want the action to happen just for that button and to glue it to the CView you make CView the base class
class mybutton : public CView
{ ...
...
-
Re: Set Focus On Mouse Hover (MFC)
That's an awful lot of overhead for a button.
In my opinion, you should just derive from CButton and make it owner draw and handle ALL the drawing yourself. Since CButton derives from CWnd, it has all the mouse handling capabilities without all the overhead of a CView.
Somewhere I have a whole CButton owner-draw class that I used in some project or other for toolbar buttons. It handles hover and all the other button stuff. I'll see if I can dig it up.
I haven't been following this thread too closely. What version of VS are you using?
-
Re: Set Focus On Mouse Hover (MFC)
yeah i know its alot of overhead but it was just an idea that i had along time ago to encapsilate the onmousemove action in an owner drawn button to change the bitmap,in which i had dropped the action for now and will work on it when I am doing clean up code on appearance so if you have something better I would like to know myself so please post to this thread what you were posting thanks hoxsiew
-
Re: Set Focus On Mouse Hover (MFC)
ver 6 introductory edition can't afford full version yet $900
-
Re: Set Focus On Mouse Hover (MFC)
(That was directed at the OP).
Where are you shopping? I think you could find a LOT better deal than that.
-
Re: Set Focus On Mouse Hover (MFC)
Hi,
I use VS 2008 SP1.
made a new class CHot of the type CButton
made also the mouse handlers (leave, hover and move)
but how do I glue it to the desired button??
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
What do you mean by "glue"? How are you using this button? In a CDialog? Just add a button the way you normally would in the resource editor, then right-click, add-variable, and type in your derived button class instead of CButton.
-
Re: Set Focus On Mouse Hover (MFC)
Hi,
That is what I did.
But when I change CButton in CHot (the one I created)
I have to fill in a variable as well, I put in dummy.
Pushing OK results in an error.
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
Put in a dummy for what?
Should be like this:
http://hoxnet.com/cap1.jpg
-
Re: Set Focus On Mouse Hover (MFC)
Hi,
where can I find the variable(in your case m_Mybutton1), which I have to use?
or do I have to create one ....
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
It will create it for you.
If your class, CHot is part of your project, then the class wizard will find it, include it's header, and add a variable with the name you give it. That's why it's called a wizard.
-
1 Attachment(s)
Re: Set Focus On Mouse Hover (MFC)
Hi,
unable to solve...
put my project in the Att.
regards,
ger
-
1 Attachment(s)
Re: Set Focus On Mouse Hover (MFC)
Since you are using a CBitmapButton, wouldn't it make sense to derive the CHot class from CBitmapButton?
In the CHOVERDlg header, the btnBmp is incorrectly declared from CBitmapButton. How do you expect to get the new overridden behavior of CHot if you don't declare a variable from CHot?
-
Re: Set Focus On Mouse Hover (MFC)
Hi Arjay,
downloaded your prog, THANKS !!
I do not fully understand the changes made, I will study them during the weekend.
will let You know.
Apart from that, how do I change the bitmap in the 'mouse handlers' in HOT.
I get the message "btnBmp" unknow.
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
HI Arjay,
Did some homework.
a couple of thinks I do not fully understand.
I tested the att. things work as I want.
The only thing left is to change the button bitmap on hover and leave.
I tested the program leaving out the HOT.h and HOT.cpp
the mouse handlers react on the whole dialog, that I do understand.
this is not what I want, so I have to 'connect' the mouse handlers to the button instead.
We have to create a new class, called HOT. and attach it to the button.
understand that part as well.
So at this stage we have three classes called HOVER, HOVERDlg and HOT.
the Bitmap object is decleared in the HOVERDlg, called btnbmp
you commented the statement: CBitmapButton btnBmp;
and put in; CHot btnBmp;
I do not fully understand this.
I think you used the wizard also to "connct" the class to the bitmap-button.
you put in a variable name called; m_buttonhot.
and also put commented out the statement;
DDX_Control(pDX, IDC_BUTTON1, m_ButtonHot);
also this i do not fully understand.
Using calls like 'MessageBox(0,0,0) I know where the program is.
now I have to remove these Messages and change the the button image.
How do I make the chanege as the btnbmp is decleared in HOVERDlg and
is not know in CHOT.
That part, the interaction between the classes, is hard to get used to.
regards,
ger
-
Re: Set Focus On Mouse Hover (MFC)
Hi Guys,
FOUND IT !!!
was searching in the wong direction....
iso of redrawing the button I'm using the focus option!
in the mouse hover handler I use
PostMessage(WM_SETFOCUS, TRUE, 0);
and in the leave handler I use
PostMessage(WM_KILLFOCUS, TRUE, 0);
btw do I have to use sendmessage or postmessage??
yet I would like to have an answer on the two quest. in the previous post. (Please)
regards,
ger