?? enumqqchatwindows.cs
字號:
?/*
*--------------------------------------------------------
* Class Name(類名): EnumQQChatWindows
* Author(創(chuàng)建者): 三角貓
* Email(電子郵件): alex_zzg@163.com
* Create Time(創(chuàng)建時間): 2008/11/15 21:27:18
* CLR Version(CLR版本): 2.0.50727.312
* Copyright (c) 三角貓 www.zu14.cn
* All Rights Reserved.
* File Memo(備注):
*--------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.Serialization;
namespace QQAutoMsg
{
/// <summary>
/// 自定義異常捕獲,未做富實(shí)現(xiàn)
/// </summary>
///
[Serializable()]
public class QQAutoMsgException : System.Exception
{
public QQAutoMsgException() { }
public QQAutoMsgException(string message)
: base(message)
{ }
public QQAutoMsgException(string message, Exception innerException)
: base(message, innerException)
{ }
protected QQAutoMsgException(SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}
}
/// <summary>
/// 獲取所有已打開的QQ聊天窗口,包括群聊窗口
/// </summary>
internal static class EnumQQChatWindows
{
#region Private Variables Declaration
/// <summary>
/// 窗口列表
/// </summary>
private static List<QQChatWindow> chatWindows = new List<QQChatWindow>();
/// <summary>
/// QQ窗體的類名
/// </summary>
private const string qqChatWindowClassName = "#32770";
/// <summary>
/// 驗(yàn)證聊天窗口 與 群窗口的正則表達(dá)式
/// </summary>
private const string qqChatWindowCaptionPattern = "^與\\s.+?\\s交談中$|.+?\\s-\\s(高級){0,1}群$";
#endregion
/// <summary>
/// 保存QQ聊天窗體的句柄和標(biāo)題的結(jié)構(gòu)體
/// </summary>
internal struct QQChatWindow
{
internal string Caption;
internal IntPtr WindowHwnd;
internal QQChatWindow(IntPtr windowHwnd, string caption)
{
WindowHwnd = windowHwnd;
Caption = caption;
}
}
/// <summary>
/// 捕獲到的所有QQ聊天窗口
/// </summary>
internal static List<QQChatWindow> Windows
{
get
{
chatWindows.Clear(); //清除上次保存的窗口列表
////枚舉所有桌面窗體
if (NativeMethods.EnumDesktopWindows(IntPtr.Zero, new NativeMethods.EnumDesktopWindowsDelegate(EnumQQChatWindowsProc), IntPtr.Zero))
{
return chatWindows;
}
throw new QQAutoMsgException(System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString(System.Globalization.CultureInfo.InvariantCulture));
}
}
#region 私有內(nèi)容
/// <summary>
/// 枚舉回調(diào)函數(shù)的代理
/// </summary>
/// <param name="hWnd">窗體句柄</param>
/// <param name="lParam"></param>
/// <returns></returns>
private static bool EnumQQChatWindowsProc(IntPtr hWnd, uint lParam)
{
EnumWindowsProcByRegex(hWnd);
return true;
}
/// <summary>
/// 獲取窗體標(biāo)題
/// </summary>
/// <param name="hWnd">窗體句柄</param>
/// <returns></returns>
private static bool EnumWindowsProcByRegex(IntPtr hWnd)
{
StringBuilder caption = new StringBuilder(NativeMethods.GetWindowTextLength(hWnd) + 1);
NativeMethods.GetWindowText(hWnd, caption, caption.Capacity);
return IsQQChatWindow(hWnd, caption.ToString());
}
/// <summary>
/// 利用窗體標(biāo)題和窗體類名,準(zhǔn)確定位QQ的聊天窗體
/// </summary>
/// <param name="hWnd">窗體句柄</param>
/// <param name="caption">窗體標(biāo)題</param>
/// <returns></returns>
private static bool IsQQChatWindow(IntPtr hWnd, string caption)
{
Regex reg = new Regex(qqChatWindowCaptionPattern, RegexOptions.Singleline);
if (CompareWindowClassName(hWnd) && reg.IsMatch(caption))
{
chatWindows.Add(new QQChatWindow(hWnd, caption));
return true;
}
return false;
}
/// <summary>
/// 對比取得的窗體類名 和 QQ窗體類名,確定是否是QQ窗體
/// </summary>
/// <param name="hWnd">窗體句柄</param>
/// <returns></returns>
private static bool CompareWindowClassName(IntPtr hWnd)
{
StringBuilder className = new StringBuilder(255 + 1); //ClassName 最長255個字符
////獲取窗體類名
NativeMethods.GetClassName(hWnd, className, className.Capacity);
return className.ToString().Equals(qqChatWindowClassName);
}
#endregion
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -