• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1261
  • Last Modified:

How to create a Window (or other control) in another application's window

I would like to create a window or control inside another application's window, so that it moves around with the target window.  Preferrably, the window created would 'belong' to the target window and disappear when the application window or the application itself is closed.  I know that keep the contents of the window updated will be the responsibility of my program.  It would be VERY nice if the window I created could have events that communicate with my program as well!

The following creates a window, but at the top left of the screen - not in the target application window.  I have tried various combinations of the 1st and 4th parameters but this is the best I have been able to do so far.  The hwnd parameter is that of the target window.

        Dim retval As Integer = 0
        Dim btext As String = buttontext

        retval = CreateWindowEx(WS_EX_TOPMOST, "BUTTON", btext, WS_CHILD Or WS_VISIBLE, 5, 10, 50, 100, 0, 0, hwnd, 0&)

        SendMessage(retval, WM_PAINT, 0, 0)

I am using Visual Studio 2005, Visual Basic.  The target application is unmanaged code.
0
mluckham
Asked:
mluckham
  • 3
2 Solutions
 
DjDezmondCommented:
This may not be what your explicitly after, but i feel it may help you along the way...

You may already be using system hooks in this project, but if your not, i would suggest you look into it. It allows you intercept, and inject code between applications. This will allow cross application communication...

  http://www.codeproject.com/win32/AntonioWinLock.asp

  http://www.vbaccelerator.com/home/VB/Code/Libraries/Hooks/vbAccelerator_Hook_Library/article.asp

Just a few articles i have posted in other questions.

Hope these help
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Use the SetParent() API:

    Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
0
 
mluckhamAuthor Commented:
Thanks for the quick replies.

DjDezmond - I have used hooks in other apps using VB6, Delphi - but understand that .NET apps ("managed code") cannot be hooked due to permissions issues?  Pehaps I am incorrect - I hope so.  But in the current project, although the target app is not (for now) managed code, I have not used hooks yet and am hoping to avoid them.

IdleMind - I am trying your idea, it will take some time as I seem to not be able to create the window itself now ... I must have stumbled across a working combination before that was not preserved in the code :(
0
 
mluckhamAuthor Commented:
IdleMind - your solution worked - thanks a lot!

Here is the code snippet:

        Dim new_hwnd As Integer = 0

        ' This will create a button provided the hwnd is Window control type.  Examples of such types
        ' are Windows themselves, buttons, various types of lists.  The button initially appears on the desktop,
        ' not in hwnd's window.

        new_hwnd = CreateWindowEx(WS_EX_TOPMOST, "BUTTON", btext, BS_TEXT Or WS_VISIBLE, 5, 10, 50, 100, 0, 0, hwnd, 0&)

        ' An application can use the SetParent function to set the parent window of a pop-up,
        ' overlapped, or child window. The new parent window and the child window must belong
        ' to the same application.
        '
        ' If the window identified by the hWndChild parameter is visible, the system performs the
        ' appropriate redrawing and repainting.
        '
        '       SendMessage(new_hwnd, WM_PAINT, 0, 0)   ' so there is no need to do this
        '
        ' For compatibility reasons, SetParent does not modify the WS_CHILD or WS_POPUP window
        ' styles of the window whose parent is being changed. Therefore, if hWndNewParent is NULL, you
        ' should also clear the WS_CHILD bit and set the WS_POPUP style after calling SetParent. Conversely,
        ' if hWndNewParent is not NULL and the window was previously a child of the desktop, you should
        ' clear the WS_POPUP style and set the WS_CHILD style before calling SetParent.
        '
        '  (maybe have to set WS_CHILD?  It appears to work without it though)

        If (new_hwnd <> 0) Then

            ' this moves the new window into the parent window/control rendering space

            Dim prev_parent_hwnd As Integer = SetParent(new_hwnd, hwnd)

            '  Windows 2000/XP: When you change the parent of a window, you should synchronize the UISTATE
            ' of both windows. For more information, see WM_CHANGEUISTATE and WM_UPDATEUISTATE.

            SendMessage(hwnd, WM_CHANGEUISTATE, 0, 0&)
        End If

0
 
mluckhamAuthor Commented:
Although I've split the points, the bulk of them go to IdleMind because he directly answered my question.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now