[RESOLVED] [win32] - mouse move message seems not be activated :(
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: [RESOLVED] [win32] - mouse move message seems not be activated :(

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    605

    [RESOLVED] [win32] - mouse move message seems not be activated :(

    Code:
    .................
    enum MouseButtons
    {
        None = 0,
        Left = 1,
        Right = 2,
        Middle = 3,
        X1 = 4,
        X2 = 5
    };
    
    int pos = 0;
    
    const string lab[6] = {"qwerty", "asdfgh", "zxcvbn", "poiuyt", "lkjhgf", "mnbvcx"};
    
    class label
    {
    private:
    
        HCURSOR hCursor=(HCURSOR)LoadCursor(GetModuleHandle(NULL),IDC_APPSTARTING);
        string strCaption="Label ";
        string strCursor="";
        color clrTextColor=0;
        color clrBackColor=0;
        bool blnTransparent=false;
        bool blnAutoSize=false;
        bool blnTabStop=false;
        int intTop=0;
        int intLeft=0;
        int intWidth=0;
        int intHeight=0;
    
        void TrackMouse(HWND hwnd, long HoverTime=0)
        {
            TRACKMOUSEEVENT tme;
            tme.cbSize = sizeof(TRACKMOUSEEVENT);
            tme.dwFlags = TME_HOVER | TME_LEAVE; //Type of events to track & trigger.
            tme.dwHoverTime = HoverTime; //How long the mouse has to be in the window to trigger a hover event.
            tme.hwndTrack = hwnd;
            TrackMouseEvent(&tme);
        }
    
        static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
        {
            static bool Tracking = false;
            static MouseButtons MBButtons;
            static bool blControl=false;
            static bool blShift=false;
            WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), propname);
            if (oldproc == NULL)
                MessageBox(NULL, "oldprocnull", "oldprocnull", MB_OK);
    
            label *inst = (label*)GetProp(hwnd, classprop);
            if (inst == NULL && msg == WM_NCCREATE)
            {
                inst = (label*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
                SetProp(hwnd, classprop, (HANDLE)inst);
            }
    
            if (inst == NULL)
                MessageBox(NULL, "instnull", "instnull", MB_OK);
    
            HBRUSH g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
    
            //Working with messages
            switch(msg)
            {
                case WM_CREATE:
                {
                    inst->Create(inst->intLeft, inst->intTop);
                }
                break;
                case WM_CTLCOLORSTATIC:
                {
                    if ( inst->blnTransparent == true)
                    {
                        SetTextColor((HDC)wParam, inst->clrTextColor);
                        SetBkMode( (HDC)wParam, TRANSPARENT);
                        return (LRESULT) GetStockObject(HOLLOW_BRUSH);
                    }
                    else
                    {
                        DeleteObject(g_hbrBackground);
                        SetTextColor((HDC)wParam, inst->clrTextColor);
                        SetBkColor((HDC)wParam,inst->clrBackColor);
                        SetBkMode((HDC)wParam, TRANSPARENT);
                        g_hbrBackground = CreateSolidBrush(inst->clrBackColor);
                        return (LONG)g_hbrBackground;
                    }
                }
                break;
                case WM_PAINT:
                {
                    if (inst->Paint==NULL) break;
                    PAINTSTRUCT  ps;
                    HDC hdc = BeginPaint(inst->hwnd, &ps);
                    inst->Paint(inst->hwnd,hdc);
                    EndPaint(inst->hwnd, &ps);
                }
                break;
                case WM_NCHITTEST:
                    return DefWindowProc(hwnd, msg, wParam, lParam);
    
                case WM_LBUTTONUP:
                case WM_RBUTTONUP:
                case WM_MBUTTONUP:
                case WM_XBUTTONUP:
                {
                    SetFocus(inst->hwnd);
    
                    int xPos = GET_X_LPARAM(lParam);
                    int yPos = GET_Y_LPARAM(lParam);
                    inst->MouseUp(MBButtons, blControl, blShift, xPos, yPos);
    
                }
                break;
    
                case WM_LBUTTONDOWN:
                case WM_RBUTTONDOWN:
                case WM_MBUTTONDOWN:
                case WM_XBUTTONDOWN:
                {
                    SetFocus(inst->hwnd);
    
                    int xPos = GET_X_LPARAM(lParam);
                    int yPos = GET_Y_LPARAM(lParam);
    
                    blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
                    blShift = ((wParam & MK_SHIFT) == MK_SHIFT);
    
                    if((wParam & MK_LBUTTON)!= false)
                        MBButtons=Left;
                    else if (wParam & MK_RBUTTON)
                        MBButtons=Right;
                    else if (wParam & MK_MBUTTON)
                        MBButtons=Middle;
                    else if (wParam & MK_XBUTTON1)
                        MBButtons=X1;
                    else if (wParam & MK_XBUTTON2)
                        MBButtons=X2;
    
                    inst->MouseDown(MBButtons, blControl, blShift, xPos, yPos);
    
                }
                break;
    
                case WM_MOUSEMOVE:
                {
                    if (!Tracking)
                    {
                        inst->TrackMouse(hwnd);
                        Tracking = true;
                        inst->MouseEnter();
                    }
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
    
                    if((wParam & MK_LBUTTON)!= false)
                        MBButtons=Left;
                    else if (wParam & MK_RBUTTON)
                        MBButtons=Right;
                    else if (wParam & MK_MBUTTON)
                        MBButtons=Middle;
                    else if (wParam & MK_XBUTTON1)
                        MBButtons=X1;
                    else if (wParam & MK_XBUTTON2)
                        MBButtons=X2;
    
                    inst->MouseMove(MBButtons,blControl,blShift,xPos,yPos);
    
                }
                break;
                case WM_MOUSEHOVER:
                {
                    inst->MouseHover();
                }
                break;
                case WM_MOUSELEAVE:
                {
                    inst->MouseLeave();
                    Tracking = false;
                }
    
                break;
                default:
                    break;
            }
    
            //If oldproc is NULL then use DefWindowProc - but shouldn't be!
            return oldproc ? CallWindowProc(oldproc, hwnd, msg, wParam, lParam) : DefWindowProc(hwnd, msg, wParam, lParam);
        }
    .................
    why the WM_MOVE is only sometimes activated?

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: [win32] - mouse move message seems not be activated :(

    Presumeably WM_MOUSEMOVE did work correctly at some point as you're used this code for some time. So what has changed since it last worked? How do you know that WM_MOUSEMOVE is only sometimes activated - what's it not doing that you expect it to do? Have you logged the msg's being received ? Is there a pattern to when WM_MOUSEMOVE isn't being activated as expected?

    Code:
    HBRUSH g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
    For every message received, you are creating a new brush but only deleting the current one in some cases in WM_CTLCOLORSTATIC - you have a resource leak.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2009
    Posts
    605

    Re: [win32] - mouse move message seems not be activated :(

    Quote Originally Posted by 2kaud View Post
    Presumeably WM_MOUSEMOVE did work correctly at some point as you're used this code for some time. So what has changed since it last worked? How do you know that WM_MOUSEMOVE is only sometimes activated - what's it not doing that you expect it to do? Have you logged the msg's being received ? Is there a pattern to when WM_MOUSEMOVE isn't being activated as expected?

    Code:
    HBRUSH g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
    For every message received, you are creating a new brush but only deleting the current one in some cases in WM_CTLCOLORSTATIC - you have a resource leak.
    currected:
    Code:
    HBRUSH g_hbrBackground = NULL;
    how i know that these isn't activated:
    Code:
    case WM_MOUSEMOVE:
                {
                    if (!Tracking)
                    {
                        inst->TrackMouse(hwnd);
                        Tracking = true;
                        inst->MouseEnter();
                    }
                    static int xPos = (int)(short) LOWORD(lParam);
                    static int yPos = (int)(short) HIWORD(lParam);
    
                    if((wParam & MK_LBUTTON)!= false)
                        MBButtons=Left;
                    else if (wParam & MK_RBUTTON)
                        MBButtons=Right;
                    else if (wParam & MK_MBUTTON)
                        MBButtons=Middle;
                    else if (wParam & MK_XBUTTON1)
                        MBButtons=X1;
                    else if (wParam & MK_XBUTTON2)
                        MBButtons=X2;
    
                    inst->MouseMove(MBButtons,blControl,blShift,xPos,yPos);
    
                }
                break;
    (MouseMove is like a lambda functions)
    when the window is created:
    Code:
    case WM_CREATE:
            {
                label1.setParent(hwnd);
                label1.setTransparent(true);
                label1.setBorder(1);
                label1.setAutoSize(true);
                label1.MouseMove=[](int Button, bool control, bool shift,int x, int y)
                {
                    label1.setText("X: " + to_string(x) + "  Y: " + to_string(y));
                };
    
            }
    i move the mouse, but isn't working

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: [win32] - mouse move message seems not be activated :(

    So if the text is changed when you comment out the code, the problem is either in TrackMouse(), MouseEnter(), or MouseMove(). So you'll need to debug the code to see where the problem occurs. What has changed since it last worked?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2009
    Posts
    605

    Re: [win32] - mouse move message seems not be activated :(

    Quote Originally Posted by 2kaud View Post
    So if the text is changed when you comment out the code, the problem is either in TrackMouse(), MouseEnter(), or MouseMove(). So you'll need to debug the code to see where the problem occurs. What has changed since it last worked?
    the text is changed the i do the mouse leave... see these code(very code is commented):
    Code:
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
        {
            static bool Tracking = false;
            static MouseButtons MBButtons;
            static bool blControl=false;
            static bool blShift=false;
    
            WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), propname);
            if (oldproc == NULL)
                MessageBox(NULL, "oldprocnull", "oldprocnull", MB_OK);
    
            label *inst = (label*)GetProp(hwnd, classprop);
            if (inst == NULL && msg == WM_NCCREATE)
            {
                inst = (label*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
                SetProp(hwnd, classprop, (HANDLE)inst);
            }
    
            if (inst == NULL)
                MessageBox(NULL, "instnull", "instnull", MB_OK);
    
            HBRUSH g_hbrBackground = NULL;
    
            //Working with messages
            switch(msg)
            {
                /*case WM_CREATE:
                {
                    inst->Create(inst->intLeft, inst->intTop);
                }
                break;
                case WM_CTLCOLORSTATIC:
                {
                    if ( inst->blnTransparent == true)
                    {
                        SetTextColor((HDC)wParam, inst->clrTextColor);
                        SetBkMode( (HDC)wParam, TRANSPARENT);
                        return (LRESULT) GetStockObject(HOLLOW_BRUSH);
                    }
                    else
                    {
                        DeleteObject(g_hbrBackground);
                        SetTextColor((HDC)wParam, inst->clrTextColor);
                        SetBkColor((HDC)wParam,inst->clrBackColor);
                        SetBkMode((HDC)wParam, TRANSPARENT);
                        g_hbrBackground = CreateSolidBrush(inst->clrBackColor);
                        return (LONG)g_hbrBackground;
                    }
                }
                break;
                case WM_PAINT:
                {
                    if (inst->Paint==NULL) break;
                    PAINTSTRUCT  ps;
                    HDC hdc = BeginPaint(inst->hwnd, &ps);
                    inst->Paint(inst->hwnd,hdc);
                    EndPaint(inst->hwnd, &ps);
                }
                break;*/
                case WM_NCHITTEST:
                    return DefWindowProc(hwnd, msg, wParam, lParam);
    
    			/*case WM_LBUTTONUP:
                case WM_RBUTTONUP:
                case WM_MBUTTONUP:
                case WM_XBUTTONUP:
                {
                    SetFocus(inst->hwnd);
    
                    int xPos = GET_X_LPARAM(lParam);
                    int yPos = GET_Y_LPARAM(lParam);
                    inst->MouseUp(MBButtons, blControl, blShift, xPos, yPos);
    
                }
                break;
    
                case WM_LBUTTONDOWN:
                case WM_RBUTTONDOWN:
                case WM_MBUTTONDOWN:
                case WM_XBUTTONDOWN:
                {
                    SetFocus(inst->hwnd);
    
                    int xPos = GET_X_LPARAM(lParam);
                    int yPos = GET_Y_LPARAM(lParam);
    
                    blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
                    blShift = ((wParam & MK_SHIFT) == MK_SHIFT);
    
    				if((wParam & MK_LBUTTON)!= false)
                        MBButtons=Left;
                    else if (wParam & MK_RBUTTON)
                        MBButtons=Right;
                    else if (wParam & MK_MBUTTON)
                        MBButtons=Middle;
                    else if (wParam & MK_XBUTTON1)
                        MBButtons=X1;
                    else if (wParam & MK_XBUTTON2)
                        MBButtons=X2;
    
                    inst->MouseDown(MBButtons, blControl, blShift, xPos, yPos);
    
                }
                break;*/
    
                case WM_MOUSEMOVE:
                {
                    int xPos = GET_X_LPARAM(lParam);
                    int yPos = GET_Y_LPARAM(lParam);
                    inst->setText("position: " + to_string(xPos));
                }
                break;
    
                default:
                    break;
            }
    
            //If oldproc is NULL then use DefWindowProc - but shouldn't be!
            return oldproc ? CallWindowProc(oldproc, hwnd, msg, wParam, lParam) : DefWindowProc(hwnd, msg, wParam, lParam);
        }
    these strange problem i only see it with mouse move. i said that i left you code, but i delete the log file code... sorry

  6. #6
    Join Date
    Apr 2009
    Posts
    605

    Re: [win32] - mouse move message seems not be activated :(

    it's crazy, when i do the mouse exit the text is changed lol
    Code:
    case WM_MOUSEMOVE:
                {
    
                    int xPos = GET_X_LPARAM(lParam);
                    int yPos = GET_Y_LPARAM(lParam);
                    inst->setText("position: " + to_string(xPos));
                    /*if (!Tracking)
                    {
                        inst->TrackMouse(hwnd);
                        Tracking = true;
                        inst->MouseEnter();
                    }
    
    
                    if((wParam & MK_LBUTTON)!= false)
                        MBButtons=Left;
                    else if (wParam & MK_RBUTTON)
                        MBButtons=Right;
                    else if (wParam & MK_MBUTTON)
                        MBButtons=Middle;
                    else if (wParam & MK_XBUTTON1)
                        MBButtons=X1;
                    else if (wParam & MK_XBUTTON2)
                        MBButtons=X2;
    
                    inst->MouseMove(MBButtons,blControl,blShift,xPos,yPos);*/
    
                }
                break;

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: [win32] - mouse move message seems not be activated :(

    i move the mouse, but isn't working
    So is the problem you aren't receiving the WM_MOUSEMOVE message or it is being received but it isn't being processed as expected? What have you changed since this was last working?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Apr 2009
    Posts
    605

    Re: [win32] - mouse move message seems not be activated :(

    Quote Originally Posted by 2kaud View Post
    So is the problem you aren't receiving the WM_MOUSEMOVE message or it is being received but it isn't being processed as expected? What have you changed since this was last working?
    i just add more tools nothing more. i left your code. do you need the entire class?

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: [win32] - mouse move message seems not be activated :(

    Well now seems like a good time to get to grips with debugging windows code!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Apr 2009
    Posts
    605

    Re: [win32] - mouse move message seems not be activated :(

    Quote Originally Posted by 2kaud View Post
    Well now seems like a good time to get to grips with debugging windows code!
    the problem isn't learn how use the debug... you know that code you did, is advanced
    so i can't acomplish that so easy

  11. #11
    Join Date
    Apr 2009
    Posts
    605

    Re: [win32] - mouse move message seems not be activated :(

    after more tests i found the problem. the problem was with window and not with label. was these extended style: WS_EX_COMPOSITED.
    so i have 1 question: if the WS_EX_COMPOSITED mess with mouse move, how can i avoid flickers on controls?(i'm not speaking about WM_PAINT, on these situation i must use the double buffer way)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center