?
Solved

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

Posted on 2006-11-20
5
Medium Priority
?
1,247 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 80 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 1920 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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