C#调用Windows API函数,实现透明窗体
2006-11-24 01:22
351 查看
这篇文章不是Windows Mobile的,而是Win32的。这篇文章主要介绍一下C#下如何调用Windows API函数,这里也想说一下,Windows Mobile编程不能把眼光只局限于手机,手机与PC端相结合的程序也是很有挑战力、很有市场的。所以,这也是我写这篇文章的原因之一。
做Delphi的时候,实现窗体透明很简单,因为Delphi对Windows API的封装很好。不只对API函数封装的到位,对API函数所用到的参数封装的也很好。而.net没有对API函数进行封装,对API函数的参数就更没有封装了。调用API函数只能用Invoke的方式,参数也需要我们自己进行相关定义。
DesktopWinAPI.cs类文件,Invoke了窗体透明所需要的API函数:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace DeviceAnywhereDesktop
{
class DesktopWinAPI
{
[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 = 0x00000001;
public static uint LWA_ALPHA = 0x00000002;
[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 = 0x00080000
}
}
}
DeviceForm.cs单元是API函数的调用方式:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DeviceAnywhereDesktop
{
public partial class DeviceForm : Form
{
public DeviceForm()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Parent = DesktopWinAPI.GetDesktopWindow();
cp.ExStyle = 0x00000080 | 0x00000008;//WS_EX_TOOLWINDOW | WS_EX_TOPMOST
return cp;
}
}
private void SetWindowTransparent(byte bAlpha)
{
try
{
DesktopWinAPI.SetWindowLong(this.Handle, (int)DesktopWinAPI.WindowStyle.GWL_EXSTYLE,
DesktopWinAPI.GetWindowLong(this.Handle, (int)DesktopWinAPI.WindowStyle.GWL_EXSTYLE) | (uint)DesktopWinAPI.ExWindowStyle.WS_EX_LAYERED);
DesktopWinAPI.SetLayeredWindowAttributes(this.Handle, 0, bAlpha, DesktopWinAPI.LWA_COLORKEY | DesktopWinAPI.LWA_ALPHA);
}
catch
{
}
}
private void DeviceForm_Load(object sender, EventArgs e)
{
this.SetWindowTransparent(100);
}
}
}
做Delphi的时候,实现窗体透明很简单,因为Delphi对Windows API的封装很好。不只对API函数封装的到位,对API函数所用到的参数封装的也很好。而.net没有对API函数进行封装,对API函数的参数就更没有封装了。调用API函数只能用Invoke的方式,参数也需要我们自己进行相关定义。
DesktopWinAPI.cs类文件,Invoke了窗体透明所需要的API函数:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace DeviceAnywhereDesktop
{
class DesktopWinAPI
{
[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 = 0x00000001;
public static uint LWA_ALPHA = 0x00000002;
[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 = 0x00080000
}
}
}
DeviceForm.cs单元是API函数的调用方式:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DeviceAnywhereDesktop
{
public partial class DeviceForm : Form
{
public DeviceForm()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Parent = DesktopWinAPI.GetDesktopWindow();
cp.ExStyle = 0x00000080 | 0x00000008;//WS_EX_TOOLWINDOW | WS_EX_TOPMOST
return cp;
}
}
private void SetWindowTransparent(byte bAlpha)
{
try
{
DesktopWinAPI.SetWindowLong(this.Handle, (int)DesktopWinAPI.WindowStyle.GWL_EXSTYLE,
DesktopWinAPI.GetWindowLong(this.Handle, (int)DesktopWinAPI.WindowStyle.GWL_EXSTYLE) | (uint)DesktopWinAPI.ExWindowStyle.WS_EX_LAYERED);
DesktopWinAPI.SetLayeredWindowAttributes(this.Handle, 0, bAlpha, DesktopWinAPI.LWA_COLORKEY | DesktopWinAPI.LWA_ALPHA);
}
catch
{
}
}
private void DeviceForm_Load(object sender, EventArgs e)
{
this.SetWindowTransparent(100);
}
}
}
相关文章推荐
- 用C#调用Windows API和其它进程通信及C# 获得另外一程序 句柄 后控制该程序 控制其透明 窗体大小
- C#通过委托和异步调用实现窗体间的通讯
- C# 委托实现多线程调用窗体
- C#Winform 自定义透明按钮和单窗体模块化实现
- C#事件调用,实现两个窗体间传值
- C#透明窗体实现方法
- C#实现透明窗体
- C#利用接口实现窗体间函数调用
- C# 在windows form实现不规则窗体&透明窗体
- 利用System.EventHandler来实现两个窗体间的事件调用 (C#)
- C#实现按钮透明,窗体透明的方法
- C#中实现 子窗体控制调用父窗体成员和控件
- c# 实现透明窗体,透明图片,透明按钮
- C#透明窗体实现方法
- C#调用外部exe作为子窗体或UI? VC++如何实现呢呢?...札记plus
- C# 使用委托实现多线程调用窗体
- C# 透明窗体制作实现方法比较分析
- [转自:https://www.cnblogs.com/dskin/p/4606293.html] C# Winform实现炫酷的透明动画界面 做过.NET Winform窗体美化的人应该都很熟悉U
- C# 实现一个窗体调用另一个窗体的控件
- C#窗体应用程序中,实现调用已有应用程序