c# 获取鼠标处窗口句柄,程序嵌入桌面
2014-04-01 22:45
453 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int GW_HWNDFIRST = 0; //{同级别 Z 序最上}
const int GW_HWNDLAST = 1; //{同级别 Z 序最下}
const int GW_HWNDNEXT = 2; //{同级别 Z 序之下}
const int GW_HWNDPREV = 3; //{同级别 Z 序之上}
const int GW_OWNER = 4; //{属主窗口}
const int GW_CHILD = 5; //{子窗口中的最上}
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32.dll", EntryPoint = "GetWindow")]//获取窗体句柄,hwnd为源窗口句柄
/*wCmd指定结果窗口与源窗口的关系,它们建立在下述常数基础上:
GW_CHILD
寻找源窗口的第一个子窗口
GW_HWNDFIRST
为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口
GW_HWNDLAST
为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口
GW_HWNDNEXT
为源窗口寻找下一个兄弟窗口
GW_HWNDPREV
为源窗口寻找前一个兄弟窗口
GW_OWNER
寻找窗口的所有者
*/
public static extern int GetWindow(
int hwnd,
int wCmd
);
[DllImport("user32.dll", EntryPoint = "SetParent")]//设置父窗体
public static extern int SetParent(
int hWndChild,
int hWndNewParent
);
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
public static extern int GetCursorPos(
ref POINTAPI lpPoint
);
[StructLayout(LayoutKind.Sequential)]//定义与API相兼容结构体,实际上是一种内存转换
public struct POINTAPI
{
public int X;
public int Y;
}
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
public static extern int WindowFromPoint(
int xPoint,
int yPoint
);
private void timer1_Tick(object sender, EventArgs e)
{
POINTAPI point = new POINTAPI();//必须用与之相兼容的结构体,类也可以
GetCursorPos(ref point);//获取当前鼠标坐标
int hwnd = WindowFromPoint(point.X, point.Y);//获取指定坐标处窗口的句柄
this.label1.Text =point.X.ToString() + ":" + point.Y.ToString() + "-" + hwnd.ToString();//显示效果,此时窗口已经嵌入桌面了
}
const int GW_CHILD = 5;//定义窗体关系
private void Form1_Load(object sender, EventArgs e)
{
int hDesktop = FindWindow("Progman", null);//获取系统句柄
hDesktop = GetWindow(hDesktop, GW_CHILD);//获取其子窗口句柄,就是桌面的句柄
SetParent((int)this.Handle, hDesktop);//设置父窗体,第一个为要被设置的窗口,第二个参数为指定其父窗口句柄
}
}
}
一个网上的例子
c# 获取鼠标处窗口句柄,程序嵌入桌面
using System;
using System.Collections.Generic;
using System
.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32.dll", EntryPoint = "GetWindow")]//获取窗体句柄,hwnd为源窗口句柄
/*wCmd指定结果窗口与源窗口的关系,它们建立在下述常数基础上:
GW_CHILD
寻找源窗口的第一个子窗口
GW_HWNDFIRST
为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口
GW_HWNDLAST
为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口
GW_HWNDNEXT
为源窗口寻找下一个兄弟窗口
GW_HWNDPREV
为源窗口寻找前一个兄弟窗口
GW_OWNER
寻找窗口的所有者
*/
public static extern int GetWindow(
int hwnd,
int wCmd
);
[DllImport("user32.dll", EntryPoint = "SetParent")]//设置父窗体
public static extern int SetParent(
int hWndChild,
int hWndNewParent
);
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
public static extern int GetCursorPos(
ref POINTAPI lpPoint
);
[StructLayout(LayoutKind.Sequential)]//定义与API相兼容结构体,实际上是一种内存转换
public struct POINTAPI
{
public int X;
public int Y;
}
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
public static extern int WindowFromPoint(
int xPoint,
int yPoint
);
private void timer1_Tick(object sender, EventArgs e)
{
POINTAPI point = new POINTAPI();//必须用与之相兼容的结构体,类也可以
GetCursorPos(ref point);//获取当前鼠标坐标
int hwnd = WindowFromPoint(point.X, point.Y);//获取指定坐标处窗口的句柄
this.label1.Text =point.X.ToString() + ":" + point.Y.ToString() + "-" + hwnd.ToString();//显示效果,此时窗口已经嵌入桌面了
}
const int GW_CHILD = 5;//定义窗体关系
private void Form1_Load(object sender, EventArgs e)
{
int hDesktop = FindWindow("Progman", null);//获取系统句柄
hDesktop = GetWindow(hDesktop, GW_CHILD);//获取其子窗口句柄,就是桌面的句柄
SetParent((int)this.Handle, hDesktop);//设置父窗体,第一个为要被设置的窗口,第二个参数为指定其父窗口句柄
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int GW_HWNDFIRST = 0; //{同级别 Z 序最上}
const int GW_HWNDLAST = 1; //{同级别 Z 序最下}
const int GW_HWNDNEXT = 2; //{同级别 Z 序之下}
const int GW_HWNDPREV = 3; //{同级别 Z 序之上}
const int GW_OWNER = 4; //{属主窗口}
const int GW_CHILD = 5; //{子窗口中的最上}
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32.dll", EntryPoint = "GetWindow")]//获取窗体句柄,hwnd为源窗口句柄
/*wCmd指定结果窗口与源窗口的关系,它们建立在下述常数基础上:
GW_CHILD
寻找源窗口的第一个子窗口
GW_HWNDFIRST
为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口
GW_HWNDLAST
为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口
GW_HWNDNEXT
为源窗口寻找下一个兄弟窗口
GW_HWNDPREV
为源窗口寻找前一个兄弟窗口
GW_OWNER
寻找窗口的所有者
*/
public static extern int GetWindow(
int hwnd,
int wCmd
);
[DllImport("user32.dll", EntryPoint = "SetParent")]//设置父窗体
public static extern int SetParent(
int hWndChild,
int hWndNewParent
);
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
public static extern int GetCursorPos(
ref POINTAPI lpPoint
);
[StructLayout(LayoutKind.Sequential)]//定义与API相兼容结构体,实际上是一种内存转换
public struct POINTAPI
{
public int X;
public int Y;
}
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
public static extern int WindowFromPoint(
int xPoint,
int yPoint
);
private void timer1_Tick(object sender, EventArgs e)
{
POINTAPI point = new POINTAPI();//必须用与之相兼容的结构体,类也可以
GetCursorPos(ref point);//获取当前鼠标坐标
int hwnd = WindowFromPoint(point.X, point.Y);//获取指定坐标处窗口的句柄
this.label1.Text =point.X.ToString() + ":" + point.Y.ToString() + "-" + hwnd.ToString();//显示效果,此时窗口已经嵌入桌面了
}
const int GW_CHILD = 5;//定义窗体关系
private void Form1_Load(object sender, EventArgs e)
{
int hDesktop = FindWindow("Progman", null);//获取系统句柄
hDesktop = GetWindow(hDesktop, GW_CHILD);//获取其子窗口句柄,就是桌面的句柄
SetParent((int)this.Handle, hDesktop);//设置父窗体,第一个为要被设置的窗口,第二个参数为指定其父窗口句柄
}
}
}
一个网上的例子
c# 获取鼠标处窗口句柄,程序嵌入桌面
using System;
using System.Collections.Generic;
using System
.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32.dll", EntryPoint = "GetWindow")]//获取窗体句柄,hwnd为源窗口句柄
/*wCmd指定结果窗口与源窗口的关系,它们建立在下述常数基础上:
GW_CHILD
寻找源窗口的第一个子窗口
GW_HWNDFIRST
为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口
GW_HWNDLAST
为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口
GW_HWNDNEXT
为源窗口寻找下一个兄弟窗口
GW_HWNDPREV
为源窗口寻找前一个兄弟窗口
GW_OWNER
寻找窗口的所有者
*/
public static extern int GetWindow(
int hwnd,
int wCmd
);
[DllImport("user32.dll", EntryPoint = "SetParent")]//设置父窗体
public static extern int SetParent(
int hWndChild,
int hWndNewParent
);
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
public static extern int GetCursorPos(
ref POINTAPI lpPoint
);
[StructLayout(LayoutKind.Sequential)]//定义与API相兼容结构体,实际上是一种内存转换
public struct POINTAPI
{
public int X;
public int Y;
}
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
public static extern int WindowFromPoint(
int xPoint,
int yPoint
);
private void timer1_Tick(object sender, EventArgs e)
{
POINTAPI point = new POINTAPI();//必须用与之相兼容的结构体,类也可以
GetCursorPos(ref point);//获取当前鼠标坐标
int hwnd = WindowFromPoint(point.X, point.Y);//获取指定坐标处窗口的句柄
this.label1.Text =point.X.ToString() + ":" + point.Y.ToString() + "-" + hwnd.ToString();//显示效果,此时窗口已经嵌入桌面了
}
const int GW_CHILD = 5;//定义窗体关系
private void Form1_Load(object sender, EventArgs e)
{
int hDesktop = FindWindow("Progman", null);//获取系统句柄
hDesktop = GetWindow(hDesktop, GW_CHILD);//获取其子窗口句柄,就是桌面的句柄
SetParent((int)this.Handle, hDesktop);//设置父窗体,第一个为要被设置的窗口,第二个参数为指定其父窗口句柄
}
}
}
相关文章推荐
- C#获取鼠标处的窗口(window)句柄
- C#:根据鼠标位置获取窗口句柄,名字等
- C#可调用API接口来获取窗口句柄,发送消息控制其余程序窗体大小
- C#通过鼠标获取窗口句柄
- C# 通过窗口句柄获取程序路径 图标
- C#获取鼠标位置窗口句柄
- C# 通过窗口句柄获取程序运行路径 通过程序运行路径获取程序图标
- C#获取鼠标处的窗口(window)句柄的小工具
- Windows程序获取鼠标下窗口句柄的例子
- C#_获取鼠标所在处的窗口句柄
- 转:VC:如何根据文件名来获取程序进程和窗口句柄的代码(转载)
- C#如何获取鼠标相对当前窗口的坐标
- C#获取进程的主窗口句柄的实现方法
- 获取鼠标点窗口句柄
- C#中出现了一个问题,就是程序主窗体已经打开,在调用子窗体时候出现了创建窗口句柄时出错。
- python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过autopy实现右键菜单和另存为操作
- C# 获取指定进程的主窗口句柄
- [转载]C#获取进程的主窗口句柄
- 用鼠标获取任意窗口的句柄, 并把它当作"干儿子"