Introduction
Came home one day, opened MSN Messenger, all my friends were online, but "Away"..
Some 30 minutes later, a friend of mine sent me a message. He was already online for 20 minutes, yet I had no way of knowing this. Then, it struck me - "Away" is evil.
A friend of mine commented that my tool sucks because while offline (in my case, when the workstation is locked) you cannot receive messages (which you can do when "Away"). The new version of the MSN Messenger now enables receiving messages when offline, so I consider this problem solved, and my friend - a very jealous guy :)
This article combines knowledge from different CodeProject articles to create a very useful tool that satisfies a very personal need, however I hope you can relate :)
Code
The code uses a simple Windows form to intercept window messages (you may replace this by any Windows service, if needed). On loading, the form is set to not be visible and not show in the taskbar.
"Session Change" messages arrive to the form when using the WTSRegisterSessionNotification
method and is identified by constants.
Using the Messenger API, the status is set to invisible (appear offline) or online considering the session state.
Enjoy:
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;
}
}