110,545
社区成员
发帖
与我相关
我的任务
分享
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern bool SetLayeredWindowAttributes(
IntPtr hWnd, int crKey, byte bAlpha, int dwFlags);
[DllImport("user32.dll")]
public static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate,
IntPtr hrgnUpdate, uint flags);
public const int GWL_EXSTYLE = -20;
public const int WS_EX_LAYERED = 0x00080000;
public const int LWA_ALPHA = 0x00000002;
public const int RDW_INVALIDATE = 1;
public const int RDW_ERASE = 4;
public const int RDW_ALLCHILDREN = 0x80;
public const int RDW_FRAME = 0x400;
private void button1_Click(object sender, EventArgs e)
{
//设置透明
IntPtr vHandle = FindWindow("Notepad", null); // 这里换成你获得的窗体句柄,测试的时候可以用记事本。
SetWindowLong(vHandle, GWL_EXSTYLE,
GetWindowLong(vHandle, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(vHandle, 0, 255 / 2/*透明度*/, LWA_ALPHA);
}
private void button2_Click(object sender, EventArgs e)
{
//恢复
IntPtr vHandle = FindWindow("Notepad", null);
SetWindowLong(vHandle, GWL_EXSTYLE,
GetWindowLong(vHandle, GWL_EXSTYLE) & ~WS_EX_LAYERED);
RedrawWindow(vHandle, IntPtr.Zero, IntPtr.Zero,
RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
}
[DllImport("user32.dll")]
public static extern bool MoveWindow(
IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
private void button3_Click(object sender, EventArgs e)
{
//改变窗体大小和位置
IntPtr vHandle = FindWindow("Notepad", null);
MoveWindow(vHandle, 20, 20, 200, 200, true);
}