Archive for the ‘Tools & Techs’ Category


Lock windows when laptop lid is closed…

   Posted by: AK

Yeah right, its very simple, You can do it from control panel, Your laptop does it, I have a cheap laptop, blah blah blah…

Blv me when I say that I cannot do it on any of my 4 laptops. I can make it to sleep, hibernate or even turn off. But one thing I cannot do on any of my windows 7 laptops is to make them lock windows on closing lid.

I also thought that it should be easy, should be some option in control panel, should be some utility available if I google. But guess what, nothing at all. Nothing in entire world wide web. I am not saying that I am an expert in search but I couldn’t find anything to perform this simple task for me.

At the end, I had to write it myself. A small piece of c# code, doing just that. Nothing fancy and, to my amazement, very very easy to implement. I’ve seen that people want to do it but nobody knows how to do it. It’s not because its hard, but only because its not very well known and properly documented. I intend to keep it same and have no intention of writing a whole tutorial on how to write this tool.

However, I can mention that all one has to do is register a service or application to listen to power setting change event and be sure to register for lid switch change event. That’s it.

If you don’t want to do it yourself but need this tweak, ask me and I’ll email it to you Dowload it from here.

And Microsoft!!! Thanks for at least providing the infrastructure to do it but a documentation would have helped even more.

Note: I would like to mention that this tool works only on Windows 7 and Vista. However, I have not tested it for XP and have reports that it does not work for XP.

Code (as lot of people want to understand how it works):

You need only do the following. This is not exact code but just idea about what you need and how to use it.

  1. Declare IDs
    static Guid GUID_LIDSWITCH_STATE_CHANGE = new Guid(0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3);
    const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;
    const int DEVICE_NOTIFY_SERVICE_HANDLE = 0x00000001;
    // This structure is sent when the PBT_POWERSETTINGSCHANGE message is sent.
    // It describes the power setting that has changed and contains data about the change
    [StructLayout(LayoutKind.Sequential, Pack = 4)]
    internal struct POWERBROADCAST_SETTING
    public Guid PowerSetting;
    public uint DataLength;
    public byte Data;
  2. Declare the Native Function we need to use
    [DllImport(@"User32", SetLastError = true, EntryPoint = "RegisterPowerSettingNotification",CallingConvention = CallingConvention.StdCall)]
    private static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient,ref Guid PowerSettingGuid,Int32 Flags);
  3. This is how you register for lid switch change
    private void RegisterForPowerNotifications(IntPtr hwnd)
    IntPtr hLidSwitch = RegisterPowerSettingNotification(hwnd, ref GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_WINDOW_HANDLE);
  4. RegisterForPowerNotifications(this.Handle); //call this function to register for power notification
  5. override wnd proc:
    protected override void WndProc(ref Message m)
    base.WndProc(ref m);
    if (m.Msg == WM_POWERBROADCAST)
    catch (Exception)
  6. Rest of the code
    private void PowerSettingChange(Message m)
    m.LParam, typeof(POWERBROADCAST_SETTING));
    if (ps.DataLength == Marshal.SizeOf(typeof(Int32)) && ps.PowerSetting == GUID_LIDSWITCH_STATE_CHANGE)
    //use some logic to lock workstation. You can uSe the below functions
    public static extern void LockWorkStation(); private void LockSystem()

private static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient,ref Guid PowerSettingGuid,Int32 Flags);

I was trying hard to find a suitable way to show text and image in same cell in a DataGridView Column. Thanks to DotNetMonitor I found the code to extend the column so that it can do that. However, as I am pretty new to C#, I did not know how to use this code. Once I figured it out how simple it is, first thing that came to my mind was to share this. This information might be helpful for someone like me who is totally new to C#. All you need to do is:

  1. Copy the code from provided link and create a new file.
  2. This way, You’ll get two new classes, TextAndImageColumn and TextAndImageCell.
  3. Change the type of required column to the TextAndImageColumn
  4. Populate the text values just as you do normally.
  5. To show the image, All you need to do is, set the image property when you are populating the data. You may need to type cast the column object to TextAndImageCell for this. Below is the sample line of code:
    ((TextAndImageCell)(myDataGridView[columnIndex, rowPos])).Image = Image.FromFile(“myImage.ico”);
  6. just replace the myImage.ico with the path to your image.

I hope this helps someone.