C++ WINAPI How can I run an event of mouse only once?

Viewed 91

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.

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;
        }

then handle CreateMouseEvent in WM_MOUSEMOVE:

case WM_MOUSEMOVE:
{
    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;
}

And I will also give a picture to this question! http://db.vertexstudio.xyz/lnk/PanelPic/debuginf.png

any solution?thanks!

1 Answers

Some of your problems have already been mentioned above.

There are some variables that are not defined in your example, neither type nor value, but I can extrapolate some at least.

Also, in several places, you try to directly compare a C style string pointer (not a std::string!) to another C style string pointer.

    if (Panelid == PanelID) is one place
    
    if (CtlName == L"Button") //HOVER  === is another

    if (CtlName == L"CloseButton") ///HOVER another

You can use something like int _wcsnicmp( const wchar_t *string1,const wchar_t *string2, size_t count); or whatever your compiler supports. Optionally, you could just use std::wstring.

As for the click, you need to save the rect you get with GetClientRect(hWnd, &winrc); It needs persistence, ie saved outside of the function and accessible. Then compare to this rectangle and act accordingly.

Related