
不记得有替代函数 ,你还是用 GetWindowRect吧
// 引用
using SystemRuntimeInteropServices;
[DllImport("user32dll")]
public static extern int GetWindowRect(IntPtr hWnd, out RECT lpRect);
// 获得客户区矩形
[DllImport("user32dll")]
public static extern int GetClientRect(IntPtr hWnd, out RECT lpRect);
// 矩形结构
[StructLayout(LayoutKindSequential)]
public struct RECT
{
int left;
int top;
int right;
int bottom;
}
// 调用
IntPtr hwnd = ; // 窗口句柄
RECT rc;
GetWindowRect(hwnd, out rc);
GetClientRect(hwnd, out rc);
因为是子窗口,所以找不到。可以用EnumChildWindows来寻找。
BOOL EnumChildWindows( HWND hWndParent,
WNDENUMPROC lpEnumFunc,
LPARAM lParam
);
1定义一个模块, 内容为:
Option Explicit
Public Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function FindWindowa Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
2 给窗口加一个按钮, 在按钮的click事件里写以下内容:
Option Explicit
Private Sub Command1_Click()
Dim hwnd As Long
hwnd = GetForegroundWindow ' FindWindowa("Notepad", "新建 文本文档txt - 记事本")
Dim str1 As String, len1 As Long
str1 = Space(255) '定义接收字串
GetWindowText hwnd, str1, 1024
Do While hwnd <> 0
hwnd = GetNextWindow(hwnd, 2) '只有2才表示找下一个窗口
len1 = GetWindowText(hwnd, str1, Len(str1))
If (InStr(1, str1, "记事", 1) > 0) Then
MsgBox "你要的窗口找到了, 它是:" + str1
Exit Sub '这一句看情况修改
End If
Loop
MsgBox "很遣憾, 没有你要找的窗口"
End Sub
3 测试, 一定会通过
1、启动VS,新建C# WinForm项目。
2、在Form1中添加4个Label控件,并布局如下。
3、在Form1中添加代码,如下。
4、完成之后,调试运行,结果如下。
注意事项:
C++不仅拥有计算机高效运行的实用性特征,同时还致力于提高大规模程序的编程质量与程序设计语言的问题描述能力。
以上就是关于c#知道窗口句柄,如何获取窗口大小全部的内容,包括:c#知道窗口句柄,如何获取窗口大小、VC++ 高手进 :findwindow函数怎么获取不到这个窗口、VB中如何使用GetNextWindow函数枚举所有窗口等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)