Search code examples
c++winapiatlwtl

Which HWND would I use to create a dialog in my WinMain function?


I'm currently trying to exercise C++ with WTL, and I am now facing this problem. So far I've worked with WTL quite a bit, but I always had a template which had the most basic window creation implemented already. However, I now want to do it myself, as I can't get to the template right now.

Following:

This is my WinMain:

#include "stdafx.h"
#pragma once
#include "MusicPlayerDialog.h"
#include "resource.h"

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev,
    LPSTR szCmdLine, int nCmdShow)
{
    UNREFERENCED_PARAMETER(szCmdLine);
    UNREFERENCED_PARAMETER(hInstPrev);

    _Module.Init(NULL, hInst);

    MusicPlayerDialog myDialog;
    MSG msg;

    myDialog.Create( **//PROBLEM//** )

    myDialog.ShowWindow(nCmdShow);
    myDialog.UpdateWindow();

    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

And here is my dialog:

#pragma once
#include "stdafx.h"
#include "resource.h"

class MusicPlayerDialog : public CDialogImpl<MusicPlayerDialog> 
{
public:

    enum { IDD = IDD_MAINDIALOG };

    BEGIN_MSG_MAP(MusicPlayerDialog)
        MESSAGE_HANDLER(WM_CLOSE, OnClose)
        MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
        MESSAGE_HANDLER(WM_INITDIALOG, OnInit)
    END_MSG_MAP()

    LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnInit(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

private:

};

I really would like to do it with a dialog, considering it is easier for starters like me to create a GUI with the dialog designer. I am pretty sure using raw windows is better, but I just want to learn the communications beetween windows, dialogs etc. right now.

My problem right now is, that I don't know what to pass to myDialog.Create()

In the first place, the problem is the HWND i would have to pass to it. I read up on MSDN, and as it seems, the parameter is representing the Dialogs "Parent" Window.

However, I didn't create any other windows yet, so there is nothing to pass to it.

Do I have to get a handle to, I don't know, my... desktop window? Something like that? I really can't figure it out.

I know that there is a way to grab the parent window of a given window/dialog, but I don't think that would work here, considering there is nothing to grab.


Solution

  • If the 2nd parameter to CDialog::Create (pParentWnd) is NULL, then the dialog object's parent window is set to the main application window. An other option is to use CWnd* AFXAPI AfxGetMainWnd( ); what returns the application's main window if it is called from the application's primary thread.

    See also CDialog::Create and AfxGetMainWnd