C#怎样获得窗口句柄

C#怎样获得窗口句柄,第1张

一个网上的例子

c# 获取鼠标处窗口句柄,程序嵌入桌面

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);//设置父窗体,第一个为要被设置的窗口,第二个参数为指定其父窗口句柄

}

}

}

用CWnd::findWindow(参数,参数);

函数:

static CWnd PASCAL FindWindow(

LPCTSTR lpszClassName,

LPCTSTR lpszWindowName

);

具体用法自己查MSDN,窗口的信息可以用Spy++这个工具,在 VS工具 里

每一个从Cwnd类派生出来的类都有一个成员变量m_hWnd,即指向当前窗口的句柄,直接使用它即可。 其中从CWnd类派生出来的类有CFrameWnd、CDialog、CView、CControlBar等等。

以下模块:

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Private Type RECT

Left As Long

Top As Long

Right As Long

Bottom As Long

End Type

'自定义数据类型

Public Type mType

fhwnd As Long '窗口句柄

fText As String 255 '窗口标题

fRect As RECT '窗口矩形

pHwnd As Long '父窗句柄

pText As String 255 '父窗标题

End Type

'获取控件信息,写成SUB了,其实用FUNCTION返回值也可以,只是函数里面定义就多了,总的来看需要2个mType数组,这样做只需要一个,占用空间小了

Public Sub mGetAllWindow(m_Type() As mType)

Dim Wndback As Long '上一个被查找的目标句柄

Dim i As Long '数组控制

Do

ReDim Preserve m_Type(i)

DoEvents

'获取hwnd,第一个参数指定为0,查找桌面子窗口,第2个参数是开始查找的窗口,第34个参数使函数查找所有窗口

m_Type(i)fhwnd = FindWindowEx(0, Wndback, vbNullString, vbNullString)

If m_Type(i)fhwnd = 0 Then '=0时已经查找一遍了,退出

Exit Sub

Else '否则获取控件相关消息

'获取标题

GetWindowText m_Type(i)fhwnd, m_Type(i)fText, 255

'获取RECT

GetWindowRect m_Type(i)fhwnd, m_Type(i)fRect

'获取父HWND

m_Type(i)pHwnd = GetParent(m_Type(i)fhwnd)

'获取父标题

GetWindowText m_Type(i)pHwnd, m_Type(i)pText, 255

End If

Wndback = m_Type(i)fhwnd '保存上一个查的句柄

i = i + 1

Loop

End Sub

以下在窗体:(添加一个LISTVIEW1,一个COMMAND1)

Private Sub Command1_Click()

Dim cType() As mType

mGetAllWindow cType()

Dim i As Long

ListView1ListItemsClear

For i = LBound(cType) To UBound(cType)

ListView1ListItemsAdd , "a" & i, cType(i)fhwnd

ListView1ListItems("a" & i)SubItems(1) = cType(i)fText

ListView1ListItems("a" & i)SubItems(2) = cType(i)fRectLeft

ListView1ListItems("a" & i)SubItems(3) = cType(i)fRectBottom

ListView1ListItems("a" & i)SubItems(4) = cType(i)fRectTop

ListView1ListItems("a" & i)SubItems(5) = cType(i)fRectRight

ListView1ListItems("a" & i)SubItems(6) = cType(i)pHwnd

ListView1ListItems("a" & i)SubItems(7) = cType(i)pText

Next

End Sub

Private Sub Form_Load()

ListView1ColumnHeadersAdd , , "句柄", 1200

ListView1ColumnHeadersAdd , , "标题", 2800

ListView1ColumnHeadersAdd , , "RectLeft", 800

ListView1ColumnHeadersAdd , , "RectBottom", 800

ListView1ColumnHeadersAdd , , "RectTop", 800

ListView1ColumnHeadersAdd , , "RectRight", 800

ListView1ColumnHeadersAdd , , "父窗句柄", 1200

ListView1ColumnHeadersAdd , , "父窗标题", 2800

ListView1View = lvwReport

ListView1FullRowSelect = True

Command1Caption = "刷新"

End Sub

以上就是关于C#怎样获得窗口句柄全部的内容,包括:C#怎样获得窗口句柄、如何用c++实现获取桌面的具体图标的句柄、KDE环境下如何如何获取一个窗体句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9585281.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存