using System;
using SystemCollectionsGeneric;
using SystemText;
using SystemDiagnostics;
using SystemRuntimeInteropServices;
namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// 激活并前置
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
[DllImport("user32dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// 显示窗体
/// </summary>
/// <param name="hwnd"></param>
/// <param name="nCmdShow"></param>
/// <returns></returns>
[DllImport("user32dll", CharSet = CharSetAuto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
//nCmdShow的含义
//0 关闭窗口
//1 正常大小显示窗口
//2 最小化窗口
//3 最大化窗口
static void Main(string[] args)
{
ConsoleTitle = "通过进程寻找记事本 显示 激活";
while (true)
{
ConsoleWriteLine("按字母g试试");
string s = ConsoleReadLine();
if (s == "g")
doSomeThing();
}
}
/// <summary>
/// 做点什么
/// </summary>
static void doSomeThing()
{
Process[] myprocess = ProcessGetProcessesByName("notepad");
foreach (Process p in myprocess)
{
IntPtr hWnd = pMainWindowHandle;
ShowWindow(hWnd, 1);
SetForegroundWindow(hWnd);
}
}
}
}
句柄,是整个Windows编程的基础。一个句柄是指使用的一个唯一的整数值,即一个4字节(64位程序中为8字节)长的数值,来标识应用程序中的不同对象和同类中的不同的实例,诸如,一个窗口,按钮,图标,滚动条,输出设备,控件或者文件等。
通俗的来讲,句柄是一个ID。我们可以利用这个ID *** 作这个ID所绑定的空间,对象等。
比较常见的句柄,比如:HINSTANCE(实例句柄),HBITMAP(位图句柄),HDC(设备描述表句柄),HICON(图标句柄)等等。
我们利用这些句柄 *** 作对象,,设备等等。
[DllImport("user32dll", EntryPoint = "FindWindow", CharSet = CharSetAuto)]
static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32dll", EntryPoint = "FindWindowEx", CharSet = CharSetAuto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[STAThread]
static void Main(string[] args)
{
string path = "\\\\\\AUT3\\bin\\Debug\\AUT3exe";
Process p = ProcessStart(path);
if (p==null)
ConsoleWriteLine("Warning:process may already exist");
ConsoleWriteLine("Finding main window handle");
IntPtr mwh = FindMainWindowHandle("Form1", 100, 25);
ConsoleWriteLine("Handle to main window is " + mwh);
//有名字控件句柄
ConsoleWriteLine("Findding handle to textbox1");
IntPtr tb = FindWindowEx(mwh, IntPtrZero, null, "<enter color>");
if (tb == IntPtrZero)
throw new Exception("Unable to find textbox1");
else
ConsoleWriteLine("Handle to textbox1 is " + tb);
ConsoleWriteLine("Findding handle to button1");
IntPtr butt = FindWindowEx(mwh, IntPtrZero, null, "button1");
if (butt == IntPtrZero)
throw new Exception("Unable to find button1");
else
ConsoleWriteLine("Handle to button1 is " + butt);
//没有名字或者重名控件
ConsoleWriteLine("Findding handle to listbox1");
IntPtr lb = FindWindowByIndex(mwh,3);
if (lb == IntPtrZero)
throw new Exception("Unable to find listbox1");
else
ConsoleWriteLine("Handle to listbox1 is " + lb);
}
方法:
//通过索引查找相应控件句柄
static IntPtr FindWindowByIndex(IntPtr hwndParent,int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtrZero;
do
{
result = FindWindowEx(hwndParent,result,null,null);
if (result != IntPtrZero)
{
++ct;
}
} while (ct<index&&result!=IntPtrZero);
return result;
}
}
//获得待测程序主窗体句柄
private static IntPtr FindMainWindowHandle(string caption,int delay,int maxTries)
{
IntPtr mwh = IntPtrZero;
bool formFound = false;
int attempts = 0;
while (!formFound && attempts < maxTries)
{
if (mwh == IntPtrZero)
{
ConsoleWriteLine("Form not yet found");
ThreadSleep(delay);
++attempts;
mwh = FindWindow(null, caption);
}
else
{
ConsoleWriteLine("Form has been found");
formFound = true;
}
}
if (mwh == IntPtrZero)
throw new Exception("Could not find main window");
else
return mwh;
}
以上就是关于C#,怎么获取其他窗体对象全部的内容,包括:C#,怎么获取其他窗体对象、在VC++中 什么是句柄、如何使用C#获取VC写的程序窗口中某个按钮的句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)