获取窗口句柄

13 篇文章 0 订阅

使用FindWindow函数获取窗口句柄

  示例:使用FindWindow函数获取窗口句柄,然后获得窗口大小和标题,并且移动窗口到指定位置。


#include <Windows.h>
#include <stdio.h>
#include <string.h>

int _tmain(int argc, _TCHAR* argv[])
{
    //根据窗口名获取QQ游戏登录窗口句柄
    //HWND hq=FindWindow(NULL,_T("进制") );   
    HWND hq=FindWindow(NULL,_T("Preivew1") );   

    //得到QQ窗口大小
    RECT rect;
    GetWindowRect(hq,&rect); 

    int w=rect.right-rect.left;
    int h=rect.bottom-rect.top;

    printf("width:%d,height:%d\n",w,h);

    //移动QQ窗口位置
    MoveWindow(hq,100,100,w,h,false);

    //得到桌面窗口
    HWND hd=GetDesktopWindow();
    GetWindowRect(hd,&rect);    
    w=rect.right-rect.left;
    h=rect.bottom-rect.top;
    printf("width:%d,height:%d\n",w,h);

    return 0;
}

使用EnumWindows和EnumChildWindows函数

  使用EnumWindows和EnumChildWindows函数以及相对的回调函数EnumWindowsProc和EnumChildWindowsProc获取所有顶层窗口以及它们的子窗口(有些窗口做了特殊处理,比如QQ是不能通过这个方法获得的)

#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include <string.h>

//EnumChildWindows回调函数,hwnd为指定的父窗口
BOOL CALLBACK EnumChildWindowsProc(HWND hWnd,LPARAM lParam)
{
    TCHAR WindowTitle[100]={0};    
    ::GetWindowText(hWnd,WindowTitle,100);

    printf("child:%s\n",WindowTitle);

    return true;
}

//EnumWindows回调函数,hwnd为发现的顶层窗口
BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM lParam)
{
    if (GetParent(hWnd)==NULL && IsWindowVisible(hWnd) )  //判断是否顶层窗口并且可见
    {
        TCHAR WindowTitle[100]={0};    
        ::GetWindowText(hWnd,WindowTitle,100);
        printf("father:%s\n",WindowTitle);

        EnumChildWindows(hWnd,EnumChildWindowsProc,NULL); //获取父窗口的所有子窗口
    }

    return true;
}

int main(int argc, _TCHAR* argv[])
{
    //获取屏幕上所有的顶层窗口,每发现一个窗口就调用回调函数一次
    EnumWindows(EnumWindowsProc ,NULL );

    return 0;
}

使用GetDesktopWindow和GetNextWindow函数得到所有的子窗口

示例:

#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include <string.h>

int main(int argc, _TCHAR* argv[])
{   
    //得到桌面窗口
    HWND hd = GetDesktopWindow();

    //得到屏幕上第一个子窗口
    hd  = GetWindow(hd,GW_CHILD);
    char s[200]={0};

    //循环得到所有的子窗口
    while(hd!=NULL)
    {
        memset(s,0,200);
        GetWindowText(hd,s,200);
        /*if (strstr(s,"QQ2012"))
        {
        cout<<s<<endl;
        SetWindowText(hd,"My Windows");
        }*/
        printf("%s\n",s);

        hd  = GetNextWindow(hd,GW_HWNDNEXT);
    }
    return 0;
}

原文链接:
http://blog.csdn.net/dazhong159/article/details/7903382

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值