How would one handle right clicks on a specific control for example, if the user was to right click a static control and I would like to display a menu.. I thought that I could do something like this..

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_COMMAND:
        {
             if (LOWORD(wParam) == MYCTRL)
             {
                    if (HIWORD(wParam) == WM_RBUTTONDOWN)
                    {
                          // code here..
                    }
             }
         }
         break;
     }
}

but it doesnt seem to work

thanks.

Recommended Answers

All 8 Replies

Yeah, I have read that.. wParam contains information on which virtual keys are held down and lParam contains the x and y co-ordinates of the mouse at the point when the message is triggered. I want to know how I could make this control specific, as this applies to the entire client area.

Thanks.

Use some imagination here.
You know where you clicked inside the client area. (From lParam)
You know the rectangular limits of the stati control (Lookup GetWindowRect).
So you can find out if you clicked inside the control or outside.
If it is inside, then do what you want to do.

The other way will be to subclass the static label control, which is unnecessary and hard work.

Ok thanks..

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_RBUTTONDOWN:
        {
             MessageBox(0,0,0,0);
        }
        break;
    }
}

I'm assuming this would be where the message would be placed, right clicking even then still does not do anything. You can see I have put a MessageBox() API inside the RBUTTONDOWN message and when testing nothing happens.

What is the problem here?

Thanks.

Are you sure that passing all parameters as 0 into the MessageBox function will work?
Are the other windows messages working properly? If they are not, then maybe the Windows Procedure isn't properly bound to the Main Window.

yes, MessageBox(0,0,0,0) works without a problem, I have tested it and just for the record if I change it to MessageBox(hwnd,"Title","Message",MB_ICONERROR); the same thing still happens, all of the other window messages do work correctly, such as WM_CREATE and WM_COMMAND etc.

Nothing wrong with the Message handling part. Must be something else. Try adding

return FALSE;

at the end of the Windows Procedure function.
Like

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_RBUTTONDOWN:
        {
             MessageBox(0,0,0,0);
        }
        break;
    }
    return FALSE;
}

I don't think your windows proc even compiles since it is not returning a value.
If that doesnt work, post your full windows procedure.

I found out the error by commenting out some window messages and pinpointed where it was going wrong..

case WM_NCHITTEST:
        {
             UINT uHitTest;
             uHitTest = DefWindowProc(hwnd, WM_NCHITTEST, wParam, lParam);
                 if(uHitTest == HTCLIENT)
                     return HTCAPTION;
                 else
                     return uHitTest;
        }
        break;

I was using this code to enable the window to be dragged by its client area, since I have the title bar hidden. Is there any way I can do this so it does not affect the right clicks etc? or some kind of modification to the above code?

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.