|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Runtime.InteropServices;
namespace CustomAddressDialog
{
/// <summary>
/// This class encapsulates all P/Invoke unmanaged functions.
/// </summary>
[SuppressUnmanagedCodeSecurity]
internal class WinApiProvider
{
/// <summary>
/// The <b>FindWindow</b> method finds a window by it's classname and caption.
/// </summary>
/// <param name="lpClassName">The classname of the window (use Spy++)</param>
/// <param name="lpWindowName">The Caption of the window.</param>
/// <returns>Returns a valid window handle or 0.</returns>
[DllImport("user32", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// Retrieves the WindowTest of the window given by the handle.
/// </summary>
/// <param name="hWnd">The windows handle</param>
/// <param name="lpString">A stringbuilder object wich receives the window text</param>
/// <param name="nMaxCount">The max length of the text to retrieve, usually 260</param>
/// <returns>Returns the length of chars received.</returns>
[DllImport("user32", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
/// <summary>
/// Returns a list of windowtext of the given list of window handles..
/// </summary>
/// <param name="windowHandles">A list of window handles.</param>
/// <returns>Returns a list with the corresponding window text for each window.</returns>
public static List<string> GetWindowNames(List<IntPtr> windowHandles)
{
List<string> windowNameList = new List<string>();
// A Stringbuilder will receive our windownames...
StringBuilder windowName = new StringBuilder(260);
foreach (IntPtr hWnd in windowHandles)
{
int textLen = GetWindowText(hWnd, windowName, 260);
// get the windowtext
windowNameList.Add(windowName.ToString());
}
return windowNameList;
}
/// <summary>
/// Returns a list of all child window handles for the given window handle.
/// </summary>
/// <param name="hParentWnd">Handle of the parent window.</param>
/// <returns>A list of all child window handles recursively.</returns>
public static List<IntPtr> EnumChildWindows(IntPtr hParentWnd)
{
// The list will hold all child handles.
List<IntPtr> childWindowHandles = new List<IntPtr>();
// We will allocate an unmanaged handle and pass a pointer to the EnumWindow method.
GCHandle hChilds = GCHandle.Alloc(childWindowHandles);
try
{
// Define the callback method.
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
// Call the unmanaged function to enum all child windows
EnumChildWindows(hParentWnd, childProc, GCHandle.ToIntPtr(hChilds));
}
finally
{
// Free unmanaged resources.
if (hChilds.IsAllocated)
hChilds.Free();
}
return childWindowHandles;
}
/// <summary>
/// A method to enummerate all childwindows of the given windows handle.
/// </summary>
/// <param name="hWnd">The parent window handle.</param>
/// <param name="callback">The callback method wich is called for each childwindow.</param>
/// <param name="userObject">A pointer to a userdefined object, e.g a list.</param>
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr hWnd, EnumWindowProc callback, IntPtr userObject);
/// <summary>
/// Callback method to be used when enumerating windows.
/// </summary>
/// <param name="hChildWindow">Handle of the next window</param>
/// <param name="pointer">Pointer to a GCHandle that holds a reference to the dictionary for our windowHandles.</param>
/// <returns>True to continue the enumeration, false to bail</returns>
private static bool EnumWindow(IntPtr hChildWindow, IntPtr pointer)
{
GCHandle hChilds = GCHandle.FromIntPtr(pointer);
((List<IntPtr>)hChilds.Target).Add(hChildWindow);
return true;
}
/// <summary>
/// Delegate for the EnumChildWindows method
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="parameter">Caller-defined variable</param>
/// <returns>True to continue enumerating, false to exit the search.</returns>
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
/// <summary>
/// Sends a message command to the give window address.
/// </summary>
/// <param name="hWnd">handle to destination window</param>
/// <param name="Msg">message</param>
/// <param name="wParam">first message parameter</param>
/// <param name="lParam">second message parameter</param>
/// <returns></returns>
[DllImport("user32")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
/// <summary>
/// Constant defines a System command message
/// </summary>
public const uint WM_SYSCOMMAND = 0x0112;
/// <summary>
/// Defines the Windows Close command
/// </summary>
public const int SC_CLOSE = 0xF060;
/// <summary>
/// Set a new parent for the given window handle
/// </summary>
/// <param name="hWndChild">The handle of the target window</param>
/// <param name="hWndNewParent">The window handle of the parent window</param>
[DllImport("user32")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
/// <summary>
/// Create a new window.
/// Description see http://msdn2.microsoft.com/en-us/library/ms632680.aspx
/// </summary>
/// <param name="dwExStyle">Specifies the extended window style of the window being created</param>
/// <param name="lpClassName">A class name - see http://msdn2.microsoft.com/en-us/library/ms633574.aspx</param>
/// <param name="lpWindowName">Pointer to a null-terminated string that specifies the window name</param>
/// <param name="dwStyle">Specifies the style of the window being created</param>
/// <param name="x">The window startposition X</param>
/// <param name="y">The window startposition Y</param>
/// <param name="nWidth">Width</param>
/// <param name="nHeight">Height</param>
/// <param name="hWndParent">Parent window handle</param>
/// <param name="hMenu">Handle to a menu</param>
/// <param name="hInstance">Handle to the instance of the module to be associated with the window</param>
/// <param name="lpParam">Pointer to a value to be passed to the window through the CREATESTRUCT structure </param>
/// <returns>If the function succeeds, the return value is a handle to the new window</returns>
[DllImport("user32.dll")]
public static extern IntPtr CreateWindowEx(
uint dwExStyle,
string lpClassName,
string lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam);
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm a 1968 model, made in Germany.
After School transformed into an electronic engineer and started repairing computers in 1986. From PET till now In the IT-world. Currently employed as softwaredeveloper in a company creating solutions for pharmacies.
MCSE2000, MCSD, MCTS - Team Foundation Server, MCTS - Visual Studio Tools for Office.
Specialized in Outlook/Exchange custom development. Languages: german, english, C#, C++, VB.Net, VB6, SQL.
In 2006 received an award as MVP - Visual Developer VSTO by Microsoft.
Homepage: [http://www.x4u.de]