Menu

(win32)How do i subclass a control?

2003-01-15
2012-09-26
  • Nobody/Anonymous

    have seen some msgs where people are talking about "Subclassing " a control. Can anyone explain me what is that about?

     
    • Nobody/Anonymous

      Well say you want to catch when you hit the scrollbar in a listbox control.
      It is impossible with just a normal listbox because the messages from the
      scrollbar in the listbox is handled internally.

      If you want to try something - download the source from the tutorial
      at http://www.winprog.org/tutorial/ and load the ctl_one project
      into Dev-cpp. Compile it and see how it looks.

      Now add a global variable and a new function:
      --- snip ---
      WNDPROC OldProc; // This holds a pointer to DlgProc
      HWND hWndc; // This can be moved to the DlgProc function

      LRESULT CALLBACK NewProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
      {
          switch(msg)
          {
          case WM_VSCROLL:
              MessageBox(hwnd, "You are pressing the scrollbar!", "WoW", MB_OK);
              break;

          default:
              break;
          }

          return CallWindowProc(OldProc, hwnd, msg, wParam, lParam);
      }
      --- snip ---
      In the WM_INITDIALOG add
      --- snip ---
                  hWndc = GetDlgItem(hwnd, IDC_LIST);
                  OldProc = (WNDPROC)SetWindowLong(hWndc, GWL_WNDPROC, (LONG)NewProc);
      --- snip ---
      This will replace the original CALLBACK function with the one you supplied.
      From now on all messages goes to NewProc first and are then dispatched
      to the original DlgProc function!
      This can be used for several things, this is just an example!
      /Roger

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.