15.2 Get the Handle for a Control, a Window, or a File


Problem

You need to call an unmanaged function that requires the handle for a control, a window, or a file.

Solution

Many classes, including all Control -derived classes and the FileStream class, return the handle as an IntPtr through a property named Handle . Other classes also provide similar information; for example, the System.Diagnostics.Process class provides a Process.MainWindowHandle property in addition to the Handle property.

Discussion

The .NET Framework doesn't hide underlying details such as the operating system handles used for controls and windows . Although you usually won't use this information, you can retrieve it if you need to call an unmanaged function that requires it. Many Microsoft Windows API functions, for example, require control or window handles.

As an example, consider the Windows-based application shown in Figure 15.1. It consists of a single window that always stays on top of all other windows regardless of focus. (This behavior is enforced by setting the Form.TopMost property to true .) The form also includes a timer that periodically calls the unmanaged GetForegroundWindow and GetWindowText WinAPI functions to determine which window currently has the focus.

There's one additional detail in this example. The code also uses the Form.Handle property to get the handle of the main application form. It then compares with the handle of the active form to test if the current application has focus. Here's the complete form code:

 using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Text; public class ActiveWindowInfo : System.Windows.Forms.Form {     // (Designer code omitted.)     private System.Windows.Forms.Timer tmrRefresh;     private System.Windows.Forms.Label lblCurrent;     private System.Windows.Forms.Label lblHandle;     private System.Windows.Forms.Label lblCaption;     [DllImport("user32.dll")]     private static extern int GetForegroundWindow();     [DllImport("user32.dll")]     private static extern int GetWindowText(int hWnd, StringBuilder text,        int count);     private void tmrRefresh_Tick(object sender, System.EventArgs e) {         int chars = 256;         StringBuilder buff = new StringBuilder(chars);         int handle = GetForegroundWindow();         if (GetWindowText(handle, buff, chars) > 0) {             lblCaption.Text = buff.ToString();             lblHandle.Text = handle.ToString();             if (new IntPtr(handle) == this.Handle) {                 lblCurrent.Text = "True";             } else {                 lblCurrent.Text = "False";             }         }     } } 
Tip  

The Windows Forms infrastructure manages handles transparently . Changing some form properties can force the CLR to generate a new handle. For that reason, you should always retrieve the handle before you use it (rather than storing it in a member variable for a long period of time).




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net