CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2012
    Posts
    15

    How to encapsulate a winapi Dialog in a class?

    I have made a simple Pane class so that I can encapsulate a winapi Dialog.
    I call it as:

    Here the code I have, but it displays no dialog.

    Here is what I do in main:
    Code:
    			Pane(&hDlg, hInstance, nCmdShow);
    
    			
    			while((ret = GetMessage(&msg, 0, 0, 0)) != 0)
    			{
    				if(ret == -1)
    					return -1;
    
    				if(!IsDialogMessage(hDlg, &msg)) 
    				{
    					TranslateMessage(&msg);
    					DispatchMessage(&msg);
    				}
    		 
    			}//while

    Pane.h
    Code:
    #pragma once
    
    #include "stdafx.h"
    #include "resource.h"
    
    #pragma comment(linker, \
      "\"/manifestdependency:type='Win32' "\
      "name='Microsoft.Windows.Common-Controls' "\
      "version='6.0.0.0' "\
      "processorArchitecture='*' "\
      "publicKeyToken='6595b64144ccf1df' "\
      "language='*'\"")
    
    #pragma comment(lib, "ComCtl32.lib")
    
    class Pane
    {
    public:
    
    	static HWND m_hDlg;
    	MSG msg;
    	HINSTANCE hInstance;
    	int nCmdShow;
    
    	Pane(HWND *hDlg, HINSTANCE hINstance, int ncmdshow);
    	~Pane(void);
    
    	static void onRestartNow();
    	static void onRestartLater();
    	static void onClose();
    
    	static BOOL CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
    };
    Pane.cpp
    Code:
    #include "StdAfx.h"
    #include "Pane.h"
    
    HWND Pane::m_hDlg = 0;
    
    Pane::Pane(HWND *hDlg, HINSTANCE hINstance, int nCmdShow)
    {
    	InitCommonControls();
    	m_hDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0, DialogProc, 0);
    	*hDlg = m_hDlg;
    
    	HICON hIcon = LoadIcon(NULL, IDI_EXCLAMATION);
    	HWND hImageCtl = GetDlgItem(*hDlg, IDC_STATIC_ICON);
    	::SendMessage(hImageCtl, STM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon); 
    	ShowWindow(m_hDlg, nCmdShow);
    }
    
    
    void Pane::onRestartNow()
    {
    	SendMessage(m_hDlg, WM_CLOSE, 0, 0);
    }
    
    void Pane::onRestartLater()
    {
    	SendMessage(m_hDlg, WM_CLOSE, 0, 0);
    }
    
    void Pane::onClose()
    {
        DestroyWindow(m_hDlg);
    }
    
    
    INT_PTR CALLBACK Pane::DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
      switch(uMsg) /* more compact, each "case" through a single line, easier on the eyes */
      {
      case WM_COMMAND:
    	switch(LOWORD(wParam))
        {
    		case ID_RESTART_NOW: onRestartNow(); return TRUE;
    		case ID_RESTART_LATER: onRestartLater(); return TRUE;
        }
        break;
    
      case WM_CLOSE:   onClose(); return TRUE;
      case WM_DESTROY: PostQuitMessage(0); return TRUE;
      }
    
      return FALSE;
    }
    
    Pane::~Pane(void)
    {
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to encapsulate a winapi Dialog in a class?

    Quote Originally Posted by krs0 View Post
    I have made a simple Pane class so that I can encapsulate a winapi Dialog.
    1) Tell me what this line does:
    Code:
    Pane(&hDlg, hInstance, nCmdShow);
    Whatever you believe it's doing, it isn't doing it, and probably the reason you're not seeing a dialog.

    2) Nowhere do you check your return codes for your API calls. What if CreateDialogParam returns an error? You now have a useless object being potentially used in an application.

    3) If you forget to call onClose(), your Pane destructor is not coded to clean up the resources. Look up the term "RAII". As a matter of fact, what happens if that Pane object goes out of scope without an onClose() call? You now have a resource leak.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 28th, 2013 at 05:39 AM.

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to encapsulate a winapi Dialog in a class?

    Did you debug your code? What does CreateDialogParam return? If the return value is NULL then call GetLastError to get extended error information.
    Victor Nijegorodov

  4. #4
    Join Date
    Oct 2012
    Posts
    15

    Re: How to encapsulate a winapi Dialog in a class?

    1. Pane(&hDlg, hInstance, nCmdShow);
    This is how I want to create and display the dialog. I put the handler in hDlg so that I can use it later in IsDialogMessage(hDlg, &msg).

    2. Can you post a short example of check on one of the calls please?

    3. I will add onClose() to the destructor is that enough?

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to encapsulate a winapi Dialog in a class?

    Quote Originally Posted by krs0 View Post
    I have made a simple Pane class so that I can encapsulate a winapi Dialog.
    To add, you really haven't encapsulated anything. You are using global (static) variables in the Pane class. All you did was rearrange where you're declaring your global variables.

    If it were truly encapsulated, there would be no globals (except maybe the dialog procedure function). It takes much more thought and design to really encapsulate a dialog window than just moving your globals to a class.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to encapsulate a winapi Dialog in a class?

    Quote Originally Posted by krs0 View Post
    1. Pane(&hDlg, hInstance, nCmdShow);
    This is how I want to create and display the dialog. I put the handler in hDlg so that I can use it later in IsDialogMessage(hDlg, &msg).
    That line of code creates a temporary and that temporary is immediately destroyed. That's why I asked you what it did, and obviously you weren't aware of what it was doing.

    If you want proof, put a breakpoint in the Pane destructor. When that line of code is executed, you will see the destructor magically get called. That object that was created is gone after that line is executed.
    2. Can you post a short example of check on one of the calls please?
    All of those functions return a value. Are you reading the documentation for those functions you're calling?

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Do you see the Return value section?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 28th, 2013 at 05:52 AM.

  7. #7
    Join Date
    Oct 2012
    Posts
    15

    Re: How to encapsulate a winapi Dialog in a class?

    2. Parts of code I have from net examples and I was trying to quickly make them work. I see now what you mean with error checks.

  8. #8
    Join Date
    Jan 2013
    Posts
    1

    Re: How to encapsulate a winapi Dialog in a class?

    Hi,

    You have to debug the code and need to check what is the value returned by CreateDialogParam.You can get the error info by using GetLastError. During debug if found any then mention it in the post.
    Please refer http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured