一个网上的例子
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemRuntimeInteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32dll", 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("user32dll", EntryPoint = "SetParent")]//设置父窗体
public static extern int SetParent(
int hWndChild,
int hWndNewParent
);
[DllImport("user32dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
public static extern int GetCursorPos(
ref POINTAPI lpPoint
);
[StructLayout(LayoutKindSequential)]//定义与API相兼容结构体,实际上是一种内存转换
public struct POINTAPI
{
public int X;
public int Y;
}
[DllImport("user32dll", 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(pointX, pointY);//获取指定坐标处窗口的句柄
thislabel1Text =pointXToString() + ":" + pointYToString() + "-" + hwndToString();//显示效果,此时窗口已经嵌入桌面了
}
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)thisHandle, hDesktop);//设置父窗体,第一个为要被设置的窗口,第二个参数为指定其父窗口句柄
}
}
}
这个很简单啊VisualBasiccodePrivateDeclareFunctionGetParentLib"user32"Alias"GetParent"(ByValhwndAsLong)AsLongPrivateDeclareFunctionGetWindowTextLib"user32"Alias"GetWindowTextA"(ByValhwndAsLong,ByVa
第一点 后台句柄是动态的 这次你打开是这个句柄值下次打开就变了
第二点 按键抓抓只是参考数据的 没有生成代码功能
根据你的提问提供一下找色代码方式
Hwnd="" //输入句柄值 双引号去掉 例子Hwnd=123456(前提是句柄值是固定不变的)
//Hwnd = PluginWindowMousePoint() ←一般句柄值获取都是用这条代码的
颜色值十六进制=GetPixelColor(10,5)
颜色值十六进制= PluginBkgndGetPixelColor(Hwnd, 0, 0)
If 颜色值十六进制="FFFFFF" Then
MessageBox ""
End If
在VB工程里新建一个Form1
在Form1上添加一个按纽Command1 和两个标签Label1和Label2
然后在Form1代码窗口里粘贴如下代码:
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim a As Single
Dim b As Single
MeLabel1 = X
MeLabel2 = Y
End Sub
按F5运行后,鼠标晃动到按纽Command1上时,可以看到Label1和Label2里面有数值在快速变化
该数字就是鼠标在按纽上的XY坐标
Private Sub Command1_MouseMove()就是Command1的鼠标移动事件
同理:
所有拥有MouseMove事件的控件都能用这个方法取得鼠标的当前坐标
以上就是关于C#怎样获得窗口句柄全部的内容,包括:C#怎样获得窗口句柄、如何获得光标位置坐标或者是光标所在窗口的句柄、按键精灵抓制定句柄,然后抓相对点坐标,取色,然后如何生成脚本内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)