[Mwinapi-commits] SF.net SVN: mwinapi:[83] trunk/ManagedWinapi
Status: Beta
Brought to you by:
schierlm
From: <sch...@us...> - 2009-06-26 14:05:09
|
Revision: 83 http://mwinapi.svn.sourceforge.net/mwinapi/?rev=83&view=rev Author: schierlm Date: 2009-06-26 13:30:47 +0000 (Fri, 26 Jun 2009) Log Message: ----------- - Add WinForms-like MouseDown/Up/Move/Wheel events to LowLevelMouseHook - Add DomainName and DnsDomainName properties to MachineIdentifiers [submitted by Duncan Smart <duncan dot smart at gmail dot com>] Modified Paths: -------------- trunk/ManagedWinapi/LowLevelHook.cs trunk/ManagedWinapi/MachineIdentifiers.cs Modified: trunk/ManagedWinapi/LowLevelHook.cs =================================================================== --- trunk/ManagedWinapi/LowLevelHook.cs 2009-04-13 15:35:03 UTC (rev 82) +++ trunk/ManagedWinapi/LowLevelHook.cs 2009-06-26 13:30:47 UTC (rev 83) @@ -177,6 +177,18 @@ /// </summary> public event LowLevelMessageCallback MessageIntercepted; + /// <summary>Occurs when the mouse pointer is moved.</summary> + public event EventHandler<MouseEventArgs> MouseMove; + + /// <summary>Occurs when a mouse button is pressed.</summary> + public event EventHandler<MouseEventArgs> MouseDown; + + /// <summary>Occurs when a mouse button is released.</summary> + public event EventHandler<MouseEventArgs> MouseUp; + + /// <summary>Occurs when the mouse wheel moves.</summary> + public event EventHandler<MouseEventArgs> MouseWheel; + /// <summary> /// Represents a method that handles an intercepted mouse action. /// </summary> @@ -206,6 +218,9 @@ if (code == HC_ACTION) { MSLLHOOKSTRUCT llh = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); + + fireMouseEvents(wParam, llh.pt, llh.mouseData); + bool handled = false; if (MouseIntercepted != null) { @@ -224,6 +239,76 @@ return 0; } + private void fireMouseEvents(IntPtr wParam, POINT pt, int mouseData) + { + const int WM_MOUSEMOVE = 0x200, + WM_LBUTTONDOWN = 0x201, WM_LBUTTONUP = 0x202, + WM_RBUTTONDOWN = 0x204, WM_RBUTTONUP = 0x205, + WM_MBUTTONDOWN = 0x207, WM_MBUTTONUP = 0x208, + WM_MOUSEWHEEL = 0x20A, + WM_XBUTTONDOWN = 0x020B, WM_XBUTTONUP = 0x020C; + + switch ((int)wParam) + { + case WM_MOUSEMOVE: + if (MouseMove != null) + MouseMove(this, new MouseEventArgs(MouseButtons.None, 0, pt.X, pt.Y, 0)); + break; + + case WM_LBUTTONDOWN: + if (MouseDown != null) + MouseDown(this, new MouseEventArgs(MouseButtons.Left, 1, pt.X, pt.Y, 0)); + break; + case WM_LBUTTONUP: + if (MouseUp != null) + MouseUp(this, new MouseEventArgs(MouseButtons.Left, 1, pt.X, pt.Y, 0)); + break; + + case WM_RBUTTONDOWN: + if (MouseDown != null) + MouseDown(this, new MouseEventArgs(MouseButtons.Right, 1, pt.X, pt.Y, 0)); + break; + case WM_RBUTTONUP: + if (MouseUp != null) + MouseUp(this, new MouseEventArgs(MouseButtons.Right, 1, pt.X, pt.Y, 0)); + break; + + case WM_MBUTTONDOWN: + if (MouseDown != null) + MouseDown(this, new MouseEventArgs(MouseButtons.Middle, 1, pt.X, pt.Y, 0)); + break; + case WM_MBUTTONUP: + if (MouseUp != null) + MouseUp(this, new MouseEventArgs(MouseButtons.Middle, 1, pt.X, pt.Y, 0)); + break; + + case WM_MOUSEWHEEL: + if (MouseWheel != null) + MouseWheel(this, new MouseEventArgs(MouseButtons.None, 0, pt.X, pt.Y, hiWord(mouseData))); + break; + + case WM_XBUTTONDOWN: + if (MouseDown != null) + { + MouseButtons xbutton = hiWord(mouseData) == 1 ? MouseButtons.XButton1 : MouseButtons.XButton2; + MouseDown(this, new MouseEventArgs(xbutton, 1, pt.X, pt.Y, 0)); + } + break; + case WM_XBUTTONUP: + if (MouseUp != null) + { + MouseButtons xbutton = hiWord(mouseData) == 1 ? MouseButtons.XButton1 : MouseButtons.XButton2; + MouseUp(this, new MouseEventArgs(xbutton, 1, pt.X, pt.Y, 0)); + } + break; + } + } + + private static short hiWord(int n) + { + return (short)((n >> 0x10) & 0xFFFF); + } + #region PInvoke Declarations [StructLayout(LayoutKind.Sequential)] Modified: trunk/ManagedWinapi/MachineIdentifiers.cs =================================================================== --- trunk/ManagedWinapi/MachineIdentifiers.cs 2009-04-13 15:35:03 UTC (rev 82) +++ trunk/ManagedWinapi/MachineIdentifiers.cs 2009-06-26 13:30:47 UTC (rev 83) @@ -25,6 +25,7 @@ using System.Net.NetworkInformation; using System.IO; using System.Net; +using System.ComponentModel; namespace ManagedWinapi { @@ -64,6 +65,24 @@ [DllImport("advapi32.dll", SetLastError = true, PreserveSig = false)] private static extern void LsaClose(IntPtr ObjectHandle); + [DllImport("netapi32.dll", CharSet = CharSet.Auto)] + private static extern int NetWkstaGetInfo(string server, + int level, + out WKSTA_INFO_100 info); + + [DllImport("netapi32.dll")] + private static extern int NetApiBufferFree(WKSTA_INFO_100 info); + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] + private class WKSTA_INFO_100 + { + public int wki100_platform_id; + public string wki100_computername; + public string wki100_langroup; + public int wki100_ver_major; + public int wki100_ver_minor; + } + /// <summary> /// The security identifier of this machine. This id is generated at /// installation time (or when running tools like SysPrep or NewSid) @@ -90,6 +109,37 @@ } /// <summary> + /// The NetBIOS name of the domain to which this machine is joined, or the LAN workgroup name if not. + /// For the Active Directory DNS domain name, see <see cref="DnsDomainName"/>. + /// </summary> + public static string DomainName + { + get + { + WKSTA_INFO_100 wkstaInfo; + int result = NetWkstaGetInfo(null, 100, out wkstaInfo); + if (result != 0) + throw new Win32Exception(result); + string domainName = wkstaInfo.wki100_langroup; + NetApiBufferFree(wkstaInfo); + return domainName; + } + } + + /// <summary> + /// The Active Directory DNS domain of which this machine is a member. + /// For the NetBIOS domain name see <see cref="DomainName"/>. + /// </summary> + public static string DnsDomainName + { + get + { + IPGlobalProperties ipGlobalProps = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); + return ipGlobalProps.DomainName; + } + } + + /// <summary> /// The DNS host name of this machine. Can be easily changed. /// </summary> public static string HostName This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |