September 26, 2022, 06:31:00 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


OnMouseLeave

Started by Bruce Peaslee, December 29, 2005, 12:10:43 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bruce Peaslee

It's easy to detect when the curser enters a window, but I'm having trouble detecting when it leaves. From MSDN, I am trying the following:


struct TRACKMOUSEEVENT
{
ÂÃ,  UINT cbSize;
ÂÃ,  UINT dwFlags;
ÂÃ,  UINT *hwndTrack;
ÂÃ,  UINT dwHoverTime;
}
...
MyMapWindow::OnMouseMove(int x, int y, int flags),int
{
TRACKMOUSEEVENT *TrackEvent;
TrackEvent = new(TRACKMOUSEEVENT,1);
MyMapWindow *pTemp;
pTemp = this;
pTemp->DrawRect(100,10,10,10,BLACK,BLACK);
TrackEvent->cbSizeÂÃ,  = 16;
TrackEvent->dwFlags = TME_LEAVE;
TrackEvent->hwndTrack = pTemp;
TrackEvent->dwHoverTime = 100;
TrackMouseEvent(TrackEvent);
return true;
}

MyMapWindow::WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int
{
select message
{
case 675: //OnMouseLeave
MessageBox(this,"Left","",0);
}
return MDIChildWnd!!WndProc(message, wparam, lparam);
}


Thanks in advance.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Parker

What's easier, handling the WM_SETCURSOR message, testing if the HWND is not equal to yours. I don't think there is a mouse leave event, I looked for it once.

case WM_SETCURSOR:
if (wParam <> this->m_hWnd)
{
MessageBox(this, "Mouse left the window", "Notice", 0);
}

It may be sent whenever the mouse moves though, which would be a problem.

Edit: Oh, I see on MSDN WM_MOUSELEAVE. I'll check, one minute.

Ionic Wind Support Team

Isn't Microsoft fun ;)

There are two TrackMouseEvent funcitons, one with an underscore and one without.  Use the one with the underscore.

DECLARE IMPORT,_TrackMouseEvent(tme as TRACKMOUSEEVENT),INT;

Also you don't need to use NEW and a pointer. 

TRACKMOUSEEVENT tme;
tme.cbSize = LEN(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = m_hwnd;
tme.dwHoverTime = 25;
_TrackMouseEvent(tme);

Whats this?


MyMapWindow *pTemp;
pTemp = this;
pTemp->DrawRect(100,10,10,10,BLACK,BLACK);


Provided you wanted to draw a rectangle on each and every mouse move message you don't need such pointer stuff.  Simply:


DrawRect(100,10,10,10,BLACK,BLACK);


would do it.  The call is within a class method that belongs to the window class.  So DrawRect is a native function to that class.
Ionic Wind Support Team

Ionic Wind Support Team

And using pTemp like you did wouldn't work either.  It's looking for a handle to a windows window.  Which in this case is simply m_hWnd as illustrated in my previous post.

Paul.
Ionic Wind Support Team

Parker

Yeah, I was about to recommend using this->m_hWnd, but it seems I've been beaten to the point ;)

Ionic Wind Support Team

Still that's not enough.  Since you need to subclass the button because you want the mouse move messages from the button, and not the window.  Working on an example as we speak.

Paul.
Ionic Wind Support Team

Ionic Wind Support Team

Here is an example for a button in a dialog.  The important thing to remember about _TrackMouseEvent is the WM_MOUSELEAVE message is sent to the button, not the parent window.



#define BUTTON_1 1
#define STATIC_2 2
#define STATIC_3 3

DECLARE IMPORT,InvalidateRect(hWnd as unsigned int,lpRect as POINTER,bErase as INT),INT;
DECLARE IMPORT,SetPropA(hwnd as unsigned int,lpString as STRING,hData as unsigned int),INT;
DECLARE IMPORT,_TrackMouseEvent(tme as TRACKMOUSEEVENT),INT;

struct TRACKMOUSEEVENT
{
    unsigned INT cbSize;
    unsigned INT dwFlags;
    unsigned INT hwndTrack;
    unsigned INT dwHoverTime;
}

CONST TME_HOVER = 1;
CONST TME_LEAVE = 2;
CONST WM_MOUSEHOVER = 0x2A1;
CONST WM_MOUSELEAVE = 0x2A3;


class HoverButton:CButton
{
declare OnMouseMove(int x,int y,int flags),int;
declare WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int;
CStatic *pStatic; //reference to another control for displaying messages
}

class dlg:dialog
{
declare OnInitDialog(),int;
declare OnClose(),int;
declare OnControl(int nID, int nNotifyCode, unsigned int hControl),int;
HoverButton *pHover;
}

global sub main()
{
dlg d1;
d1.Create(0,0,300,202,0x80C80080,0,"Caption",0);
d1.AddControl(CTBUTTON,"Button1",115,91,70,20,0x5000000B,0x0,BUTTON_1);
d1.AddControl(CTSTATIC,"On Mouse Leave demo",92,27,129,18,0x5000010B,0x0,STATIC_2);
d1.AddControl(CTSTATIC,"",87,138,126,21,0x50000100,0x0,STATIC_3);

d1.DoModal();
return 0;
}

dlg::OnClose(),int
{
if(pHover <> NULL)
delete pHover;
CloseDialog(1);
return true;
}

dlg::OnInitDialog(),int
{
/* Initialize any controls here */
CControl *pControl;
pHover = new(HoverButton,1);
//tell the button to use our class
pControl = GetControl(BUTTON_1);
pHover->m_hWnd = pControl->m_hWnd;
SetPropA(pHover->m_hWnd,"THIS",pHover+0);
//use the static control for info display
pHover->pStatic = GetControl(STATIC_3);
CenterWindow();
return true;
}

dlg::OnControl(int nID, int nNotifyCode, unsigned int hControl),int
{
select nID
{
case BUTTON_1:
if(nNotifyCode = 0)
{
/*button clicked*/
}
}
return true;
}


HoverButton::OnMouseMove(int x,int y,int flags),int
{
TRACKMOUSEEVENT tme;
tme.cbSize = LEN(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER | TME_LEAVE;
tme.hwndTrack = m_hwnd;
tme.dwHoverTime = 25;
_TrackMouseEvent(tme);
return true;
}

HoverButton::WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int
{
select message
{
case WM_MOUSEHOVER:
pStatic->SetText("Hover!");
SetColor(m_fg,RGB(255,255,0));
InvalidateRect(m_hWnd,NULL,true);
case WM_MOUSELEAVE:
pStatic->SetText("Mouse Leave!");
SetColor(m_fg,RGB(200,200,200));
InvalidateRect(m_hWnd,NULL,true);
}
return CButton!!WndProc(message,wParam,lParam);
}
Ionic Wind Support Team

Bruce Peaslee

There is no button in my example. I have several child windows in a frame. When the mouse enters a window, I want to display some data in a popup window, which closes when the mouse leaves the original window.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Ionic Wind Support Team

OK Here is a simpler example with a child window:


DECLARE IMPORT,_TrackMouseEvent(tme as TRACKMOUSEEVENT),INT;
struct TRACKMOUSEEVENT
{
    unsigned INT cbSize;
    unsigned INT dwFlags;
    unsigned INT hwndTrack;
    unsigned INT dwHoverTime;
}

CONST TME_HOVER = 1;
CONST TME_LEAVE = 2;
CONST WM_MOUSEHOVER = 0x2A1;
CONST WM_MOUSELEAVE = 0x2A3;

class HoverWindow:window
{
declare OnMouseMove(int x,int y,int flags),int;
declare WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int;
}

class MainWindow:window
{
declare OnClose(),int;
}
int run;

global sub main()
{
MainWindow win;
HoverWindow child;
win.Create(0,0,300,100,AWS_CAPTION | AWS_SYSMENU | AWS_AUTODRAW | AWS_VISIBLE | AWS_BORDER,0,"HoverDemo",0);
child.Create(10,10,60,60,AWS_VISIBLE|AWS_BORDER|AWS_CHILD,0,"",win);
win.CenterWindow();
run = 1;
do
{
wait();
} until run = 0;
return;
}

MainWindow::OnClose(),int
{
run = 0;
return true;
}

HoverWindow::OnMouseMove(int x,int y,int flags),int
{
TRACKMOUSEEVENT tme;
tme.cbSize = LEN(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER | TME_LEAVE;
tme.hwndTrack = m_hwnd;
tme.dwHoverTime = 25;
_TrackMouseEvent(tme);
return true;
}

HoverWindow::WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int
{
select message
{
case WM_MOUSEHOVER:
SetWindowColor(RGB(255,0,0));
case WM_MOUSELEAVE:
SetWindowColor(RGB(255,255,255));

}
return Window!!WndProc(message,wParam,lParam);
}
Ionic Wind Support Team

Ionic Wind Support Team

And another example that will popup another window.  Let me know when you've had enough ;)


DECLARE IMPORT,_TrackMouseEvent(tme as TRACKMOUSEEVENT),INT;
struct TRACKMOUSEEVENT
{
    unsigned INT cbSize;
    unsigned INT dwFlags;
    unsigned INT hwndTrack;
    unsigned INT dwHoverTime;
}

CONST TME_HOVER = 1;
CONST TME_LEAVE = 2;
CONST WM_MOUSEHOVER = 0x2A1;
CONST WM_MOUSELEAVE = 0x2A3;

class HoverWindow:window
{
declare OnMouseMove(int x,int y,int flags),int;
declare WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int;
window *popup;
}

class MainWindow:window
{
declare OnClose(),int;
}

class PopupWindow:window
{
}

int run;

global sub main()
{
MainWindow win;
HoverWindow child;
PopupWindow child2;
win.Create(0,0,300,100,AWS_CAPTION | AWS_SYSMENU | AWS_AUTODRAW | AWS_VISIBLE | AWS_BORDER,0,"HoverDemo",0);
child.Create(10,10,60,60,AWS_VISIBLE|AWS_BORDER|AWS_CHILD,0,"",win);
//create the popup invisible
child2.Create(100,10,120,60,AWS_CAPTION|AWS_CHILD|AWS_AUTODRAW,0x80,"Popup Info",win);
child2.WriteText(5,5,"Just some info");
child.popup = &child2;

win.CenterWindow();
run = 1;
do
{
wait();
} until run = 0;
return;
}

MainWindow::OnClose(),int
{
run = 0;
return true;
}

HoverWindow::OnMouseMove(int x,int y,int flags),int
{
TRACKMOUSEEVENT tme;
tme.cbSize = LEN(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER | TME_LEAVE;
tme.hwndTrack = m_hwnd;
tme.dwHoverTime = 25;
_TrackMouseEvent(tme);
return true;
}

HoverWindow::WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int
{
select message
{
case WM_MOUSEHOVER:
SetWindowColor(RGB(255,0,0));
popup->ShowWindow(SWSHOW);
case WM_MOUSELEAVE:
SetWindowColor(RGB(255,255,255));
popup->ShowWindow(SWHIDE);

}
return Window!!WndProc(message,wParam,lParam);
}
Ionic Wind Support Team

Bruce Peaslee

You can pause while I try to digest...   ;)
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Ionic Wind Support Team

Sorry...couldn't help myself.  Makes a good demo  ;D

This version displays a popup with a yellow background and makes it go away after a few seconds....more like a tooltip.

Not surprisingly this is pretty close to how tooltips are implimented.


DECLARE IMPORT,_TrackMouseEvent(tme as TRACKMOUSEEVENT),INT;
struct TRACKMOUSEEVENT
{
    unsigned INT cbSize;
    unsigned INT dwFlags;
    unsigned INT hwndTrack;
    unsigned INT dwHoverTime;
}

CONST TME_HOVER = 1;
CONST TME_LEAVE = 2;
CONST WM_MOUSEHOVER = 0x2A1;
CONST WM_MOUSELEAVE = 0x2A3;

class HoverWindow:window
{
declare HoverWindow();
declare OnMouseMove(int x,int y,int flags),int;
declare WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int;
declare OnTimer(int nIDEvent),int;
window *popup;
int bHover;
}

class MainWindow:window
{
declare OnClose(),int;
}

class PopupWindow:window
{
}

int run;

global sub main()
{
MainWindow win;
HoverWindow child;
PopupWindow child2;
win.Create(0,0,300,100,AWS_CAPTION | AWS_SYSMENU | AWS_AUTODRAW | AWS_VISIBLE | AWS_BORDER,0,"HoverDemo",0);
child.Create(10,10,60,60,AWS_VISIBLE|AWS_BORDER|AWS_CHILD,0,"",win);
//create the popup invisible
child2.Create(100,10,120,60,AWS_CHILD|AWS_AUTODRAW|AWS_BORDER,0,"Popup Info",win);
child2.SetWindowColor(RGB(255,255,0));
child2.DrawMode(TRANSPARENT);
child2.WriteText(5,5,"Just some info");
child2.WriteText(5,20,"For your program");
child.popup = &child2;

win.CenterWindow();
run = 1;
do
{
wait();
} until run = 0;
return;
}

MainWindow::OnClose(),int
{
run = 0;
return true;
}

HoverWindow::HoverWindow()
{
popup = null;
bHover = false;
return;
}

HoverWindow::OnMouseMove(int x,int y,int flags),int
{
if(bHover = false)
{
TRACKMOUSEEVENT tme;
tme.cbSize = LEN(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER | TME_LEAVE;
tme.hwndTrack = m_hwnd;
tme.dwHoverTime = 25;
_TrackMouseEvent(tme);
}
return true;
}

HoverWindow::OnTimer(int nIDEvent),int
{
popup->ShowWindow(SWHIDE);
StopTimer();
return true;
}

HoverWindow::WndProc(unsigned int message, unsigned int wparam, unsigned int lparam),int
{
select message
{
case WM_MOUSEHOVER:
bHover = true;
SetWindowColor(RGB(255,0,0));
StartTimer(2500); //close the popup after a few seconds
popup->ShowWindow(SWSHOW);
case WM_MOUSELEAVE:
bHover = false;
SetWindowColor(RGB(255,255,255));
StopTimer();
popup->ShowWindow(SWHIDE);

}
return Window!!WndProc(message,wParam,lParam);
}
Ionic Wind Support Team

Zen

Will onMouseLeave be added to the final class?

Lewis

Parker

One, or a few of these could be added to the installation ;)
Although only for windows, I'm sure it wouldn't work with linux.

Ionic Wind Support Team

Linux has it's own messages.  They could be adapted.
Ionic Wind Support Team

ExMember001

it look like this example didnt compile anymore...
what i want to do is near that.. i want to detect leftbuttondown inside a child window, but i cant get it to work.
can someone show me a little example on how to do that?

Ionic Wind Support Team

The one on the forums is old.  Look in your examples directory.
Ionic Wind Support Team

ExMember001

August 02, 2006, 01:07:20 PM #17 Last Edit: August 02, 2006, 02:38:15 PM by krypt
thanx i didnt saw it in the examples ;)

kryton9

Here are the updated source files for Paul's examples: