Link to home
Start Free TrialLog in
Avatar of edvinson
edvinsonFlag for United States of America

asked on

Simple Win32 Subclass Not Working

Trying to subclass a simple checkbox control in my dialog.

Below is my code.


// Favour small code
#pragma optimize("gsy", on)

#if !defined(_WINDOWS_)  // Don't optimize if WINDOWS.H has already been included

#define WIN32_LEAN_AND_MEAN           // Enable LEAN_AND_MEAN support

#endif
#include <windows.h>
#include <commctrl.h>
#include <string>
#include "Resource.h"


// GLOBALS
HWND            hWnd;
HINSTANCE      hInst;
HWND            hProgress;
HWND            g_hwndStatusbar;
WNDPROC            wpOriginalProc;
HWND            hWndCheck;

#include "safe.h"                  // Include custom header file

//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                           LPSTR lpCmdLine, int nCmdShow)
{
      hInst = hInstance;

      DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),
                hWnd, reinterpret_cast<DLGPROC>(DlgProc));


      return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
      // Initialize the Common Controls
      INITCOMMONCONTROLSEX InitCtrlEx;
      
      InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
      InitCtrlEx.dwICC  = ICC_PROGRESS_CLASS;
      InitCommonControlsEx(&InitCtrlEx);

      switch(Msg)
      {         


      case WM_INITDIALOG:
               
               // THIS IS WHERE I AM DECLARING MY SUBCLASS
            hWndCheck = GetDlgItem(hWnd, IDC_CK_LEFT);

                wpOriginalProc = (WNDPROC)SetWindowLong(hWndCheck,
                                           GWL_WNDPROC, (LONG)CheckboxProc);

                        // Center Dialog
                        // NEED TO FINISH THIS
                        // CenterWindow(hWndDlg);

                        // Create the Progress Bar / Assign Handle
                        hProgress = CreateWindowEx(0, PROGRESS_CLASS, NULL,
                                          WS_CHILD | WS_VISIBLE,
                                          30, 120, 270, 17,
                                          hWndDlg, NULL, hInst, NULL);

                        // Min/Max Range
                        SendMessage(hProgress, PBM_SETRANGE, 0, MAKELPARAM(0, number_tasks));

                        // Init to Full
                        SendMessage(hProgress, PBM_SETPOS, number_tasks, 0);

                        // Create Status Bar
                        CreateStatusBar(hWndDlg);
                        


            return TRUE;

      case WM_COMMAND:

            switch(wParam)
            {
            case ID_OK:

                  // TO DO
                  return TRUE;

            case IDC_CANCEL:

                  

                  EndDialog(hWndDlg, 0);
                  return TRUE;
            }

      
      }      


      return FALSE;
}
//---------------------------------------------------------------------------






My Function is in safe.h

// Subclass procedure
LRESULT CALLBACK CheckboxProc(
     HWND hWnd,
     UINT Message,
     WPARAM wParam,
     LPARAM lParam)
{
     switch(Message)
     {
          case WM_LBUTTONDOWN:
               // This NEVER GETS CALLED
                    MessageBox(NULL, TEXT("WM_LBUTTONDOWN by IDC_CK_LEFT..."), TEXT("Subclass"),
                    MB_OK | MB_ICONINFORMATION);
                    
               break;
     }

     return CallWindowProc(wpOriginalProc, hWnd, Message, wParam, lParam);



}



And the messagebox is NEVER getting called. I don't know why.
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkr
Try

                wpOriginalProc = (WNDPROC)SetWindowLong(hWndCheck,
                                           GWL_WNDPROC, (LONG)CheckboxProc);

                if (!wpOriginalProc)
                {
                     char buf[255];
                     wsprintf(buf,"SetWindowLong failed, reason == %d",GetLastError());
                     MessageBox(NULL,buf,"Error",MB_OK);
                }
Avatar of edvinson

ASKER

:) worked