Solved

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

Posted on 2006-11-20
5
1,224 Views
Last Modified: 2010-08-05
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
Comment
Question by:mluckham
  • 3
5 Comments
 
LVL 9

Assisted Solution

by:DjDezmond
DjDezmond earned 20 total points
Comment Utility
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 480 total points
Comment Utility
Use the SetParent() API:

    Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
0
 
LVL 1

Author Comment

by:mluckham
Comment Utility
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
 
LVL 1

Author Comment

by:mluckham
Comment Utility
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
 
LVL 1

Author Comment

by:mluckham
Comment Utility
Although I've split the points, the bulk of them go to IdleMind because he directly answered my question.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now