C#透明窗體代碼詳解
作者:佚名
C#透明窗體的實現(xiàn)這里向你介紹通過調用Windows API來完成,具體用到的方法和步驟是什么呢?那么本文就向你介紹具體的步驟。
實現(xiàn)C#透明窗體是如何實現(xiàn)的呢?這里向你介紹通過調用Windows API來實現(xiàn)C#透明窗體。那么具體的過程和步驟是什么呢?讓我們來看看具體的實現(xiàn)。
C#透明窗體實現(xiàn)實例:
C#透明窗體之WinAPI.cs類文件,Invoke & Wrap了窗體透明所需要的API函數(shù):
- [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函數(shù)的調用方式:
- [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#透明窗體的實現(xiàn)基本內容就向你介紹到這里,希望對你了解和學習C#透明窗體有所幫助。
【編輯推薦】
責任編輯:仲衡
來源:
百度空間