namespace ClassLibrary.Hardware { // 原创 Using C# and Win32API ( 最近我把所有的Win32API看了1遍 很是过瘾 )
public class Mouse { internal const byte SM_MOUSEPRESENT = 19; internal const byte SM_CMOUSEBUTTONS = 43; internal const byte SM_MOUSEWHEELPRESENT = 75;
internal struct POINTAPI { internal int x; internal int y; }
internal struct RECT { internal int left ; internal int top ; internal int right ; internal int bottom ; }
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SwapMouseButton")] internal extern static int SwapMouseButton ( int bSwap );
[System.Runtime.InteropServices.DllImport("user32" , EntryPoint="ClipCursor")] internal extern static int ClipCursor(ref RECT lpRect);
[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )] internal extern static int GetCursorPos( ref POINTAPI lpPoint );
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="ShowCursor")] internal extern static bool ShowCursor ( bool bShow ) ;
[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint = "EnableWindow" )] internal extern static int EnableWindow( int hwnd , int fEnable );
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetWindowRect")] internal extern static int GetWindowRect( int hwnd , ref RECT lpRect ) ;
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetCursorPos")] internal extern static int SetCursorPos ( int x , int y ) ;
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetSystemMetrics")] internal extern static int GetSystemMetrics( int nIndex );
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetDoubleClickTime")] internal extern static int SetDoubleClickTime ( int wCount );
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetDoubleClickTime")] internal extern static int GetDoubleClickTime() ;
[System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint="Sleep")] internal extern static void Sleep ( int dwMilliseconds ) ;
//得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系
public static int FullScreenPosition_X { get { POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos ( ref _POINTAPI ); return _POINTAPI.x; } } public static int FullScreenPosition_Y { get { POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos ( ref _POINTAPI ); return _POINTAPI.y; } }
// 隐藏 显示 鼠标
public static void Hide() { ShowCursor( false ) ; } public static void Show() { ShowCursor( true ) ; }
// 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了
public static void Lock( System.Windows.Forms.Form ObjectForm ) { RECT _FormRect = new RECT (); GetWindowRect( ObjectForm.Handle.ToInt32() , ref _FormRect ); ClipCursor( ref _FormRect ); } public static void UnLock() { RECT _ScreenRect = new RECT (); _ScreenRect.top = 0; _ScreenRect.left = 0; _ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom; _ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right; ClipCursor( ref _ScreenRect ); }
// 鼠标失效,不过失效的好像不只是鼠标,小心哦
public static void Disable( System.Windows.Forms.Form ObjectForm ) { EnableWindow( ObjectForm.Handle.ToInt32() , 0 ) ; }
public static void Enable( System.Windows.Forms.Form ObjectForm ) { EnableWindow( ObjectForm.Handle.ToInt32() , 1 ) ; }
// 鼠标自己移动 很想动画哦 参数是2个控件的handle // 看这个方法前,先用凉水擦把脸。。。 反正我写的时候 头晕
public static void Move ( int From_Handle_ToInt32 , int To_Handle_ToInt32 ) { RECT rectFrom = new RECT () ; RECT rectTo = new RECT () ; int i ; GetWindowRect( From_Handle_ToInt32 , ref rectFrom ) ; GetWindowRect( To_Handle_ToInt32 , ref rectTo ) ;
if ( ( rectFrom.left + rectFrom.right ) / 2 - ( rectTo.left + rectTo.right ) / 2 > 0 ) { for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i >= ( rectTo.left + rectTo.right ) / 2 ; i-- ) { SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ; Sleep ( 1 ) ; } } else { for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i <= ( rectTo.left + rectTo.right ) / 2 ; i++ ) { SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ; Sleep ( 1 ) ; } }
if ( ( rectFrom.top + rectFrom.bottom ) / 2 - ( rectTo.top + rectTo.bottom ) / 2 > 0 ) { for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i >= ( rectTo.top + rectTo.bottom ) / 2 ; i-- ) { SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ; Sleep ( 1 ) ; } } else { for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i <= ( rectTo.top + rectTo.bottom ) / 2 ; i++ ) { SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ; Sleep ( 1 ) ; } } } // 得到你的鼠标类型
public static string Type { get { if ( GetSystemMetrics( SM_MOUSEPRESENT ) == 0 ) { return "本计算机尚未安装鼠标" ; } else { if ( GetSystemMetrics( SM_MOUSEWHEELPRESENT ) != 0 ) { return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键滚轮鼠标" ; } else { return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键鼠标" ; } } } }
// 设置鼠标双击时间 public static void DoubleClickTime_Set( int MouseDoubleClickTime ) { SetDoubleClickTime( MouseDoubleClickTime ); } public static string DoubleClickTime_Get() { return GetDoubleClickTime().ToString() ; }
// 设置鼠标默认主键 我是没有见过谁左手用鼠标
public static void DefaultRightButton() { SwapMouseButton ( 1 ) ; } public static void DefaultLeftButton() { SwapMouseButton ( 0 ) ; } } }
| |
相关推荐
如果API需要一个结构体作为参数,你需要先在C#中定义相应的结构体,然后创建实例并传递给API。例如,API可能需要`RECT`结构体来处理矩形区域: ```csharp [StructLayout(LayoutKind.Sequential)] public struct ...
在Windows操作系统中,这种功能通常涉及到Windows API,也就是Win32 API。 在C#中调用API,我们需要使用`DllImport`特性来导入外部库(DLL),并定义对应的函数原型。关于隐藏和显示鼠标的API,主要涉及以下两个...
下面通过一个简单的例子来展示如何在C#中调用API。 ##### 实现一个简单的MessageBox 1. **创建项目**:在Visual Studio中新建一个C# Windows Forms应用程序项目。 2. **添加控件**:向主窗体添加一个按钮(Button)...
主要涉及的API函数是`mouse_event`,这是一个Win32 API函数,可以模拟鼠标的各种操作。首先,我们需要引入`System.Runtime.InteropServices`命名空间,以便能够使用P/Invoke技术调用API函数。 ```csharp using ...
我意识到我可以使用低级 Win32 API 钩子来完成这项工作,但无法在网上或 GitHub 上的其他地方找到任何工作。 我看到的大多数项目的 API 或方法都存在问题,这些问题不再适用于 Visual Studio 2012/2013 或 .NET 4.5...
以下是一个简单的示例,展示了如何在C#中更改鼠标图标: ```csharp using System; using System.Runtime.InteropServices; public class MouseCursorChanger { // P/Invoke declarations [DllImport("user32.dll...
本篇文章将详细介绍如何利用C#中的钩子(Hook)机制来实现对鼠标移动的监视,并提供一个简单的示例程序。 #### 钩子(Hook)简介 钩子是一种Windows提供的机制,允许应用程序捕获并处理系统中的特定事件。通过设置...
首先,HID设备是一类常见的USB设备,如键盘、鼠标、游戏控制器等。它们遵循USB HID类规范,无需操作系统提供特定的驱动程序即可工作,因为Windows操作系统内置了对HID设备的支持。 在C#中实现HID设备通信,我们可以...
P/Invoke允许.NET应用程序直接调用非托管代码,如Win32 API函数。 以下是创建键盘钩子的基本步骤: 1. **定义API函数和结构**:首先,我们需要声明`SetWindowsHookEx`、`UnhookWindowsHookEx`、`CallNextHookEx`...
在本文中,我们将深入探讨如何使用C#编程语言创建一个鼠标设置器,这涉及到系统级别的操作,例如修改鼠标属性、捕获鼠标事件以及与Windows API交互。C#是一种功能强大的面向对象的语言,非常适合开发这样的系统工具...
这通常涉及创建一个Win32 DLL,并在C#中导入相关API函数。 ```csharp using System.Runtime.InteropServices; // ... [DllImport("user32.dll")] public static extern IntPtr SetWindowsHookEx(int idHook, ...
总结,通过C#结合Win32 API,我们可以轻松地创建一个没有标题栏但可拖动的窗体。这个过程中涉及的关键技术包括API函数的调用、窗体样式的修改以及鼠标事件的处理。这样的自定义窗体设计,既满足了界面的个性化需求,...
在这段代码中,我们定义了一个名为`Win32`的类,其中包含了`POINT`结构体以及`GetCursorPos`方法的声明。`GetScreenPosition`方法负责调用`GetCursorPos`并返回鼠标当前位置的坐标。最后,在`Main`方法中,我们调用...
这里假设有一个名为`pictureBox`的`PictureBox`控件,用于显示图像,以及一个名为`labelPixelInfo`的`Label`控件来显示像素信息。 5. **图像处理操作**: 获取像素值后,可以执行各种图像处理操作,比如改变颜色...
2004.03.11 21:10:15 完成滚动字幕的设置,启用了一个TIMER控件,然后设置时间,删除字符串的第一个字母已达到滚动效果! 2004.03.11 22:02:00 改正更新时出现空值出错问题,新填函数isnull 2004.03.12 13:22:08 ...
总结来说,"HideCursor.zip"提供的示例涉及到C#中使用Win32 API的基本方法,包括`DllImport`特性的使用,以及如何调用`SetCursorVisible`和`ShowCursor`等API函数来控制鼠标的可见性。通过学习这个示例,开发者可以...
- 接下来,我们创建一个窗体类,并在构造函数中设置透明属性: ```csharp public partial class TransparentForm : Form { public TransparentForm() { InitializeComponent(); this.SetStyle(ControlStyles....
- 需要在C#中定义相应的P/Invoke声明,并导入必要的Win32 API函数。 - 示例代码如下: ```csharp [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); // 调用示例 ...
using Microsoft.Win32; // 创建或打开指定的主键和子键 RegistryKey rootKey = Registry.CurrentUser; RegistryKey subKey = rootKey.CreateSubKey(@"Software\MyApp"); subKey.SetValue("AppName", "My ...
首先,我们需要在C#项目中添加对用户控件的引用,并定义一个自定义的窗体类。在这个类中,我们将重写`OnPaint`事件处理程序,以绘制窗体的形状。这通常涉及到使用`Graphics`类的绘图方法,如`DrawPolygon`、`...