Solved

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

Posted on 2006-11-20
5
1,244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 9

Assisted Solution

by:DjDezmond
DjDezmond earned 20 total points
ID: 17980004
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 86

Accepted Solution

by:
Mike Tomlinson earned 480 total points
ID: 17980163
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
ID: 17981047
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
ID: 17981412
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
ID: 17981428
Although I've split the points, the bulk of them go to IdleMind because he directly answered my question.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

687 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