DCSIMG
Add Your Control On Top Another Application – Part 3 (Win32) - Shai Raiten's Blog

Shai Raiten's Blog

It's all about code...

Add Your Control On Top Another Application – Part 3 (Win32)

Add Your Control On Top Another Application – Part 3 (Win32)image

This is part 3 and we are almost done!

The first post in this series Add Your Control On Top Another Application – Part 1 (Win32) we saw how to find window handle just by pointing the process, this was done by using Win32 native methods.
In Add Your Control On Top Another Application – Part 2 (Win32)

We used another native method that helped us to find target window position based on TitleBar info.

This part will show how to use this information to add our own control on top another application (See picture).

Download Demo Project

Step 1: Create HoverControl

The HoverContorl is basically a control, I’ve created a new Window control called HoverControl with the following attributes:WindowStyle="None" AllowsTransparency="True" Background="{Binding Null}"

this is important because we want to window to be transparent and without window borders.
Below the full xaml of the HoverControl.

<Window x:Class="Win32HooksDemo.HoverControl"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="HoverControl" Height="10" Width="150"

        WindowStyle="None" AllowsTransparency="True" Background="Transparent">

  <Grid>

    <Rectangle ToolTip="Click To Close" MouseDown="rectangle1_MouseDown"  Name="rectangle1"

               Stroke="#FFC7FF00" Fill="Red" RadiusY="10" RadiusX="10" StrokeThickness="2"/>

    <TextBlock FontSize="9" FontWeight="Bold" HorizontalAlignment="Center"

               Text="This is my Hover Control" />

  </Grid>

</Window>

Mouse down event should close the HoverControls.

Step 2: Add SetWindowLong To NativeMethod Class.

The SetWindowLongPtr function changes an attribute of the specified window. The function also sets a value at the specified offset in the extra window memory.

To write code that is compatible with both 32-bit and 64-bit versions of Windows, also add SetWindowLongPtr

///The SetWindowLongPtr function changes an attribute of the specified window

[DllImport("user32.dll", EntryPoint = "SetWindowLong")]

internal static extern int SetWindowLong32(HandleRef hWnd, int nIndex, int dwNewLong);

 

[DllImport("user32.dll", EntryPoint = "SetWindowLong")]

internal static extern int SetWindowLong32(IntPtr windowHandle, Win32HooksDemo.Helpers.GWLParameter nIndex, int dwNewLong);

 

[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]

internal static extern IntPtr SetWindowLongPtr64(IntPtr windowHandle, Win32HooksDemo.Helpers.GWLParameter nIndex, IntPtr dwNewLong);

 

[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]

internal static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, IntPtr dwNewLong);

 

Step 3: Add SetWindowLong In Helpers Class

The only attribute we need to change in this case is - GWL_HWNDPARENT, our agenda is to set target window as parent\owner of our HoverControl.

//Specifies the zero-based offset to the value to be set.

//Valid values are in the range zero through the number of bytes of extra window memory, minus the size of an integer.

public enum GWLParameter

{

    GWL_EXSTYLE = -20, //Sets a new extended window style

    GWL_HINSTANCE = -6, //Sets a new application instance handle.

    GWL_HWNDPARENT = -8, //Set window handle as parent

    GWL_ID = -12, //Sets a new identifier of the window.

    GWL_STYLE = -16, // Set new window style

    GWL_USERDATA = -21, //Sets the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero.

    GWL_WNDPROC = -4 //Sets a new address for the window procedure.

}

 

Instead of checking if this 64bit or 32bit from our UI class add SetWindowLong inside the helpers class.

 

public static int SetWindowLong(IntPtr windowHandle, GWLParameter nIndex, int dwNewLong)

{

    if (IntPtr.Size == 8) //Check if this window is 64bit

    {

        return (int)NativeMethods.SetWindowLongPtr64(windowHandle, nIndex, new IntPtr(dwNewLong));

    }

    return NativeMethods.SetWindowLong32(windowHandle, nIndex, dwNewLong);

}

 

Step 4: Add HoverControl On Top Target Application

First we need to create new instance of our new HoverContorl, set the position based on Target window TitleBar position (Part 2) and set the HoverControl as Child of Target window.

Add button click event and let’s add the following code:

if (OnTopControl != null)

    OnTopControl.Close();

//Creates new instance of HoverControl

HoverControl OnTopControl = new HoverControl();

OnTopControl.Show();

//Search for HoverControl handle

IntPtr OnTopHandle = Helpers.Find(OnTopControl.Name, OnTopControl.Title);

 

//Set the new location of the control (on top the titlebar)

OnTopControl.Left = left;

OnTopControl.Top = top;

 

//Change target window to be parent of HoverControl.

Helpers.SetWindowLong(OnTopHandle, Helpers.GWLParameter.GWL_HWNDPARENT, TargetWnd.ToInt32());

 

Log("Hover Control Added!");

 

Download Demo Project

image

Enjoy

Comments

Twitter Trackbacks for Add Your Control On Top Another Application ??? Part 3 (Win32): Add Your Control On Top Another Application ??? Part 3... [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Add Your Control On Top Another Application ??? Part 3 (Win32): Add Your Control On Top Another Application ??? Part 3...         [microsoft.co.il]        on Topsy.com

# May 10, 2010 7:03 PM

Yanush said:

Simply an excellent article!

# May 11, 2010 5:01 AM

shair said:

Thank you!

# May 12, 2010 3:25 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: