CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    How to get a pointer from a window handle?

    Is there any win32 API to do that? Thanks for your inputs.

  2. #2
    Join Date
    Dec 2003
    Location
    USA
    Posts
    4

    Re: How to get a pointer from a window handle?

    CWnd::FromHandle
    static CWnd* PASCAL FromHandle( HWND hWnd );

    Return Value

    Returns a pointer to a CWnd object when given a handle to a window. If a CWnd object is not attached to the handle, a temporary CWnd object is created and attached.

    The pointer may be temporary and should not be stored for later use.

    Parameters

    hWnd

    An HWND of a Windows window.
    Let me simply be your friend!

  3. #3
    Join Date
    Aug 2000
    Posts
    1,471

    Re: How to get a pointer from a window handle?

    Thanks for your reply. But I am using CWindow not CWnd. Any idea? Thanks.
    Quote Originally Posted by wonder88
    CWnd::FromHandle
    static CWnd* PASCAL FromHandle( HWND hWnd );

    Return Value

    Returns a pointer to a CWnd object when given a handle to a window. If a CWnd object is not attached to the handle, a temporary CWnd object is created and attached.

    The pointer may be temporary and should not be stored for later use.

    Parameters

    hWnd

    An HWND of a Windows window.

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: How to get a pointer from a window handle?

    try this
    Retrieves the current window handle.
    Code:
    HRESULT GetWindowHandle(
    HWND *pHwnd);
    Parameters

    pHwnd
    [out] Window handle.
    Return Values

    Returns a pointer to the window handle.

  5. #5
    Join Date
    Dec 2003
    Location
    USA
    Posts
    4

    Re: How to get a pointer from a window handle?

    CWindow::CWindow
    This constructor initializes the m_hWnd data member to hWnd, which by default is NULL.

    The CWindow::CWindow method does not create a window. Classes CWindowImpl, CContainedWindow, and CDialogImpl ,all of which derive from CWindow, provide a method to create a window or dialog box, which is then assigned to CWindow::m_hWnd. You can also use the CreateWindow Windows CE function.

    CWindow(
    HWND hWnd = NULL);
    Parameters
    hWnd
    [in] The handle to a window.


    CWindow::Attach
    This method attaches the window identified by hWndNew to the CWindow object.

    void Attach(
    HWND hWndNew );
    Parameters
    hWndNew
    [in] The handle to a window.
    Example
    // The following example attaches an HWND to the CWindow object.
    HWND hWndFoo = ::GetDesktopWindow();
    CWindow fooWindow;
    fooWindow.Attach(hWndFoo);
    Let me simply be your friend!

  6. #6
    Join Date
    Aug 2000
    Posts
    1,471

    Re: How to get a pointer from a window handle?

    But I am looking for a win32 API.
    Quote Originally Posted by humptydumpty
    try this
    Retrieves the current window handle.
    Code:
    HRESULT GetWindowHandle(
    HWND *pHwnd);
    Parameters

    pHwnd
    [out] Window handle.
    Return Values

    Returns a pointer to the window handle.

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

    Re: How to get a pointer from a window handle?

    Quote Originally Posted by dullboy
    But I am looking for a win32 API.
    The Windows API knows nothing about CWindow, CWnd, or any C++ classes used by MFC, ATL, whatever. The next question is "convert HWND to a pointer to what?"

    All the API knows is that a window is an HWND. An HWND is whatever it is. This is the type you deal with in an API program, and you don't assume it is a pointer, or a struct, or a class, etc. It is a "HANDLE", which means it represents something that Windows knows about internally that you need not know about.

    All you need to know is that HWND is used to represent a window, and you pass it back and forth between your app and the API functions.

    For what reason do you need to convert an HWND to a pointer (to something)?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 8th, 2005 at 10:50 PM.

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