//######################################################################################### //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★ //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★ //######################################################################################### /* * Copyright ?2004, Daniel Turini * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. */ using System; using System.Runtime.InteropServices; namespace XPTable.Win32 { /// /// Summary description for NativeWindow /// internal class NativeWindow { #region Class Data /// /// /// private IntPtr handle; /// /// Prevents the delegate being collected /// private WndProcDelegate wndProcDelegate; /// /// /// private IntPtr oldWndFunc; /// /// /// private delegate IntPtr WndProcDelegate(IntPtr hwnd, int Msg, IntPtr wParam, IntPtr lParam); /// /// /// private const int GWL_WNDPROC = -4; #endregion #region Constructor /// /// Initializes a new instance of the NativeWindow class /// public NativeWindow() { wndProcDelegate = new WndProcDelegate(this.WndProc); } #endregion #region Methods /// /// Assigns a handle to this window /// /// The handle to assign to this window public void AssignHandle(IntPtr hWnd) { handle = hWnd; oldWndFunc = SetWindowLong(hWnd, GWL_WNDPROC, wndProcDelegate); } /// /// Releases the handle associated with this window /// public void ReleaseHandle() { SetWindowLong(handle, GWL_WNDPROC, oldWndFunc); handle = IntPtr.Zero; oldWndFunc = IntPtr.Zero; } /// /// Invokes the default window procedure associated with this window /// /// A Message that is associated with the current Windows message protected virtual void WndProc(ref System.Windows.Forms.Message msg) { DefWndProc(ref msg); } /// /// Invokes the default window procedure associated with this window. /// It is an error to call this method when the Handle property is 0 /// /// A Message that is associated with the current Windows message public void DefWndProc(ref System.Windows.Forms.Message m) { m.Result = CallWindowProc(oldWndFunc, m.HWnd, m.Msg, m.WParam, m.LParam); } /// /// Handler for the WndProcDelegate /// /// Handle to the window procedure to receive the message /// Specifies the message /// Specifies additional message-specific information. The contents /// of this parameter depend on the value of the Msg parameter /// Specifies additional message-specific information. The contents /// of this parameter depend on the value of the Msg parameter /// The return value specifies the result of the message processing and depends /// on the message sent private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam) { System.Windows.Forms.Message m = System.Windows.Forms.Message.Create(hWnd, msg, wParam, lParam); WndProc(ref m); return m.Result; } /// /// The SetWindowLong function changes an attribute of the specified window. The /// function also sets the 32-bit (long) value at the specified offset into the /// extra window memory /// /// Handle to the window and, indirectly, the class to which /// the window belongs /// Specifies the zero-based offset to the value to be set. /// Specifies the replacement value /// If the function succeeds, the return value is the previous value of /// the specified 32-bit integer. If the function fails, the return value is zero [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)] private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, WndProcDelegate wndProcDelegate); /// /// The SetWindowLong function changes an attribute of the specified window. The /// function also sets the 32-bit (long) value at the specified offset into the /// extra window memory /// /// Handle to the window and, indirectly, the class to which /// the window belongs /// Specifies the zero-based offset to the value to be set. /// Specifies the replacement value /// If the function succeeds, the return value is the previous value of /// the specified 32-bit integer. If the function fails, the return value is zero [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)] private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr wndFunc); /// /// The CallWindowProc function passes message information to the specified window /// procedure /// /// Pointer to the previous window procedure. If this value /// is obtained by calling the GetWindowLong function with the nIndex parameter set to /// GWL_WNDPROC or DWL_DLGPROC, it is actually either the address of a window or dialog /// box procedure, or a special internal value meaningful only to CallWindowProc /// Handle to the window procedure to receive the message /// Specifies the message /// Specifies additional message-specific information. The contents /// of this parameter depend on the value of the Msg parameter /// Specifies additional message-specific information. The contents /// of this parameter depend on the value of the Msg parameter /// The return value specifies the result of the message processing and depends /// on the message sent [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)] private static extern IntPtr CallWindowProc(IntPtr prevWndFunc, IntPtr hWnd, int iMsg, IntPtr wParam, IntPtr lParam); #endregion #region Properties /// /// Gets the handle for this window /// public IntPtr Handle { get { return this.handle; } } #endregion } }