Menu

Dialogbox class

2002-09-13
2012-09-26
  • Nobody/Anonymous

    Not exactly DevC specific, but...
    I would be happy to see a dialog class encapsulating the usual dialog stuff. I've seen some, but none of them could encapsulate that ugly callback function. I know it can be done with a static callback member passing a pointer to 'this', but cannot make it work properly as a base class to derive from.

    thanks
    rpeter

     
    • Nobody/Anonymous

      Nevermind, a new day, a better day. Started again from scratch and it works now.

      rpeter

       
    • Nobody/Anonymous

      post it please

       
    • Nobody/Anonymous

      cform.h:
      #ifndef CFORM_H
      #define CFORM_H
      #include <windows.h>

      class CForm
      {
      protected:
          HINSTANCE    hInst;
          int nDiaTemplate;
          virtual BOOL OnClose(HWND hwnd);
          virtual BOOL OnInit(HWND hwnd);
          virtual BOOL OnCommand(HWND hwnd, WPARAM wParam);
          LRESULT CALLBACK FormProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam);
          static LRESULT CALLBACK DlgProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam);
      public:
          int RunForm(HWND hParent, HINSTANCE hInstance);
      };

      #endif //CFORM_H

      cform.cpp:

      #include <windows.h>
      #include "cform.h"

      BOOL CForm::OnClose(HWND hwnd)
      {
          EndDialog(hwnd, 0);
          return TRUE;
      }

      BOOL CForm::OnInit(HWND hwnd)
      {
          return TRUE;
      }
      BOOL CForm::OnCommand(HWND hwnd, WPARAM wParam)
      {
          return TRUE;
      }

      LRESULT CALLBACK CForm::FormProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam)
      {
      switch (Message)
      {
          case WM_CLOSE:
              return OnClose(hwnd);
          case WM_COMMAND:
              return OnCommand(hwnd, wParam);
          case WM_INITDIALOG:
              return OnInit(hwnd);
          default:
              return FALSE;
      }
      }

      LRESULT CALLBACK CForm::DlgProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam)
      {
      CForm *instance;
      if (Message == WM_INITDIALOG)
          SetWindowLong(hwnd, GWL_USERDATA, lParam);
      instance = (CForm *)GetWindowLong(hwnd, GWL_USERDATA);
      if (instance != NULL)
          return instance->FormProc(hwnd,Message,wParam,lParam);
      }

      int CForm::RunForm(HWND hParent, HINSTANCE hInstance)
      {
      CForm * cInst;
      cInst = this;
      hInst=hInstance;
      return DialogBoxParam(hInst,
                MAKEINTRESOURCE(nDiaTemplate),
                hParent,
                (DLGPROC)DlgProc, (LPARAM)cInst);
                           
      }

      Usage: when deriving a new dialog box override the OnCommand member, also any member you need. Create a new constructor, which sets nDiaTemplate to the resource identifier. That's it.

      I didn't bother to handle every dialog box message as you can see.

       
    • Nobody/Anonymous

      Thanks.  I've been struggling with this for a bit.

       

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.