Advertisement
Guest User

Dialog wrapper class attempt

a guest
Sep 25th, 2012
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. /* ------- Dialog.h ------- */
  2. #pragma once
  3.  
  4. #ifndef DIALOG_H_
  5. #define DIALOG_H_
  6.  
  7. #define WIN32_LEAN_AND_MEAN
  8. #include <Windows.h>
  9. #include <map>
  10. using namespace std;
  11.  
  12. // All dialogs share this same procedure. This method actually
  13. // uses 'hWnd' to search for the correct member procedure and returns it.
  14. class Dialog;
  15. extern map<HWND,Dialog*> g_DlgProcs;
  16. INT_PTR CALLBACK g_MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  17.  
  18. class Dialog
  19. {
  20. private:
  21.     int _id;
  22.     HWND _handle, _parent;
  23.  
  24.     // Make non-copyable
  25.     Dialog(const Dialog& other);
  26.     Dialog& operator= (const Dialog&);
  27.  
  28.     // Dialog procedure
  29.     INT_PTR CALLBACK cb_procedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  30. protected:
  31.     Dialog(int id, HWND parent = HWND_DESKTOP);
  32. public:
  33.     INT_PTR CALLBACK cb_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  34.  
  35.     // Properties
  36.     int ID(void);
  37.     HWND Handle(void);
  38.     HWND Parent(void);
  39.  
  40.     // Methods
  41.     LRESULT Send(UINT msg, WPARAM wParam = 0, LPARAM lParam = 0);
  42.     void Show(void);
  43.     void Show(int nCmdShow);
  44.     void Hide(void);
  45.     void Destroy(void);
  46.  
  47.     // Events
  48.     virtual bool ProcessMsg(UINT msg, WPARAM wParam, LPARAM lParam);
  49.     virtual void OnClose(void);
  50.     virtual void OnDestroy(void);
  51.     virtual void OnInitialize(void);
  52.     virtual void OnMenuCommand(bool isAccel, int id);
  53.     virtual void OnCtrlCommand(int code, int id, HWND from);
  54.     virtual void OnNotification(int code, int id, HWND from);
  55. };
  56.  
  57. #endif // DIALOG_H_
  58.  
  59. /* ------- Dialog.cpp -------- */
  60. #include "Dialog.h"
  61. #include "Macros.h" // Just for Alert(string msg) messagebox macro
  62.  
  63. map<HWND,Dialog*> g_DlgProcs;
  64.  
  65. INT_PTR CALLBACK g_MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  66.     if (g_DlgProcs.find(hWnd) != g_DlgProcs.end()) {
  67.         Alert("blah"); // Gets executed repeatedly
  68.         return g_DlgProcs[hWnd]->cb_proc(hWnd, msg, wParam, lParam);
  69.     } else {
  70.         Alert("blah"); // Removing the above alert, this gets
  71.                        // executed repeatedly, erm, as well.. O.o strange
  72.         return FALSE;
  73.     }
  74. }
  75.  
  76. Dialog::Dialog(int id, HWND parent /* = HWND_DESKTOP */) {
  77.     _id = id;
  78.     _parent = parent;
  79.  
  80.     // Tried this before CreateDialogParam
  81.     g_DlgProcs.insert(make_pair(_handle, this));
  82.  
  83.     _handle = CreateDialogParam(
  84.         (HINSTANCE)GetModuleHandle(NULL),
  85.         MAKEINTRESOURCE(id), _parent,
  86.         (DLGPROC)g_MainDlgProc, NULL
  87.     );
  88.  
  89.     // Then tried it after CreateDialogParam
  90.     g_DlgProcs.insert(make_pair(_handle, this));
  91. }
  92.  
  93. INT_PTR CALLBACK Dialog::cb_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  94.     Alert("blah"); // Never gets executed
  95.  
  96.     bool handled = true;
  97.  
  98.     switch (msg)
  99.     {
  100.     case WM_INITDIALOG:
  101.         OnInitialize();
  102.         break;
  103.     case WM_COMMAND:
  104.         if (HIWORD(wParam) == 0 || HIWORD(wParam) == 1) {
  105.             OnMenuCommand((HIWORD(wParam) == 1), (int)LOWORD(wParam));
  106.         } else {
  107.             OnCtrlCommand((int)HIWORD(wParam), (int)LOWORD(wParam), (HWND)lParam);
  108.         }
  109.         break;
  110.     case WM_NOTIFY:
  111.         {
  112.             LPNMHDR head = (LPNMHDR)lParam;
  113.             OnNotification(head->code, head->idFrom, head->hwndFrom);
  114.         }
  115.         break;
  116.     case WM_CLOSE:
  117.         OnClose(); // DestroyWindow(_handle)
  118.         break;
  119.     case WM_DESTROY:
  120.         OnDestroy(); // PostQuitMessage(0)
  121.     default:
  122.         handled = ProcessMsg(msg, wParam, lParam);
  123.     }
  124.  
  125.     // Convert bool to Windows BOOL enum
  126.     return ((handled == true) ? TRUE : FALSE);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement