求 C# 获得另外一程序 句柄 后控制该程序,如控制其透明,或窗体大小等(求代码)

zenwong 2008-03-01 10:31:25
如题
...全文
172 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
王集鹄 2008-03-01
  • 打赏
  • 举报
回复
都是些API调用,参考如下代码:
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);
}


KK3K2005 2008-03-01
  • 打赏
  • 举报
回复
估计要调用 WINDOWS API了
蒋晟 2008-03-01
  • 打赏
  • 举报
回复
控制?目标程序有给你控制的接口么?

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧