C# 图像处理: 获取当前活动窗口句柄,获取窗口大小及位置
2014-03-30 19:43
495 查看
需调用API函数
需在开头引入命名空间 using System.Runtime.InteropServices;
获取当前窗口句柄:GetForegroundWindow()
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
返回值类型是IntPtr,即为当前获得焦点窗口的句柄
使用方法 : IntPtr myPtr=GetForegroundWindow();
获取到该窗口句柄后,可以对该窗口进行操作.比如,关闭该窗口或在该窗口隐藏后,使其显示
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
其中ShowWindow(IntPtr hwnd, int nCmdShow);
nCmdShow的含义
0 关闭窗口
1 正常大小显示窗口
2 最小化窗口
3 最大化窗口
使用实例: ShowWindow(myPtr, 0);
获取窗口大小及位置:
需要调用方法GetWindowRect(IntPtr hWnd, ref RECT lpRect)
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left; //最左坐标
public int Top; //最上坐标
public int Right; //最右坐标
public int Bottom; //最下坐标 }
示例:
InPtr awin = GetForegroundWindow(); //获取当前窗口句柄
RECT rect = new RECT();
GetWindowRect(awin, ref rect);
int width = rc.Right - rc.Left; //窗口的宽度
int height = rc.Bottom - rc.Top; //窗口的高度
int x = rc.Left;
int y = rc.Top;
需在开头引入命名空间 using System.Runtime.InteropServices;
获取当前窗口句柄:GetForegroundWindow()
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
返回值类型是IntPtr,即为当前获得焦点窗口的句柄
使用方法 : IntPtr myPtr=GetForegroundWindow();
获取到该窗口句柄后,可以对该窗口进行操作.比如,关闭该窗口或在该窗口隐藏后,使其显示
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
其中ShowWindow(IntPtr hwnd, int nCmdShow);
nCmdShow的含义
0 关闭窗口
1 正常大小显示窗口
2 最小化窗口
3 最大化窗口
使用实例: ShowWindow(myPtr, 0);
获取窗口大小及位置:
需要调用方法GetWindowRect(IntPtr hWnd, ref RECT lpRect)
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left; //最左坐标
public int Top; //最上坐标
public int Right; //最右坐标
public int Bottom; //最下坐标 }
示例:
InPtr awin = GetForegroundWindow(); //获取当前窗口句柄
RECT rect = new RECT();
GetWindowRect(awin, ref rect);
int width = rc.Right - rc.Left; //窗口的宽度
int height = rc.Bottom - rc.Top; //窗口的高度
int x = rc.Left;
int y = rc.Top;
相关文章推荐
- c# 获取当前活动窗口句柄,获取窗口大小及位置
- c# 获取当前活动窗口句柄,获取窗口大小及位置
- c# 获取当前活动窗口句柄,获取窗口大小及位置
- 在MFC中获取窗口\视图句柄 &获取当前活动的CView .
- 更改获取到窗口句柄的游戏界面大小、位置
- MFC窗口最大化&移动窗口位置&获取控件句柄,设置控件大小
- 易语言获取当前鼠标的窗口句柄、位置、颜色值
- C#获取当前活动窗口进程、进程ID、程序路径
- C#获取鼠标位置窗口句柄
- MFC 获取当前活动窗口的句柄
- 在MFC中获取窗口\视图句柄 &获取当前活动的CView .
- Camshift原理 camshift利用目标的颜色直方图模型将图像转换为颜色概率分布图,初始化一个搜索窗的大小和位置,并根据上一帧得到的结果自适应调整搜索窗口的位置和大小,从而定位出当前图像中目标的
- C#:根据鼠标位置获取窗口句柄,名字等
- WinAPI: GetActiveWindow - 获取当前活动窗口的句柄
- 在MFC中获取窗口\视图句柄 &获取当前活动的CView
- 获取当前活动窗口\视图句柄
- MFC 获取当前活动窗口的句柄
- 获取当前活动窗口句柄和标题
- C#可调用API接口来获取窗口句柄,发送消息控制其余程序窗体大小
- C# 图像处理:获取鼠标位置信息(全局)