通过枚举窗口获得窗口句柄名字并重命名窗口

创建一个对话框工程,调用回调函数EnumWindowsProc枚举所有窗口存入数组m_hwndFind中,修改ONOK函数,获得窗口名字并改名

// RenWinDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RenWin.h"
#include "RenWinDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CAboutDlg dialog used for App About
int m_num = 0 ;
HWND hwnd;
HWND m_hwndFind[100] = { NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
TCHAR m_store[MAX_PATH];
class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CRenWinDlg dialog

CRenWinDlg::CRenWinDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRenWinDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRenWinDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRenWinDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRenWinDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRenWinDlg, CDialog)
//{{AFX_MSG_MAP(CRenWinDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CRenWinDlg message handlers

BOOL CRenWinDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CRenWinDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CRenWinDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CRenWinDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CRenWinDlg::OnOK()
{
// TODO: Add extra validation here

//CDialog::OnOK();
{
::EnumWindows(CRenWinDlg::EnumWindowsProc,NULL);
for(int i = 0;i <=m_num;i++)
{
HWND m_wnd = m_hwndFind[i];
::GetWindowText(m_wnd,m_store,128);

if (strstr(m_store, (char *)"招商证券全能版"))
{
::SetWindowText(m_wnd, (char *)"6225");
break;
}
}
}
}
BOOL CRenWinDlg::EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
//be sure the current window is visible
if(::GetWindowLong(hWnd,GWL_STYLE)& WS_VISIBLE)
{
m_hwndFind[m_num] = hWnd;//record the HWND handle into array
m_num++;//count start
}
return 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
函数功能 在窗口列表中寻找与指定条件相符的第一个子窗口 。 该函数获得一个窗口句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数查找子窗口,从排在给定的子窗口后面的下一个子窗口开始。在查找时不区分大小写。 参数; (1)hwndParent:要查找的子窗口所在的父窗口句柄(如果设置了hwndParent,则表示从这个hwndParent指向的父窗口中搜索子窗口)。 如果hwndParent为 0 ,则函数以桌面窗口为父窗口,查找桌面窗口的所有子窗口。 Windows NT5.0 and later:如果hwndParent是HWND_MESSAGE,函数仅查找所有消息窗口。 (2)hwndChildAfter :子窗口句柄。查找从在Z序中的下一个子窗口开始。子窗口必须为hwndParent窗口的直接子窗口而非后代窗口。如果HwndChildAfter为NULL,查找从hwndParent的第一个子窗口开始。如果hwndParent 和 hwndChildAfter同时为NULL,则函数查找所有的顶层窗口及消息窗口。 (3)lpszClass:指向一个指定了类名的空结束字符串,或一个标识类名字符串的成员的指针。如果该参数为一个成员,则它必须为前次调用theGlobaIAddAtom函数产生的全局成员。该成员为16位,必须位于lpClassName的低16位,高位必须为0。 (4)lpszWindow:指向一个指定了窗口名(窗口标题)的空结束字符串。如果该参数为 NULL,则为所有窗口全匹配。 返回值 Long,找到的窗口句柄。如未找到相符窗口,则返回零。会设置GetLastError 如果函数成功,返回值为具有指定类名和窗口名的窗口句柄。如果函数失败,返回值为NULL。 若想获得更多错误信息,请调用GetLastError函数。
### 回答1: 在Visual Basic中,可以通过使用Windows API函数来获指定进程名称的窗口句柄。下面是一个实现该功能的示例代码: ```vb Imports System.Runtime.InteropServices Public Class Form1 ' 导入Windows API函数 <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr End Function <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer End Function ' 定义方法来获进程窗口句柄 Private Function GetWindowHandleByProcessName(ByVal processName As String) As IntPtr Dim processes As Process() = Process.GetProcessesByName(processName) If processes.Length > 0 Then ' 获第一个匹配进程的窗口句柄 Return processes(0).MainWindowHandle End If Return IntPtr.Zero End Function Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim processName As String = "notepad" Dim windowHandle As IntPtr = GetWindowHandleByProcessName(processName) If windowHandle <> IntPtr.Zero Then Dim windowTitle As New StringBuilder(256) GetWindowText(windowHandle, windowTitle, windowTitle.Capacity) MessageBox.Show("找到进程名为 '" + processName + "' 的窗口句柄:" + windowHandle.ToString() + vbCrLf + "窗口标题:" + windowTitle.ToString()) Else MessageBox.Show("未找到进程名为 '" + processName + "' 的窗口句柄") End If End Sub End Class ``` 在上面的示例中,`FindWindow`用于查找带有指定类名和窗口名称的顶层窗口句柄,`GetWindowText`用于获指定窗口的标题文本。通过使用`Process.GetProcessesByName()`方法获指定进程名称的进程列表,然后从中获第一个进程的主窗口句柄,即可获得所需的窗口句柄。 ### 回答2: 在VB中,我们可以通过进程名获得窗口句柄,具体步骤如下: 1. 首先,我们需要引用Windows API函数,以便使用相关的函数和结构体。在VB中,可以使用Declare语句引用API函数。 ```vb Declare Function FindWindowExA Lib "user32" (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long ``` 2. 接下来,我们可以通过进程名遍历系统中的进程,找到目标进程的窗口句柄。我们使用FindWindowExA函数,该函数通过父窗口句柄、子窗口句柄、类名和窗口标题来查找窗口。 ```vb Dim hWnd as Long Dim hWndProcess as Long Dim procID as Long hWnd = FindWindowExA(0, 0, vbNullString, "目标进程窗口标题") ``` 3. 一旦我们找到窗口句柄,我们可以使用GetWindowThreadProcessId函数获进程ID。 ```vb GetWindowThreadProcessId hWnd, procID ``` 4. 最后,我们可以使用进程ID和窗口句柄进行进一步的操作,例如向窗口发送消息、操纵窗口等。 需要注意的是,通过进程名获得窗口句柄可能存在一些限制。因为一个进程可以创建多个窗口,而窗口标题也可能不唯一,所以在使用FindWindowExA函数时,我们需要确保目标窗口的标题是唯一的,以免出现误判的情况。 总之,通过以上步骤,我们可以在VB中通过进程名获得窗口句柄,并且可以进一步使用该句柄进行相关操作。 ### 回答3: 在VB中,我们可以通过以下步骤来通过进程名获窗口句柄。 首先,我们需要使用System.Diagnostics命名空间中的Process类来获已经运行的所有进程的详细信息。我们可以使用Process.GetProcessesByName方法来获指定进程名的进程对象数组。 接下来,我们可以遍历这个进程对象数组,通过使用Process.MainWindowTitle属性来获进程的主窗口标题,并与我们想要的进程名进行比较。 一旦找到匹配的进程,我们可以使用Process.MainWindowHandle属性来获窗口句柄。这个句柄可以用于后续的窗口操作。 下面是一个通过进程名获窗口句柄的示例代码: ``` Imports System.Diagnostics Imports System.Runtime.InteropServices Public Class Form1 <DllImport("user32.dll", SetLastError:=True)> Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr End Function Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim procName As String = "Notepad" ' 要查找的进程名 Dim processes() As Process = Process.GetProcessesByName(procName) If processes.Length > 0 Then Dim mainWindowHandle As IntPtr = processes(0).MainWindowHandle ' 使用窗口句柄进行后续操作 MessageBox.Show("窗口句柄:" & mainWindowHandle.ToString()) Else MessageBox.Show("未找到指定进程") End If End Sub End Class ``` 以上是一个基本的示例,你可以根据实际需求进行扩展和修改,例如通过进程名模糊匹配等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值