Visual Studio 2005 和 Live Messenger 最无用的插件
Visual Studio 2005 和 Live Messenger 最没用的插件。
引言
你是否曾经想在 Live Messenger 上展示你的下一个秘密项目,让你的朋友们来询问?没有什么比这个为 Visual Studio 2005 设计的没用插件更好的了,它允许你在 Live Messenger 上显示“正在编辑……”
使用代码
这段代码的重要部分如下
//Define the following in the class
private EnvDTE.WindowEvents winEvents;
//In the OnConnection function, do the following
// Retrieve the event objects from the automation model.
winEvents = (EnvDTE.WindowEvents)events.get_WindowEvents(null);
winEvents.WindowActivated +=
new _dispWindowEvents_WindowActivatedEventHandler(this.WindowActivated);
//In the OnDisconnection, make sure you clean up the event handler
winEvents.WindowActivated -=
new _dispWindowEvents_WindowActivatedEventHandler(this.WindowActivated);
//To use FindWindowExA and SendMessageA,
//you have to import those functions from user32.dll
//Do the following in the class
[DllImport("user32", EntryPoint = "SendMessageA")]
private static extern int SendMessage(int Hwnd,
int wMsg, int wParam, int lParam);
[DllImport("user32", EntryPoint = "FindWindowExA")]
private static extern int FindWindowEx(int hWnd1,
int hWnd2, string lpsz1, string lpsz2);
//And Since we use a constant for WM_COPYDATA, we define
private const short WM_COPYDATA = 74;
//This is the actual function that sends message to MSN.
//The exact format for the string can be found in MSN API section of MSDN
private void SendMSNMessage(bool enable, string category, string message)
{
string buffer = "\\0" + category + "\\0" +
(enable ? "1" : "0") +
"\\0{0}\\0" + message + "\\0\\0\\0\\0\0";
int handle = 0;
data.dwData = 0x0547;
data.lpData = VarPtr(buffer);
data.cbData = buffer.Length * 2;
handle = FindWindowEx(0, handle, "MsnMsgrUIManager", null);
if (handle > 0)
SendMessage(handle, WM_COPYDATA, 0, VarPtr(data));
}
//Finally the event handling function.
public void WindowActivated(EnvDTE.Window gotFocus,EnvDTE.Window lostFocus)
{
SendMSNMessage(true, "Music","Coding in Visual Studio: " +
gotFocus.Caption);
}
虽然这篇文章很短,但代码也很短,这对于那些对开发和扩展 VS2005 感兴趣的人来说非常棒。我用了3个小时完成了这段代码,包括学习 VS2005 的插件架构;我相信你能做得更好。
更多细节,请查看 Visual Studio 2005 SDK,可以从微软网站免费下载。祝你好运,玩得开心!
关注点
这个插件中有一些你可能感兴趣的地方
- 如何在 C# 中发送窗口消息
- 如何与 MSN Messenger 的 API 通信
- 如何为 Visual Studio 2005 编写插件
- 如何在 Visual Studio 2005 中处理事件
历史
- 版本 1.0 - 仅供你个人使用。