Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CWnd::FromHandle (Temporary/Permanent for life of Dlg)

103 views
Skip to first unread message

Jeffrey Walton

unread,
Jun 18, 2010, 9:46:56 AM6/18/10
to
Hi All,

I have a MFC DLL that takes a HWND from applications. The Dialog's
wizard generated constructor is boilerplate. But I also needed one for
HWNDs

CMyDialog: public CDialog
{
// MFC callers
explicit CMyDialog(CWnd* pParent = NULL);
// Win32 callers
explicit CMyDialog(HWND hOwner = NULL);
};

CMyDialog::CMyDialog(HWND hOwner = NULL)
: CDialog(CMyDialog::IDD, (hOwner ? CWnd::FromHandle(hOwner) : NULL)
{
}

Unfortunately, the docs state the mapping is temporary [1]. How do I
get a mapping of an HWND -> CWnd for the lifetime of the dialog?

Thanks,
Jeff

[1] http://msdn.microsoft.com/en-us/library/e547yfza(VS.80).aspx

Seetharam

unread,
Jun 18, 2010, 10:19:39 AM6/18/10
to
use FromHandlePermanent().

-Seetharam

Jeffrey Walton

unread,
Jun 18, 2010, 12:07:03 PM6/18/10
to
Hi Seethharam,

Thank you very much.

Jeff

On Jun 18, 10:19 am, Seetharam <smi...@gmail.com> wrote:
> use FromHandlePermanent().
>
> -Seetharam

Joseph M. Newcomer

unread,
Jun 18, 2010, 2:18:20 PM6/18/10
to
Why does it take an HWND from the app and not a CWnd*?

Note the constructor below is pretty useless. The "owner" of a dialog is not the window
you pass in, but the main window of the app. Therefore, no matter what you put in the
constructor, the parent of the dialog will be the main window of the app. So get rid of
all that code entirely, it is doing nothing useful.

A bit of experimentation with Spy++ will show you that the code you wrote below has no
effect.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Goran

unread,
Jun 19, 2010, 6:35:09 AM6/19/10
to
On Jun 18, 3:46 pm, Jeffrey Walton <noloa...@gmail.com> wrote:
> Hi All,
>
> I have a MFC DLL that takes a HWND from applications. The Dialog's
> wizard generated constructor is boilerplate. But I also needed one for
> HWNDs
>
> CMyDialog: public CDialog
> {
>     //  MFC callers
>     explicit CMyDialog(CWnd* pParent = NULL);
>     // Win32 callers
>     explicit CMyDialog(HWND hOwner = NULL);
>
> };
>
> CMyDialog::CMyDialog(HWND hOwner = NULL)
> : CDialog(CMyDialog::IDD, (hOwner ? CWnd::FromHandle(hOwner) : NULL)
> {
>
> }
>
> Unfortunately, the docs state the mapping is temporary [1]. How do I
> get a mapping of an HWND -> CWnd for the lifetime of the dialog?

FromHandlePermanent(). However, I think that you should reconsider
that question. Why do you care about that CWnd? You can always get it
when you need it, just call FromHandle "locally".

On top of that, since it's a HWND, you don't gain much by getting a
CWnd, do you? What are you planning to do with it? Send/post it some
messages, most likely. If so,

CWnd* p = CWnd::FromHandle(...)
BOOL b = pWnd->SendMessage(...)

is more work than

BOOL b = ::SendMessage(hWnd, ...);

Goran.

P.S. By the way, you seem to be mixing "parent" (1st ctor) and
"owner" (2nd ctor) windows?

0 new messages