Category Archives: Winforms

While you were sleeping…

I just came across an interesting use case which I thought I’d share – why let your application churn away doing unnecessary processing if the user isn’t paying attention?

So I started looking into detecting whether the workstation is locked, or if the screensaver is running – two fairly reliable ways of telling that the user has gone walkabout…

To detect a locked workstation, you need to listen out for:

Microsoft.Win32.SystemEvents.SessionSwitch

…which fires each time the user locks or unlocks the workstation.

To detect the state of the screensaver, you have to do slightly more work – there are no helpful events – instead you need some sort of polling mechanism to call away to a Win32 function:

private const int SPI_GETSCREENSAVERRUNNING = 0x0072;
[DllImport("user32.dll", SetLastError=true)]
static extern bool SystemParametersInfo(int action, int param, ref int retval, int updini);

int active = 1;
SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref active, 0);
bool screenSaverActive = (active == 1);

So I stitched these 2 bits of logic together into a nice helper class and now I can tell (within reason) when my users are there or not, and give our servers a break every now and then.

Leave a comment

Filed under UI Hints, Useful, Winforms, WPF

Click-through : ToolStrip activation for defocused apps

I’m currently building a multi-process desktop application suite that has a “launch bar” tool to start up instances of all the available widgets. A simple Winforms ToolStrip control seemed the obvious choice for  creating a nice consistent strip of buttons, but I came across the issue that if the launch bar did not have focus, then it took 2 clicks to launch a widget when clicking on a button – one to activate the application and one to click the button.

Having Googled extensively I found that this interaction is not in fact a bug, but actually designed behaviour. I would normally accept this and move on, but I found it particularly inconsistent that the ToolStrip buttons actually illuminate/highlight on mouseover even when the app doesn’t have focus – thus fooling the user into thinking that they need only click once.

Finally,I found a few helpful articles – especially this one by Rick Brewster. The solution lies in overriding the WndProc method for the ToolStrip (or MenuStrip):

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (this.clickThrough &&
m.Msg == NativeConstants.WM_MOUSEACTIVATE &&
m.Result == (IntPtr)NativeConstants.MA_ACTIVATEANDEAT)
{
m.Result = (IntPtr)NativeConstants.MA_ACTIVATE;
}
}

Leave a comment

Filed under C#, UI Hints, Winforms

WANTED: Winforms Recipes

Having spent the best part of 7 years working in C# Winforms, it’s still not 100% clear which is the best way to handle a user’s interaction with a humble TextBox. The .Net framework offers many events that can be used to manage behaviour, but I’ve never seen any documentation stating the “Best way” or the “Right way” to do it.

Take for example a simple interaction. I have a simple textbox, and would like to perform an action once the user has typed the value and hit [Enter]. Which event should I use ? Validated, Validating, KeyPress, Leave ?

I know it all depends upon the exact scenario, but hey, Microsoft, throw us a bone!

Leave a comment

Filed under C#, UI Hints, Winforms