Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code in my windowless framework is an area like just button,but some code I wanted to run it only once until the mouse move on the area next time.
All those code are in WM_MOUSEMOVE.
C++
int ClickAreaPtInfo(HWND hWnd, int x, int y, int sizex, int sizey, LPARAM lParam,int &value)
        {
            POINT pt;
            pt.x = LOWORD(lParam);
            pt.y = HIWORD(lParam);
            RECT rect;
            GetClientRect(hWnd, &rect);
            RECT  rc = { x,y,x + sizex,y + sizey };
            if (PtInRect(&rc, pt))
            {
                value = 1;
                return -1;
            }
            else
            {
                value = 0;
                return -1;
            }
            return -1;
        }
int _CreateMouseEvent(HWND hWnd, int x, int y, int sizex, int sizey, LPARAM lParam,RUNFUN function(), const wchar_t* Panelid, const wchar_t* CtlName)
        {
            int val = 0;
            int msg = 0;


            //int ist = 0;            int istprev = 0;
            RECT winrc;
            GetClientRect(hWnd, &winrc);
            RECT rc;
            RectTypeConvert(rc,x, y, sizex, sizey);

            if (Panelid == PanelID)
            {
                int nst = 1;
                //OutputDebugString(L"HOVER!\n");
                msg = 1;
                ClickAreaPtInfo(hWnd, x, y, sizex, sizey, lParam, val);
                if (val == 1)
                {

                    if (ClickMsg == 1) //Click(Get from WM_LBUTTONUP)
                    {
                        ClickMsg = 0;
                        function();
                    }
                    else
                    {
//It must be run for only once until the mouse leave next time,or it will  lead to a lot of resource occupation
                        if (CtlName == L"Button") //HOVER
                        {

                            if (nst == 1)
                            {
                                HDC hdc = GetDC(hWnd);
                                CreateSimpleButtonEx(hWnd, hdc, x, y, sizex, sizey, UICOLOR_GREENSEA, 1, ButtonText);
                                ReleaseDC(hWnd, hdc);
                                nst = 0;
                                return 0;
                            }
                            else
                            {
                                nst = 0;
                                return 0;
                            }
                            return 0;
                        }
                        if (CtlName == L"CloseButton") ///HOVER
                        {
                            if (nst == 1)
                            {
                                HDC hdc = GetDC(hWnd);
                                CreateRect(hWnd, hdc, x, y, sizex, sizey, UICOLOR_PEACHRED);
                                PanelDrawCloseBtn(hWnd, hdc, rc.right - 40, 0, 40, 40, 12, UICOLOR_WHITE);
                                ReleaseDC(hWnd, hdc);
                                nst = 0;
                                return 0;
                            }
                            else
                            {
                                nst = 0;
                                return 0;
                            }
                            return 0;
                        }
                        else
                        {
                            return 0;
                        }
                    }

                }
                if (val == 0) //Leave
                {
                        nst = 1;
                        InvalidateRect(hWnd, &rc, 0); //It must be run for only once until the mouse leave next time,or it will  lead to a lot of resource(CPU) occupation
 
                }
            }
            if (Panelid == PrevPanelID)
            {
                msg = 1;
            }

            else
            {
                msg = 0;
            }
            

            return 0;
        }

any solution?thanks!

What I have tried:

I add some new value (like "nst" in my code) to check,but it seem useless.
see
http://db.vertexstudio.xyz/lnk/PanelPic/debuginf.png
Posted
Updated 1-Oct-21 21:39pm
v2
Comments
Richard MacCutchan 1-Oct-21 6:22am    
Use a static boolean variable. When you enter the event handler check if it is TRUE. If it is then exit the handler, if not set it to TRUE and continue. When you leave the handler set it to FALSE to allow further events.
EnderMo233 1-Oct-21 7:12am    
Could you please to modify my code?
When I do it by myself it still seems useless
Richard MacCutchan 1-Oct-21 7:15am    
Sorry, no. I have no idea what that code is supposed to be doing or when it does it. And to be honest, it is not clear what you really want.
EnderMo233 1-Oct-21 7:30am    
It is a button in windowless framework.
So if your pointer is in the rect,it means hover and it color get darker and lighter,and if pointer is not in rect,it means leave and the control turned in to normal.
but,it cause a problem.
When your pointer is not in the rect,and window redraw everytime when you move the mouse,and it takes up a lot of CPU,so I wanted to redraw the window only once to make the button normal but don't want to redraw everytime I move the mouse.
also if mouse is in the rect ,it still redraw when I move the mouse,I also wanted to redraw it only once.
Richard MacCutchan 1-Oct-21 8:27am    
Sorry, but that does not sound like there is a simple code fix. You will have to do some experimentation for yourself to try and control it as you want. As I suggested above, you need some flag to tell you when the mouse first moves into the position to change the display. And look at exactly where you are drawing on your panel, make sure it only happens inside the WM_PAINT handler.

This is mostly a matter of semantics but your code does not "run an event." It responds to events by calling an event handler function. What you need to do is essentially what Richard wrote. Set a flag when the mouse has entered the region of interest and you have handled that event and then clear the flag when the mouse has left the region. Then if the event handler sees the flag set it does not handle the event. The effect will be the event is handled only the first time the mouse enters the region. This requires that you track all mouse movements and check whether or not it is in your region of interest.
 
Share this answer
 
Comments
EnderMo233 2-Oct-21 5:07am    
Thank you for replay.
I means,in my code,the window redraw when everytime I move my mouse,it takes up a lot up CPU.
So,I only wanted to redraw the window for only once if my mouse is not in the clickarea.here is an image.
http://db.vertexstudio.xyz/lnk/PanelPic/debuginf.png
case WM_MOUSEMOVE:
{
//CreateClickEvent(hWnd, rc.right - 40, 0, 40, 40, lParam, CloseWindow, L"Init");
CreateMouseEvent(hWnd, 20, 60, 140, 40, lParam, test, L"Init",L"Button");
CreateMouseEvent(hWnd, 20, 120, 140, 40, lParam, test3, L"Init", L"Button");
CreateMouseEvent(hWnd, 20, 180, 140, 40, lParam, btn3, L"Init",L"Button");
CreateMouseEvent(hWnd, rc.right - 40, 0, 40, 40, lParam, CloseWindow, L"Init",L"CloseButton");
break;
}
"Init" means the first panel.
The idea with the static var is good, but depending on your code can it be better to have some global object with that var.

The static bool is normal boolm but in global level or outside of functions. It must be declared before it can be used.

C++
bool eventWasFired = false; 

int ClickAreaPtInfo(HWND hWnd, int x, int y, int sizex, int sizey, LPARAM lParam,int &value)
{
  if( eventWasFired ) return 0;
  eventWasFired = true;
 
Share this answer
 
Comments
EnderMo233 1-Oct-21 20:50pm    
I will show you more to makes this more clearly.
http://db.vertexstudio.xyz/lnk/PanelPic/debuginf.png
and also,show you more codes.
case WM_MOUSEMOVE:
{
//CreateClickEvent(hWnd, rc.right - 40, 0, 40, 40, lParam, CloseWindow, L"Init");
CreateMouseEvent(hWnd, 20, 60, 140, 40, lParam, test, L"Init",L"Button");
CreateMouseEvent(hWnd, 20, 120, 140, 40, lParam, test3, L"Init", L"Button");
CreateMouseEvent(hWnd, 20, 180, 140, 40, lParam, btn3, L"Init",L"Button");
CreateMouseEvent(hWnd, rc.right - 40, 0, 40, 40, lParam, CloseWindow, L"Init",L"CloseButton");
break;
}
thank you for helping me.
EnderMo233 2-Oct-21 4:48am    
by the way,leave means is has been hoverd,but you can see there is a lot of "CreateMouseEvent" there,and I don't know what should I do next.
EnderMo233 2-Oct-21 5:00am    
if you use global var,and if there is more than one MouseEvent in this panel( in CreateMouseEvent(hWnd, 20, 60, 140, 40, lParam, test, L"Init",L"Button"),"Init" is a panel).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900