vaska11
vaska11

Reputation: 380

How to detect if static text button up action

So, I have three components on my dialog. Two are buttons(PUSHBUTTON), and the other is a static text (LTEXT). But all three controls act 'like' buttons. They are clickable and do whatever they are supposed to do.

So the code looks something like this:

// MyDlg.cpp
BOOL MyDlg::DefaultWindowProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
   switch( uMsg)
   {
       ...
       ...
       case WM_COMMAND:
            if( HIWORD(wParam) == 0 )
                dwSelectID = LOWORD(wParam);

            if ( dwSelectID == IDC_BUTTON1 )
                doSomething1();
            else if ( dwSelectID == IDC_BUTTON2 )
                doSomething2();
            else if ( dwSelectID == IDC_STATIC )
                doSomething3();
   }
}

It works fine. But the mechanism within those controls works differently. Two buttons runs the respective functions on LBUTTONUP whereas the static runs the respective function on LBUTTONDOWN. I am assuming these different behaviors occur just naturally. Is there something I can do to make the static text work the same?

Upvotes: 1

Views: 309

Answers (1)

vaska11
vaska11

Reputation: 380

Okay, so I followed Jonathan's suggestion, which was to use sub-classing.

I subclassed the static control so that the control could capture the LButtonUp message. When the subclassed control receives the message, it posts a user-defined message to its parent handle. Then the parent handle takes care of whatever action it needs to deal with.

Simple design of the code would be something like this:

// MyDlg.cpp
BOOL MyDlg::DefaultWindowProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
   switch( uMsg)
   {
       ...
       ...
       case SOME_SPECIAL_MSG:
            doSomething3():
            return (INT_PTR)TRUE;
       case WM_COMMAND:
            if( HIWORD(wParam) == 0 )
                dwSelectID = LOWORD(wParam);

            if ( dwSelectID == IDC_BUTTON1 )
                doSomething1();
            else if ( dwSelectID == IDC_BUTTON2 )
                doSomething2();
       ...
       ...
   }
}
// Static.cpp
LRESULT CALLBACK UDStatic::StaticProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam, DWORD_PTR pwRefData)
{
   UDStatic* pThis = (UDStatic*)pwRefData;
   switch (nMsg)
   {
      ...
      case WM_LBUTTONUP:
          pThis->OnLButtonUp(hWnd, wParam, lParam, pwRefData);
          break;
   }
}
void UDStatic::OnLButtonUp(hWnd, wParam, lParam, pwRefData)
{
    HWND hWndParent = GetParent(hWnd);
    if (hWndParent)
        PostMessage( hWndParent, SOME_SPECIAL_MSG, 0, 0);
}

The other way around solving the problem would be simply making a customized text button, so it would inherit the property of the button (BN_CLICKED); Gets the job done.

Upvotes: 1

Related Questions