威尼斯www.9778.com-威尼斯正版官方网站

WinForm特效:桌面上的遮罩层

日期:2020-01-04编辑作者:编程人生

把窗体的FormBorderStyle设为FormBorderStyle.None

一个窗体特效,帮你了解几个windows api函数.效果:windows桌面上增加一个简单的遮罩层,其中WS_EX_TRANSPARENT 比较重要,它实现了鼠标穿透的功能。

Option Explicit

using System.Runtime.InteropServices;

[csharp] view plaincopy

Private Declare Funсtion GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Funсtion SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Funсtion SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

 

  1. using System;  
  2.   
  3. using System.Drawing;  
  4.   
  5. using System.Windows.Forms;  
  6.   
  7. using System.Runtime.InteropServices;  
  8.   
  9. namespace WindowsApplication40  
  10.   
  11. {  
  12.   
  13.     public partial class Form1 : Form  
  14.   
  15.     {  
  16.   
  17.         public Form1()  
  18.   
  19.         {  
  20.   
  21.             InitializeComponent();  
  22.   
  23.         }  
  24.   
  25.         [DllImport("user32.dll", EntryPoint = "GetWindowLong")]  
  26.   
  27.         public static extern long GetWindowLong(IntPtr hwnd, int nIndex);  
  28.   
  29.   
  30.   
  31.         [DllImport("user32.dll", EntryPoint = "SetWindowLong")]  
  32.   
  33.         public static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);  
  34.   
  35.   
  36.   
  37.         [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]  
  38.   
  39.         private static extern int SetLayeredWindowAttributes(IntPtr Handle, int crKey, byte bAlpha, int dwFlags);  
  40.   
  41.   
  42.   
  43.         const int GWL_EXSTYLE = -20;  
  44.   
  45.         const int WS_EX_TRANSPARENT = 0x20;  
  46.   
  47.         const int WS_EX_LAYERED = 0x80000;  
  48.   
  49.         const int LWA_ALPHA = 2;  
  50.   
  51.   
  52.   
  53.   
  54.   
  55.         private void Form1_Load(object sender, EventArgs e)  
  56.   
  57.         {  
  58.   
  59.             this.BackColor = Color.Silver;  
  60.   
  61.             this.TopMost = true;  
  62.   
  63.             this.FormBorderStyle = FormBorderStyle.None;  
  64.   
  65.             this.WindowState = FormWindowState.Maximized;  
  66.   
  67.             SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_TRANSPARENT | WS_EX_LAYERED);  
  68.   
  69.             SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA );  
  70.   
  71.   
  72.   
  73.         }  
  74.   
  75.     }  
  76.   
  77. }  

Const LWA_COLORKEY = &H1
Const LWA_ALPHA = &H2
Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000
Const WS_EX_TRANSPARENT As Long = &H20&

        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 = 0x2;

Private Sub Form_Load()
Dim Ret As Long

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

Ret = GetWindowLong(Me.hWnd, GWL_EXSTYLE) 
Ret = Ret Or WS_EX_LAYERED Or WS_EX_TRANSPARENT 
SetWindowLong Me.hWnd, GWL_EXSTYLE, Ret 
SetLayeredWindowAttributes Me.hWnd, 0, 200, LWA_ALPHA'第三个参数200是设置透明度 

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

End Sub

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

C# 版本:

        ///
        /// 使窗口有鼠标穿透功能
        ///
        public void CanPenetrate()
        {
            uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE);
            uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
            SetLayeredWindowAttributes(this.Handle, 0, 100, LWA_ALPHA);
        } 

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

要时窗体恢复正常,只要执行以下语句:
                this.FormBorderStyle = FormBorderStyle.None;

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);
    }

本文由威尼斯www.9778.com发布于编程人生,转载请注明出处:WinForm特效:桌面上的遮罩层

关键词:

总结C#中得到程序当前工作目录和执行目录的一些方法

 1.  System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName      获取模块的完整路径。 2.   System.Environment.CurrentDir...

详细>>

PHP数组操作详解

下标:数组中的识别名称 也就是字符串或整数在数组中的代号 要访问一个变量的内容,可以直接使用其名称。如果...

详细>>

>>>---PHP中的OOP-->面对过程与面对对象基础概念与内容

  对象的主要三个特性 对象的行为:可以对 对象施加那些操作,开灯,关灯就是行为。 对象的形态:当施加那些方...

详细>>

php学习 字符串课件

语法 字符串string可以有三种方法定义 ‘ '(单引号)“”(双引号) (定界符)    字符串 注意单引号和双引号的区别 注...

详细>>