C#获取控制台句柄的方法
1.创建类User32API.cs,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
namespace XXX
{
class User32API
{
private static Hashtable processWnd = null;
public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
static User32API()
{
if (processWnd == null)
{
processWnd = new Hashtable();
}
}
[DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
[DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
[DllImport("user32.dll", EntryPoint = "IsWindow")]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("kernel32.dll", EntryPoint = "SetLastError")]
public static extern void SetLastError(uint dwErrCode);
public static IntPtr GetCurrentWindowHandle()
{
IntPtr ptrWnd = IntPtr.Zero;
uint uiPid = (uint)System.Diagnostics.Process.GetCurrentProcess().Id; // 当前进程 ID
object objWnd = processWnd[uiPid];
if (objWnd != null)
{
ptrWnd = (IntPtr)objWnd;
if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) // 从缓存中获取句柄
{
return ptrWnd;
}
else
{
ptrWnd = IntPtr.Zero;
}
}
bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
// 枚举窗口返回 false 并且没有错误号时表明获取成功
if (!bResult && Marshal.GetLastWin32Error() == 0)
{
objWnd = processWnd[uiPid];
if (objWnd != null)
{
ptrWnd = (IntPtr)objWnd;
}
}
return ptrWnd;
}
private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
{
uint uiPid = 0;
if (GetParent(hwnd) == IntPtr.Zero)
{
GetWindowThreadProcessId(hwnd, ref uiPid);
if (uiPid == lParam) // 找到进程对应的主窗口句柄
{
processWnd[uiPid] = hwnd; // 把句柄缓存起来
SetLastError(0); // 设置无错误
return false; // 返回 false 以终止枚举窗口
}
}
return true;
}
}
}
2.调用类User32API获取控制台句柄的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XXXX
{
class XXX
{
static void Main(string[] args)
{
IntPtr hwnd=User32API.GetCurrentWindowHandle();//获取句柄
Console.WriteLine("{0} ", hwnd);
Console.ReadLine();
}
}
}
C#获取控制台句柄的方法相关推荐
- linux获取控制台窗口句柄,C#获取控制台句柄的方法
1.创建类User32API.cs,代码如下: using System; using System.Collections.Generic; using System.Linq; using Sys ...
- VC 获取控制台窗体的句柄(hWnd)
在Windows中,句柄是外国话,理解为编号的意思,只要你知道了这个窗口的句柄,就可以对这个窗体进行操作--移动窗体.改变窗体大小.把窗体极小化为图标等.Windows API函数中的句柄代表一些特殊 ...
- 获取控制台窗口的句柄(HWND)和程序中输出回车键
一.获取窗口句柄基于类名或窗口名称: http://support.microsoft.com/kb/124103/zh-cn Win32 API 提供用于获取与一个控制台应用程序相关联的窗口句柄没有 ...
- python流获取控制台_对Python捕获控制台输出流的方法详解
有时候我们的代码里可能要调用控制台命令,比如我想用Python写一个批量编译 .java 文件的脚本,用到如下代码 常规用法 os.system import os,traceback try: p ...
- python获取窗口句柄_Python+selenium 获取浏览器窗口坐标、句柄的方法
1.0 获取浏览器窗口坐标 python目录可找到Webdriver.py 文件定义了get_window_rect()函数,可获取窗口的坐标和大小(长宽),但出现"Command not ...
- c语言 取窗口句柄,MFC获取句柄的方法
句柄获取方法(获取该窗口的句柄后,即可向该窗口类类发送消息.处理程序): 例如:::PostMessage("句柄", WM_USER_DRAWVIEWPIC_EDS, 0, 0) ...
- python 获取浏览器句柄下的网页控件,Python获取浏览器窗口句柄过程解析
句柄(handle)是C++程序设计中经常提及的一个术语.它并不是一种具体的.固定不变的数据类型或实体,而是代表了程序设计中的一个广义的概念.句柄一般是指获取另一个对象的方法--一个广义的指针,它的具 ...
- python 获取浏览器句柄下的网页控件_python webdriver操作浏览器句柄
断言 assert self.driver.title.find(u"搜狗搜索引擎")>=0, "assert error" 浏览器后退,前进,前进前要先 ...
- JCEF3——谷歌浏览器内核Java版实现(一):使用jawt获取窗体句柄
前言 最近一段时间研究谷歌浏览器内核.谷歌浏览器内核一直开源,并维护更新,它的开源项目中内核更新速度和Chrome浏览器版本更新进度一样!而且它不同于WebKit(值得一题的是谷歌浏览器已不使用Web ...
最新文章
- 欧锦赛球星谱:帕克领豪阵 诺天王对决加索尔
- CSS分别设置Input样式(按input类型)
- sap.ca.ui.utils.busydialog - scenario1 - opportunity opened
- python最大分词_中文分词算法之最大正向匹配算法(Python版)
- python库测试记录(sys、time、datetime、random、hashlib)
- C语言变量声明问题——变量定义一定要放在所有执行语句/语句块的最前面吗?
- 我的2013——不平凡的第一次
- linux centos Intel® Centrino® Wireless-N 1000 无线网卡的安装
- 设计模式 - 模板方法设计模式
- 不理解 Java Steam?一步步梳理其工作方式
- mac Navicat安装提示’已损坏‘,解决方案
- [Android实例] Android有效解决加载大图片内存溢出问题及优化虚拟机内存
- java猜数游戏图形界面_java 猜数字游戏 swing实现带界面
- wpf如何实现按钮的回车事件_c# – 在WPF中模拟Enter键
- html css做一个简历表,HTML table制做我的简历
- 使用canvas画三角形,圆形,半圆,八卦
- python interpreter 中没有torch_python自动化办公之 Python 解析 PDF
- getAttribute()用法
- 【转】外连接&全连接的区别
- 2019.6.14 巴塞尔问题