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.
LVL 1
edvinsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
hWndCheck = GetDlgItem(hWnd, IDC_CK_LEFT);

What is hWnd? Replace with hWndDlg.
hWnd is unnecessary variable, it remains garbage, it is passed as [in] parameter to DialogBox function, this parameter must be NULL for main dialog.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
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);
                }
0
edvinsonAuthor Commented:
:) worked
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.