WPF WinAPI 编程详解(四 实例 )

 

 

 

QQ/微信定时发送消息 1--检测QQ/微信窗口

 

今天先来说说第一步:怎样检测QQ和微信窗口。

1,思路:首先获取QQ和微信的进程ID,中间要注意两个注意点

      <1>请保证要群发的QQ/微信窗口已经打开。

       <2>请确保QQ/微信群窗口是独立的。

2,编码 

 前端做一个列表进行检测和添加QQ/微信窗口。

<RGBCtrl:RGBWindow x:Class="Camew.Lottery.App.QunEditWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:RGBCtrl="clr-namespace:SoftRGB.Controls;assembly=SoftRGB.Controls"
        Title="QQ/微信群发窗口列表" Height="600" Width="400" ResizeMode="NoResize" WindowStartupLocation="CenterOwner">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Rectangle Fill="{StaticResource NavBarShadowBrush}"  StrokeThickness="0" VerticalAlignment="Bottom" Height="3" />
        <Border x:Name="MainNaviBarScrollViewer" Canvas.ZIndex="1" Background="{StaticResource NavBarBackground}" BorderBrush="{StaticResource CommonBorderBrush}" BorderThickness="0,0,0,1" Margin="0,0,0,3" Padding="5,3" Height="Auto">
            <Menu>
                <MenuItem x:Name="MenuItem_Add" Icon="/Camew.Lottery.App.Style;Component/Images/Add.png" Header="新增" Click="MenuItem_Add_Click" />
                <MenuItem x:Name="MenuItem_Scan" Icon="/Camew.Lottery.App.Style;Component/Images/Scan.png" Header="自动检测" Click="MenuItem_Scan_Click" />
            </Menu>
        </Border>
        <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <StackPanel x:Name="Panel_Content" >

            </StackPanel>
        </ScrollViewer>
        <TextBlock x:Name="TextBlock_EmptyTip" Text="请添加群名称" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="24" Foreground="DarkGray"/>
        <TextBlock Grid.Row="2" Margin="10" TextWrapping="Wrap">
            <Run  Foreground="Red" Text="注意1:请保证要群发的QQ/微信窗口已经打开。"/>
            <LineBreak/>
            <Run  Foreground="Red" Text="注意2:请确保QQ/微信群窗口是独立的。"/>
        </TextBlock>
        <Grid Grid.Row="3">
            
            <Border Style="{StaticResource Border_SubmitBack}" />
            <Border VerticalAlignment="Top"  Style="{StaticResource Border_SubmitBorder}"/>
            <StackPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Button x:Name="Button_OK"  Content="确定" Width="100"  Margin="10,10"  Click="Button_OK_Click"/>
                    <Button x:Name="Button_Cancel" Content="取消" Width="100" Margin="10,10" Click="Button_Cancel_Click"/>
                </StackPanel>
               
            </StackPanel>
           
        </Grid>
    </Grid>
</RGBCtrl:RGBWindow>

如图所示

 

如上图所示:有两个按钮新增和自动检测,点击新增会在进程列表中添加一个窗体项,在窗体项的TextBox中输入群名称,(注意名称要输入正确,有字母的要注意大小写!!!)点击自动检测按钮会把已经打开的窗体添加到进程列表,有两个已提到的前提条件: <1>请保证要群发的QQ/微信窗口已经打开。 <2>请确保QQ/微信群窗口是独立的。

最后就是把进程列表中的群名称和对应的进程号保存,以便后面进行消息发送。

后台主要代码:

    private void MenuItem_Scan_Click(object sender, RoutedEventArgs e)
        {
            this.Panel_Content.Children.Clear();

            //获取QQ和微信的进程ID
            List<int> pidList = new List<int>();
            Process[] process = Process.GetProcessesByName("QQ");
            pidList.AddRange(process.Select(x => x.Id));
            process = Process.GetProcessesByName("WeChat");
            pidList.AddRange(process.Select(x => x.Id));

            List<string> qunNames = new List<string>();

            WinAPI.User32.EnumWindows(delegate (IntPtr hwnd, int lParam)
            {
                //如果不是顶层窗口,或者窗口不可见
                if (WinAPI.User32.GetParent(hwnd) == 1 || WinAPI.User32.IsWindowVisible(hwnd) == 0)
                {
                    return true;
                }

                StringBuilder titleSB = new StringBuilder(256);
                WinAPI.User32.GetWindowText(hwnd, titleSB, titleSB.Capacity);
                if (qunNames.Contains(titleSB.ToString()))
                    return true;

                uint pid;
                WinAPI.User32.GetWindowThreadProcessId(hwnd, out pid);
                if (pidList.Contains((int)pid))
                {
                    //判断类名
                    //StringBuilder sb2 = new StringBuilder(256);
                    //WinAPI.User32.GetClassName(hwnd, sb2, sb2.Capacity);
                    //string className = sb2.ToString();
                    //if (className != "TXGuiFoundation" && className != "ChatWnd")
                    //    return true;

                    string title = titleSB.ToString();
                    if (!string.IsNullOrWhiteSpace(title) && title != "QQ" && title != "ChatContactMenu" && title != "微信" && !title.EndsWith("个会话"))
                    {
                        qunNames.Add(title);
                        AddQun(title);
                    }
                }

                return true;

            }, 0);

            if (this.Panel_Content.Children.Count == 0)
                MessageToolTip.Show("没有检测到打开的QQ/微信群窗口");
        }

 

上面的代码为“自动检测”按钮的后台操作,首先是通过 

List<int> pidList = new List<int>();
            Process[] process = Process.GetProcessesByName("QQ");
            pidList.AddRange(process.Select(x => x.Id));
            process = Process.GetProcessesByName("WeChat");
            pidList.AddRange(process.Select(x => x.Id));

 

来获取目前已经打开的QQ/微信窗口,这里用到了 

WinAPI.User32.EnumWindows(delegate (IntPtr hwnd, int lParam)
            {

}

这个API方法是枚举屏幕上所有顶层窗口,并将窗口句柄传送给应用程序定义的回调函数。

接下来在枚举循环中判断屏幕上的窗口是否为顶级窗口,是否可见。

如果符合前面标红的两个条件,则通过WinAPI.User32.GetWindowText(hwnd, titleSB, titleSB.Capacity);

 API方法来获取窗口的标题文本即为群名称。最后根据窗口句柄获取对应进程ID,判断该ID是否存在于前面的pidList,如果存在说明该窗口为QQ/微信窗口,最后只需对窗口标题进行筛选。

if (!string.IsNullOrWhiteSpace(title) && title != "QQ" && title != "ChatContactMenu" && title != "微信" && !title.EndsWith("个会话"))
标题不能为“QQ”,"ChatContactMenu","微信"和以“个会话”结尾的,为什么要进行如此筛选呢?这样做是避免QQ/微信的一些官方公众号或者管家之类的。。。

最后把符合条件的加入进程列表就好了。。。

 

API 详解 --》C# WinAPI 编程详解 (三 )

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值