Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Simple Win32 Subclass Not Working

Posted on 2006-04-24
3
Medium Priority
?
911 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:edvinson
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 16525553
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
 
LVL 86

Expert Comment

by:jkr
ID: 16525707
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
 
LVL 1

Author Comment

by:edvinson
ID: 16525713
:) worked
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question