查找窗口并更改其名称 [英] Find Window and change it's name

查看:126
本文介绍了查找窗口并更改其名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚餐

我想做的是一种找到窗口的方法,尽管它是进程ID,但要获取该窗口的名称并将其更改为我想要的名称.

What i'm trying to do is a way to find a window though it's process ID, get the name of that window and change it to something that i want.

我发现了一些相关内容:(代码1)

I found some things about that : ( Code 1 )

 int WINAPI GetWindowText(
   _In_  HWND   hWnd,
   _Out_ LPTSTR lpString,
   _In_  int    nMaxCount
    );

此:(代码2)

    CWnd* pWnd = GetDlgItem(); GetDlgIt
pWnd->SetWindowText(_T("WindowName"));


CString str;
pWnd->GetWindowText(str);
ASSERT(str == _T("WindowName"));

还有这个(代码3)

    HWND WindowHandel = FindWindowA(0, "WindowName"); 

    DWORD proccesID = 0

    GetWindowThreadProcessId(WindowHandel, &proccesID);

我的问题是: 我如何获取进程ID,获取该进程的窗口名称,然后输入以便代码可以将Hwnd识别为我的窗口名称,并将其更改为所需的名称. 像这样的东西:

My questions are : How can i get the Process ID, get the window name of that process, put that so the codes can recognize Hwnd as my window name and change it for something that i want. Something like this :

Process = "anyprocess.exe"
Get Process ID <
Process ID = 1234567
Get window name from the ProcessID we have <
Window name = "ILoveYou"
Change "ILoveYou" to "IHaveYou"

如果我有多个同名窗口,则有一种方法可以检查第一个打开的窗口,以便我不更改其他窗口?

And if i have more then one window with the same name, theres a way to check the first one opened so i don't change the other windows ?

在我的脑海中,从进程ID中获取WindowName似乎更安全,问题是我不想对我知道窗口名称的代码说,我希望它能够像Liam Neeson一样找到并更改它对那些坏家伙做了.

In my head, taking the WindowName from the process id seems safer, the thing is that i don't wanna say to the code that i know the window name, i want it to find it and change it just like Liam Neeson did with those bad guys.

我还想了解更多有关使用那些(0,...."或(NULL,....")的信息,"FindWindowA"命令只是一个例子,我总是看到这一点,并且不知道如何正确使用它:

And i also would like to know more about using those "(0,...." or "(NULL,....", the "FindWindowA" command was just an example, i see this always and don't know how to use it properly :

    FindWindowA(0, "WindowName")

我想要的例子对我有很大帮助=) 感谢您的耐心等候.

An example of what i want would help me a lot =) Thanks for your patience.

推荐答案

否,无法完成.一个以上的窗口可以具有相同的PID.

No, this cannot be done. More than one window can have the same PID.

但是,假设目标应用程序是只有一个可见窗口的单线程应用程序,则可以浏览所有窗口并检查其PID以找到匹配项.您必须跳过不可见的窗口.或者更好的是,跳过非Alt-Tab窗口.这是完成的过程.

However, assuming that the target application is single-threaded with only one visible window, you can go through all the windows and check their PID to find a match. You have to skip the invisible windows. Or better yet, skip non Alt-Tab windows. Here is how it's done.

注意,我只是为了好玩而写,我不会自己将它放在任何应用程序中.

Note, I just wrote this for fun, I wouldn't put this in any application myself.

BOOL IsAltTabWindow(HWND hwnd)
{
    if (!IsWindowVisible(hwnd)) return FALSE;

    HWND next = NULL;
    HWND parent = GetAncestor(hwnd, GA_ROOTOWNER);
    while (parent != next)
    {
        next = parent;
        parent = GetLastActivePopup(parent);
        if (IsWindowVisible(parent)) break;
    }
    if (next != hwnd) return FALSE;

    TITLEBARINFO ti = { 0 };
    ti.cbSize = sizeof(ti);
    GetTitleBarInfo(hwnd, &ti);
    if (ti.rgstate[0] & STATE_SYSTEM_INVISIBLE) return FALSE;

    if (GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) return FALSE;

    return TRUE;
}

BOOL CALLBACK enumProc(HWND hwnd, LPARAM lp)
{
    DWORD search_pid = DWORD(lp);
    if (!IsAltTabWindow(hwnd))
        return 1;
    DWORD pid = 0;
    GetWindowThreadProcessId(hwnd, &pid);
    if (pid != search_pid) return 1;
    SetWindowText(hwnd, "un-advised code");
    return 0;
}

int main()
{
    DWORD search_pid = 0;
    HWND hwnd = FindWindow(0, "Find me");
    GetWindowThreadProcessId(hwnd, &search_pid);

    EnumWindows(enumProc, LPARAM(search_pid));

    return 0;
}


编辑,说明:


Edit, explanation:

尝试下面的代码.首先运行Window的记事本,确保只有一个实例.

Try the code below. First run Window's notepad, make sure there is only one instance of notepad.

如果您在Windows 10上运行以下代码,它将不显示一个窗口,而是显示3个与记事本具有相同PID的窗口.这些窗口中有2个是不可见的,它们由操作系统使用,我们不应该触摸它们.

If you run the code below on Windows 10, it will show not one, but 3 windows with the same PID as Notepad. 2 of those windows are invisible, they are used by operating system, we are not supposed to touch those.

因此,我们至少要跳过不可见的窗口.还有许多其他系统窗口,我们也不希望与它们有任何关系.

So we want to skip invisible windows, at the very least. There are a number of other system windows, we don't want anything to do with them either.

IsAltTabWindow是避免这些窗口的好方法.这样可以确保我们只浏览可以通过Alt-Tab键访问的窗口.

IsAltTabWindow is good way to avoid those windows. This makes sure we only look through windows which can be accessed through Alt-Tab key.

int main()
{
    HWND hwnd = FindWindow("Notepad", 0);
    if (!hwnd) return 0;
    DWORD search_pid = 0;
    GetWindowThreadProcessId(hwnd, &search_pid);

    for (hwnd = GetWindow(GetDesktopWindow(), GW_CHILD); 
        hwnd; hwnd = GetWindow(hwnd, GW_HWNDNEXT))
    {
        char buf[300];
        GetWindowText(hwnd, buf, 300);

        DWORD pid = 0;
        GetWindowThreadProcessId(hwnd, &pid);
        if (pid == search_pid)
            std::cout << pid << "\n";
    }

    return 0;
}

这篇关于查找窗口并更改其名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆