Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609260
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:57:42+00:00 2026-05-22T00:57:42+00:00

I got the two following CDialog-class created for creating dialog windows from templates and

  • 0

I got the two following CDialog-class created for creating dialog windows from templates and a class CMainDialog that is dereived from CDialog and has some methods do manipulate controls on the dialogs.

class CDialog
    {
    public:
        CDialog(DWORD dwTemplate) : m_dwTemplateID(dwTemplate), m_hWnd(NULL) {};
        virtual ~CDialog() {};

        static INT_PTR CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

        virtual BOOL Create(HINSTANCE hInstance, HWND hParent = NULL);

        BOOL Show(BOOL bShow);
    private:
        DWORD m_dwTemplateID;

    protected:
        HWND m_hWnd;
        virtual INT_PTR HandleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    };

//Base Dialog Implementation

#include "BaseDialog.h"

//statics

INT_PTR CALLBACK Inc::CDialog::DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    Inc::CDialog* pDialog = nullptr;

    if(uMsg == WM_INITDIALOG)
    {
        //save address of the CDialog-object into the dialog´s userdata
        pDialog = (Inc::CDialog*)lParam;
        SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pDialog);
    }
    else
    {
        //get the pointer
        pDialog = (Inc::CDialog*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
    }

    if(pDialog)
    {
        //handle messages
        return pDialog->HandleMessages(hWnd, uMsg, wParam, lParam);
    }

    return FALSE;       //!pDialog
}

INT_PTR Inc::CDialog::HandleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_CLOSE:
            DestroyWindow(m_hWnd);
        return TRUE;
    case WM_DESTROY:
            PostQuitMessage(0);
            return TRUE;
    };

    return FALSE;       //Message not handled => system will take action
}

BOOL Inc::CDialog::Create(HINSTANCE hInstance, HWND hParent)
{
    m_hWnd = CreateDialogParam(hInstance, MAKEINTRESOURCE(m_dwTemplateID), hParent, DialogProc, (LPARAM)this);
    if(m_hWnd == NULL)
        return FALSE;

    return TRUE;
}

//return values: TRUE => window was previously visible, FALSE otherwise
BOOL Inc::CDialog::Show(BOOL bShow)
{
    if(bShow)
        return ShowWindow(m_hWnd, SW_SHOWNORMAL);
    return ShowWindow(m_hWnd, SW_HIDE);
}

//CMainDialog

class CMainDialog :
        public Inc::CDialog
    {
    public:
        CMainDialog(DWORD dwTemplateID);
        virtual ~CMainDialog(void);

        virtual INT_PTR HandleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

    private:
        static const char m_szDates[8][14];

        //Dialog control item
        HWND m_hDateComboBox;
        HWND m_hItemListBox;
        HWND m_hDescriptionEditBox;
        HWND m_hButtonClose;
        HWND m_hButtonSave;
        HWND m_hButtonDelete;

        //get save the control item handles
    public:
        void GetControlHandles();
    public:
        void PopulateDateComboBox();
    };

//CMain Dialog Implementation

#include "MainDialog.h"
#include <WindowsX.h>
#include "resource.h"
#include <stdexcept>

//statics
const char Inc::CMainDialog::m_szDates[8][14] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Single Event"};

Inc::CMainDialog::CMainDialog(DWORD dwTemplateID) : CDialog(dwTemplateID)
{
}


Inc::CMainDialog::~CMainDialog(void)
{
}

//adds the entries in m_szDates to the Combo Box for choosing the date
void Inc::CMainDialog::PopulateDateComboBox()
{
    for(unsigned short s = 0; s < 8; s++)
    {
        if(ComboBox_AddString(m_hDateComboBox, m_szDates[s]) <= CB_ERR)
            throw(std::runtime_error("ComboBox_AddString() failed"));
    }
}

INT_PTR Inc::CMainDialog::HandleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
            this->GetControlHandles();    // It happens here !!!!!!! <====
            this->PopulateDateComboBox();
        return TRUE;
    }

    return Inc::CDialog::HandleMessages(hWnd, uMsg, wParam, lParam);        //if message isnt handled here, default handling
}

void Inc::CMainDialog::GetControlHandles()
{
    //Get Control window handles
    m_hDateComboBox = GetDlgItem(m_hWnd, IDC_COMBO_DAY);
    m_hItemListBox = GetDlgItem(m_hWnd, IDC_LIST);
    m_hDescriptionEditBox = GetDlgItem(m_hWnd, IDC_EDIT_DESCRIPTION);
    m_hButtonClose = GetDlgItem(m_hWnd, IDC_BUTTON_CLOSE);
    m_hButtonSave = GetDlgItem(m_hWnd, IDC_BUTTON_SAVE_CHANGE);
    m_hButtonDelete = GetDlgItem(m_hWnd, IDC_BUTTON_DELETE_ITEM);
}

The problem that I do encounter is, that in the CMainDialog::HandleMessages() member function when the WM_INITDIALOG should be handled the m_hWnd member is NULL even tho the CDialog::Create() function did succeed and return the window handle to m_hWnd.

The CDialog::DialogProc-procedure seems to work in the way, that it gets the right address from the WM_INITDIALOG´s LPARAM and let the pDialog-pointer point to the right object and call it´s member functions.

Maybe You see what I did miss or where I did wrong.

Thank You in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:57:42+00:00Added an answer on May 22, 2026 at 12:57 am

    your WM_INITDIALOG message is processed during the call to CreateDialog ,while the assignment to m_hWnd (the result of CreateDialog) is done after CreateDialog ends.

    Solution : pass hWnd to GetControlHandle( HWND hWnd ) or set m_hWnd in WM_INITDIALOG of the Base class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following two classes in C#: public class MyFirstClass : IMyFirstClass {
I've got the following two tables (in MySQL): Phone_book +----+------+--------------+ | id | name
I've got two SVN branches (eg development and stable) and want to switch from
Say I've got two scheduled processes: A and B. Given that B should not
I've got two possible string inputs that my application will receive, and if it
I've got the following two programs, one acting as a reader and the other
I'm having the following problem: When I got two labels into each other: <Label
I've got two applications running on two different machines that communicate by sending Serializable
I've got two web portals that are almost identical in architecture. One requires that
I've got two theoretical domain objects: public class Person { public virtual int Id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.