(一)C# Windows Mobile 半透明窗体
2024-09-05 07:38:14
Windows Mobile,个人心中臻至完美的系统。
不忍自己对WM的钻研成果消逝,故留作纪念。
系列开篇,便是一个曾令自己困扰很久的问题:如何实现半透明窗体。
如果了解Win32编程,其实很简单。
主要用到了三个方法:
SetLayeredWindowAttributes
GetWindowLong
SetWindowLong
核心代码:
private void SetWindowTransparent(byte bAlpha)
{
SetWindowLong(this.Handle, (int)WindowStyle.GWL_EXSTYLE,
GetWindowLong(this.Handle, (int)WindowStyle.GWL_EXSTYLE) | (uint)ExWindowStyle.WS_EX_LAYERED); SetLayeredWindowAttributes(this.Handle, , bAlpha, LWA_ALPHA);
}
效果:
完整代码:
using System; using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; namespace Demo01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} [DllImport("coredll.dll")]
public extern static IntPtr GetDesktopWindow(); [DllImport("coredll.dll")]
public extern static bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
public static uint LWA_COLORKEY = 0x00000001;
public static uint LWA_ALPHA = 0x00000002; [DllImport("coredll.dll")]
public extern static uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
[DllImport("coredll.dll")]
public extern static uint GetWindowLong(IntPtr hwnd, int nIndex); public enum WindowStyle : int
{
GWL_EXSTYLE = -
} public enum ExWindowStyle : uint
{
WS_EX_LAYERED = 0x00080000
} private void SetWindowTransparent(byte bAlpha)
{
try
{
SetWindowLong(this.Handle, (int)WindowStyle.GWL_EXSTYLE,
GetWindowLong(this.Handle, (int)WindowStyle.GWL_EXSTYLE) | (uint)ExWindowStyle.WS_EX_LAYERED); SetLayeredWindowAttributes(this.Handle, , bAlpha, LWA_ALPHA); }
catch
{
}
} private void button1_Click(object sender, EventArgs e)
{
this.Close();
} private void trackBar1_ValueChanged(object sender, EventArgs e)
{
label1.Text = "透明度(0~255): " + trackBar1.Value.ToString();
SetWindowTransparent(Convert.ToByte(trackBar1.Value));
} private void Form1_Load(object sender, EventArgs e)
{
Rectangle rf = Screen.PrimaryScreen.WorkingArea; this.Location = new Point((rf.Width - this.Width) / , (rf.Height - this.Height) / ); //SetWindowTransparent(136);
}
}
}
全部代码
工程文件:
https://files.cnblogs.com/files/lesliexin/01.%E5%8D%8A%E9%80%8F%E6%98%8E%E7%AA%97%E4%BD%93.7z
最新文章
- Windows下快速安装Flask的一次经历
- Win10 QQ五笔默认中文,InputMode默认中文
- hduacm 3183 rmq
- PHP图像处理之在原图像处理
- [leetcode]_Palindrome Number
- fineuploader 上传jquery 控件
- vim配置-程序员【转】
- ASP.NET中使用Server.Transfer()方法在页间传值 实例
- TypeScript02 方法特性【参数种类、参数个数】、generate方法、析构表达式、箭头表达式、循环
- CentOS下将php和mysql命令加入到环境变量中的几种方法
- 使用 Java8 Optional 的正确姿势(转)
- Go Deeper HDU - 3715(2 - sat 水题 妈的 智障)
- maven 中的mirror详解
- 20155332 2016-2017-2 《Java程序设计》第9周学习总结
- input标签(按钮)
- java struts2入门学习---国际化
- C++中的#和##运算符
- Burp Suite pro 抓包工具配置
- [转载] MFC绘制动态曲线,用双缓冲绘图技术防闪烁
- LockSupport HotSpot里park/unpark的实现