分享
 
 
 

C#控制Windows Messenger和Windows Live Messenger窗口发送消息

王朝c#·作者佚名  2008-05-30
窄屏简体版  字體: |||超大  

前端时间做了Messenger助手,后来发现只支持Windows Messenger,不支持Windows Live Messenger,最近改了一下,用到了Active Accessibility编程,代码如下:

using System;

using Accessibility;

namespace MessengerHelper

{

/**//// <summary>

/// 对Messenger窗口进行操作

/// </summary>

public class MessengerWindowHelper

{

IntPtr _messengerWindowHandle ;

IntPtr _inputBoxHandle ;

IntPtr _submitButtonHandle ;

IAccessible _inputBox ;

IAccessible _submitButton ;

private MessengerWindowHelper(){}

public MessengerWindowHelper(IntPtr windowHandle)

{

_messengerWindowHandle = windowHandle ;

_inputBoxHandle = GetInputBoxHandle() ;

_submitButtonHandle = GetSubmitButton() ;

if (_inputBoxHandle == IntPtr.Zero && _submitButtonHandle == IntPtr.Zero)

{

GetAccessibleObjects(_messengerWindowHandle, out _inputBox, out _submitButton);

}

}

/**//// <summary>

/// 输入消息

/// </summary>

/// <param name="message"></param>

public void InputMessage(string message)

{

if (_inputBox == null)

{

Win32.SendMessageString(_inputBoxHandle, Win32.WM_SETTEXT, IntPtr.Zero, message);

}

else

{

_inputBox.set_accValue(Win32.CHILDID_SELF, message) ;

}

}

/**//// <summary>

/// 发送消息

/// </summary>

public void SendMessage()

{

if (_submitButton == null)

{

Win32.SendMessageInt(_submitButtonHandle, Win32.WM_CLICK, IntPtr.Zero, 0);

}

else

{

_submitButton.accDoDefaultAction(Win32.CHILDID_SELF) ;

}

}

private IntPtr GetInputBoxHandle()

{

IntPtr topInputHandle = Win32.FindWindowEx(_messengerWindowHandle, System.IntPtr.Zero, "RichEdit20W", null) ;

return Win32.FindWindowEx(_messengerWindowHandle, topInputHandle, "RichEdit20W", null) ;

}

private IntPtr GetSubmitButton()

{

return Win32.FindWindowEx(_messengerWindowHandle, System.IntPtr.Zero, "Button", "发送(&S)") ;

}

private object[] GetAccessibleChildren(IAccessible paccContainer)

{

object[] rgvarChildren = new object[paccContainer.accChildCount] ;

int pcObtained ;

Win32.AccessibleChildren(paccContainer,0,paccContainer.accChildCount, rgvarChildren, out pcObtained);

return rgvarChildren ;

}

private void GetAccessibleObjects(System.IntPtr imWindowHwnd, out IAccessible inputBox, out IAccessible submitButtion)

{

System.IntPtr ptrUIHWND = Win32.FindWindowEx(imWindowHwnd, System.IntPtr.Zero, "DirectUIHWND", 0);

Guid guidCOM = new Guid(0x618736E0,0x3C3D,0x11CF,0x81,0xC,0x0,0xAA,0x0,0x38,0x9B,0x71);

Accessibility.IAccessible IACurrent = null;

Win32.AccessibleObjectFromWindow(ptrUIHWND,(int)Win32.OBJID_CLIENT,ref guidCOM,ref IACurrent);

IACurrent = (IAccessible)IACurrent.accParent;

int childCount = IACurrent.accChildCount;

object[] windowChildren = new object[childCount];

int pcObtained;

Win32.AccessibleChildren(IACurrent, 0, childCount, windowChildren, out pcObtained);

inputBox = null ;

submitButtion = null ;

string accName ;

int accRole ;

foreach(IAccessible child in windowChildren)

{

accRole = (int)child.get_accRole(Win32.CHILDID_SELF) ;

accName = child.get_accName(Win32.CHILDID_SELF) ;

if (accRole == 10)

{

object[] clientChilren = GetAccessibleChildren(child) ;

IAccessible client = (IAccessible)clientChilren[0] ;

clientChilren = GetAccessibleChildren(client) ;

foreach (IAccessible childChild in clientChilren)

{

accRole = (int)childChild.get_accRole(Win32.CHILDID_SELF) ;

accName = childChild.get_accName(Win32.CHILDID_SELF) ;

if (accRole == 42 && accName == "输入")

{

inputBox = childChild ;

}

if (accRole == 43 && accName == "发送按钮")

{

submitButtion = childChild ;

}

if (inputBox != null && submitButtion != null)

{

break ;

}

}

break ;

}

}

}

}

}

using System;

using System.Runtime.InteropServices;

using Accessibility;

namespace MessengerHelper

{

/**//// <summary>

/// 调用Window API

/// </summary>

public class Win32

{

public const int WM_SETTEXT = 0x000C;

public const int WM_CLICK = 0x00F5;

public const int CHILDID_SELF = 0;

public const int CHILDID_1 = 1;

public const int OBJID_CLIENT = -4;

[DllImport("User32.dll")]

public static extern Int32 FindWindow(String lpClassName,String lpWindowName);

[DllImport("user32.dll", CharSet=CharSet.Auto)]

public static extern IntPtr FindWindowEx(

IntPtr parentHandle,

IntPtr childAfter,

string lpszClass,

int sWindowTitle /**//*HWND*/);

[DllImport("user32.dll", SetLastError = true)]

public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

[DllImport("user32.dll", EntryPoint="SendMessage")]

public static extern int SendMessageString (IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

[DllImport("user32.dll", EntryPoint="SendMessage")]

public static extern int SendMessageInt (IntPtr hwnd, int wMsg, IntPtr wParam, int lParam);

[DllImport("Oleacc.dll")]

public static extern int AccessibleObjectFromWindow(

IntPtr hwnd,

int dwObjectID,

ref Guid refID,

ref IAccessible ppvObject);

[DllImport("Oleacc.dll")]

public static extern int WindowFromAccessibleObject(

IAccessible pacc,

out IntPtr phwnd);

[DllImport("Oleacc.dll")]

public static extern int AccessibleChildren(

Accessibility.IAccessible paccContainer,

int iChildStart,

int cChildren,

[Out] object[] rgvarChildren,

out int pcObtained);

}

}

由于微软出的Messenger产品及版本繁多,而且插件也不少,所以兼容性不是很强,实用性不大,但是,个人觉得代码还是有一些借鉴作用。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有