65.9K
CodeProject 正在变化。 阅读更多。
Home

工作站锁定时控制 MSN Messenger

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.75/5 (6投票s)

2006年7月24日

1分钟阅读

viewsIcon

47895

downloadIcon

282

检测工作站是否被锁定/解锁,并相应地将 MSN Messenger 状态设置为离线/在线。

引言

有一天回家,打开 MSN Messenger,我所有的朋友都在线,但都显示为“离开”。

大约 30 分钟后,我的一个朋友给我发了一条消息。他已经在网上 20 分钟了,但我无法知道这一点。然后,我意识到,“离开”状态很糟糕。

我的一个朋友评论说我的工具很糟糕,因为在离线状态下(在我的例子中,当工作站被锁定时),你无法接收消息(而“离开”状态下可以)。现在新的 MSN Messenger 版本允许在离线状态下接收消息,所以我认为这个问题已经解决了,我的朋友——是一个非常嫉妒的人:)

这篇文章结合了 CodeProject 上不同文章的知识,创建了一个非常有用的工具,满足了一个非常个人的需求,但我希望你也能感同身受:)

代码

该代码使用一个简单的 Windows 窗体来拦截窗口消息(如果需要,你可以将其替换为任何 Windows 服务)。加载时,窗体设置为不可见且不显示在任务栏中。

使用 WTSRegisterSessionNotification 方法时,会向窗体发送“会话更改”消息,并通过常量进行识别。

使用 Messenger API,根据会话状态将状态设置为不可见(显示离线)或在线。

尽情享受吧

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using MessengerAPI;

public class MSNLockForm : Form
{
    private const int NotifyForThisSession = 0;
    private const int SessionChangeMessage = 0x02B1;
    private const int SessionLockParam = 0x7;
    private const int SessionUnlockParam = 0x8;

    [DllImport("wtsapi32.dll")]
    private static extern bool 
            WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags);

    private void InitializeComponent()
    {
        this.Name = "LockNotificationForm";
        this.Load += new 
             System.EventHandler(this.LockNotificationForm_Load);
    }

    [DllImport("wtsapi32.dll")]
    private static extern bool 
            WTSUnRegisterSessionNotification(IntPtr hWnd);

    private bool registered = false;

    public static void Main(string[] args)
    {
        Application.Run(new MSNLockForm());
    }

    protected override void Dispose(bool disposing)
    {
        if(registered)
        {
            WTSUnRegisterSessionNotification(Handle);
            registered = false;
        }

        base.Dispose(disposing);
        return;
    }

    Messenger messenger;

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        bool haveXp = Environment.OSVersion.Platform == 
             PlatformID.Win32NT && 
             (Environment.OSVersion.Version.Major > 5 || 
             (Environment.OSVersion.Version.Major == 5 &&
             Environment.OSVersion.Version.Minor >= 1));
        if(haveXp)
            registered = WTSRegisterSessionNotification(Handle, 
                                              NotifyForThisSession);
        messenger = new MessengerClass();
        IMessengerServices services = 
           (IMessengerServices) messenger.Services;
        IMessengerService service = 
           (IMessengerService) services.PrimaryService;

        return;
    }
    protected virtual void OnSessionLock()
    {
        try
        {
            messenger.MyStatus = MISTATUS.MISTATUS_INVISIBLE;
        }
        catch
        {
        }
    }
    protected virtual void OnSessionUnlock()
    {
        try
        {
            messenger.MyStatus = MISTATUS.MISTATUS_ONLINE;
        }
        catch
        {
        }
    }

    protected override void WndProc(ref Message m)
    {
        if(m.Msg == SessionChangeMessage)
        {
            if(m.WParam.ToInt32() == SessionLockParam)
                OnSessionLock();
            else if(m.WParam.ToInt32() == SessionUnlockParam)
                OnSessionUnlock();
        }
        else
            this.Visible = false;
        base.WndProc(ref m);
        return;
    }

    private void LockNotificationForm_Load(object sender, 
                                      System.EventArgs e)
    {
        this.ShowInTaskbar = false;
    }
}
© . All rights reserved.