C# 鼠标穿透窗体功能的实现方法

同样该功能需要加载命名空间

using System.Runtime.InteropServices;

代码如下:

private const uint WS_EX_LAYERED = 0x80000;
        private const int WS_EX_TRANSPARENT = 0x20;
        private const int GWL_STYLE = (-16);
        private const int GWL_EXSTYLE = (-20);
        private const int LWA_ALPHA = 0;

[DllImport("user32", EntryPoint = "SetWindowLong")]
        private static extern uint SetWindowLong(
        IntPtr hwnd,
        int nIndex,
        uint dwNewLong
        );

[DllImport("user32", EntryPoint = "GetWindowLong")]
        private static extern uint GetWindowLong(
        IntPtr hwnd,
        int nIndex
        );

[DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
        private static extern int SetLayeredWindowAttributes(
        IntPtr hwnd,
        int crKey,
        int bAlpha,
        int dwFlags
        );

/// <summary>
        /// 设置窗体具有鼠标穿透效果
        /// </summary>
        public void SetPenetrate()
        {
            GetWindowLong(this.Handle, GWL_EXSTYLE);
            SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
            SetLayeredWindowAttributes(this.Handle, 0, 100, LWA_ALPHA);
        }

(0)

相关推荐

  • 浅析c#中如何在form的webbrowser控件中获得鼠标坐标

    如图这样,其实是要插入一个time的控件,这样才能使得坐标值会根据鼠标的移动而不停变化.time插件中写 复制代码 代码如下: private void timer1_Tick(object sender, EventArgs e)        {            if (webBrowser1.Bounds.Contains(this.PointToClient(Cursor.Position)))            { this.toolStripStatusLabel1.Tex

  • C#中动态显示当前系统时间的实例方法

    各位亲们可以尝试以下代码: 注:这里我就只有一个html标签对来说明问题了,首部之类的东西,自己添加. 复制代码 代码如下: <html>     <head>         <title>网页中动态的显示系统日期时间</title>         <script language="JavaScript">             function startTime() {                 var to

  • C# 禁用鼠标中间键的方法

    复制代码 代码如下: 方法如下: 声明一个事件: Num_DiscountAmount.MouseWheel +=new MouseEventHandler(Num_DiscountAmount_MouseWheel); 编写一个事件 private void  Num_DiscountAmount_MouseWheel(object sender, MouseEventArgs e) {HandledMouseEventArgs h = e as HandledMouseEventArgs;i

  • C#实现带阴历显示的日期代码

    本文实例讲述了C#实现带阴历显示的日期代码,分享给大家供大家参考.具体方法如下: 这是一个用于酒店预定功能的带日期控件,类似去哪网酒店预定,由于需要设置节假日不同时期内的价格,因此需要自己写个时间控件.在此分享下写时间控件过程中用到的农历显示类. 复制代码 代码如下: public class CnCalendar { static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar(); public static

  • C#中winform实现自动触发鼠标、键盘事件的方法

    程序触发鼠标.键盘事件是C#程序设计中比较常见的功能,本文实例展示了C#中winform实现自动触发鼠标.键盘事件的方法,有不错的实用价值.具体如下: 要想在C#程序中触发鼠标.键盘事件就必须要调用windows函数. 一.鼠标事件的触发 1.引用windows函数mouse_event /// <summary> /// 鼠标事件 /// </summary> /// <param name="flags">事件类型</param> /

  • C#实现随鼠标移动窗体实例

    本文实例讲述了c#实现随鼠标移动窗体的方法,分享给大家供大家参考. 具体实现方法如下: private void MainForm_Load(object sender, EventArgs e) { //绑定事件 MouseMove += Form_MouseMove; MouseDown += Form_MouseDown; } private Point _mousePoint; private void Form_MouseMove(object sender, MouseEventAr

  • C#实现鼠标移动到曲线图上显示值的方法

    本文实例讲述了C#实现鼠标移动到曲线图上显示值的方法.分享给大家供大家参考.具体实现方法如下: 一.问题: 完成折线图报表后,产品经理要求把折线上的数值去掉,鼠标经过折线点时显示数值. 二.实现方法: 该方法针对dotnetcharting 下的charting折线图报表使用. 实现思路为,在该窗体上添加一个lable标签,当鼠标经过折线点时获取该点的x轴,y轴值和鼠标坐标值.然后将lable的坐标标记为鼠标所在坐标,并且给lable赋值,并且将lable显示出来.   具体实现代码如下: 复制

  • C#实现获取鼠标句柄的方法

    本文实例讲述了C#实现获取鼠标句柄的方法,分享给大家供大家参考.具体实现方法如下: 一.调用user32.dll (1)引用 using System.Runtime.InteropServices; (2)调用方法 1.获取窗口标题 [DllImport( "user32.dll" )] public static extern int GetWindowText( IntPtr hWnd, StringBuilder lpString,int nMaxCount ); 注:hWnd

  • 用C# 实现鼠标框选效果的实现代码

    实现步骤: 1.实现整个鼠标框选的几个事件(down.move.up),当鼠标点下记录鼠标框选的起点,鼠标抬起结束操作. 2.以鼠标框选过程中获取的鼠标坐标为基点计算框选的矩形的4点坐标,4点坐标以顺时针方向布点. 3.通过Shape.Path类实现在类上画出此矩形. 代码如下: 复制代码 代码如下: namespace HostDemo { public class HostCanvas : Canvas {  public HostCanvas() {   InitializeCompone

  • c#显示当前在线人数示例

    1.Global.asax文件: 复制代码 代码如下: <%@ Application Language="C#" %><%@ Import Namespace="System.Xml" %><script runat="server"> void Application_Start(object sender, EventArgs e)     {        // 在应用程序启动时运行的代码       

随机推荐