Solved

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

Posted on 2006-11-20
5
1,243 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
No Data for DropDown List 2 32
Get distinct values from excel or comm seperated file 4 41
Get sourcecode path 14 47
vb.net deleting excel sheet in workbook 11 34
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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