Sam*_*ver 11 .net c# transparent winforms
我正在设计一个始终在屏幕上,大约20%不透明的窗口.它被设计成一种状态窗口,因此它始终位于顶部,但我希望人们能够通过窗口点击下面的任何其他应用程序.我现在输入的是这个SO帖子顶部的不透明窗口:
看到灰色吧?这会阻止我在此刻输入标签框.
Rez*_*aei 21
您可以通过向其扩展样式添加WS_EX_LAYERED和WS_EX_TRANSPARENT样式来创建窗口,单击.也以使其总在最前面设置其TopMost到true并使其半透明的合适的Opacity值:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Opacity = 0.5;
this.TopMost = true;
}
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x80000;
const int WS_EX_TRANSPARENT = 0x20;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var style = GetWindowLong(this.Handle, GWL_EXSTYLE);
SetWindowLong(this.Handle,GWL_EXSTYLE , style | WS_EX_LAYERED | WS_EX_TRANSPARENT);
}
}
Run Code Online (Sandbox Code Playgroud)
样本结果
| 归档时间: |
|
| 查看次数: |
1702 次 |
| 最近记录: |