实现C#透明窗体是如何实现的呢?这里向你介绍通过调用Windows API来实现C#透明窗体。那么具体的过程和步骤是什么呢?让我们来看看具体的实现。

创新互联于2013年创立,先为慈溪等服务建站,慈溪等地企业,进行企业商务咨询服务。为慈溪企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
C#透明窗体实现实例:
C#透明窗体之WinAPI.cs类文件,Invoke & Wrap了窗体透明所需要的API函数:
- [coolcode lang="cpp" download="WinAPI.cs"]
 - using System;
 - using System.Collections.Generic;
 - using System.Text;
 - using System.Runtime.InteropServices;
 - namespace TransForm
 - {
 - class WinAPI
 - {
 - [DllImport("user32.dll")]
 - public extern static IntPtr GetDesktopWindow();
 - [DllImport("user32.dll")]
 - public extern static bool
 - SetLayeredWindowAttributes(
 - IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
 - public static uint LWA_COLORKEY = 0×00000001;
 - public static uint LWA_ALPHA = 0×00000002;
 - [DllImport("user32.dll")]
 - public extern static uint
 - SetWindowLong(IntPtr hwnd,
 - int nIndex, uint dwNewLong);
 - [DllImport("user32.dll")]
 - public extern static uint
 - GetWindowLong(IntPtr hwnd, int nIndex);
 - public enum WindowStyle : int
 - {
 - GWL_EXSTYLE = -20
 - }
 - public enum ExWindowStyle : uint
 - {
 - WS_EX_LAYERED = 0×00080000
 - }
 - }
 - }
 - [/coolcode]
 
C#透明窗体之DeviceForm.cs单元是API函数的调用方式:
- [coolcode lang="cpp" download="form1.cs"]
 - using System;
 - using System.Collections.Generic;
 - using System.ComponentModel;
 - using System.Data;
 - using System.Drawing;
 - using System.Text;
 - using System.Windows.Forms;
 - namespace TransForm
 - {
 - public partial class Form1 : Form
 - {
 - public Form1()
 - {
 - InitializeComponent();
 - }
 - private void Form1_Load(object sender, EventArgs e)
 - {
 - this.SetWindowTransparent(100);
 - }
 - private void SetWindowTransparent(byte bAlpha)
 - {
 - try
 - {
 - WinAPI.SetWindowLong(
 - this.Handle,
 - (int)WinAPI.WindowStyle.GWL_EXSTYLE,
 - WinAPI.GetWindowLong(
 - this.Handle,
 - (int)WinAPI.WindowStyle.GWL_EXSTYLE) |
 - (uint)WinAPI.ExWindowStyle.WS_EX_LAYERED);
 - WinAPI.SetLayeredWindowAttributes(
 - this.Handle, 0, bAlpha,
 - WinAPI.LWA_COLORKEY | WinAPI.LWA_ALPHA);
 - }
 - catch
 - {
 - }
 - }
 - protected override CreateParams CreateParams
 - {
 - get
 - {
 - CreateParams cp = base.CreateParams;
 - cp.Parent = WinAPI.GetDesktopWindow();
 - cp.ExStyle = 0×00000080 | 0×00000008;
 - //WS_EX_TOOLWINDOW | WS_EX_TOPMOST
 - return cp;
 - }
 - }
 - }
 - }
 - [/coolcode]
 
C#透明窗体的实现基本内容就向你介绍到这里,希望对你了解和学习C#透明窗体有所帮助。
                标题名称:C#透明窗体代码详解
                
                路径分享:http://www.csdahua.cn/qtweb/news15/49015.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网