///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