Jump to content
LaptopVideo2Go Forums
Scale

Close laptop lid code

Recommended Posts

Scale

Hey guys,

(skip this if you dont care about the backstory :) )

Last wensday i received my inspiron 1720 and well i wanted to install windows xp, So i did and i download nvidia drivers like i always do and it didnt work well after a bit of googling i came here downloaded the moddef inf and it worked perfectly. But later i noticed that my monitor didnt turn off when i closed it after yet again googling i found out that its becuase im using these drivers not made by dell.

Now maby theres a solution to this and i didnt find any so i just created my own and i tought i'd share it with you all.

This code is writen in c# and will cost you your standby function of your laptop.

It works like this, I configured my laptop to go into standby when i close the lid my program will then cancel the standby and turn off the monitor instead any mouse or keyboard movement will turn the monitor back on.

//create an override function which receives messages send to your program
protected override void WndProc(ref Message m)
	{

		base.WndProc(ref m);

		switch (m.Msg)
		{
			//Powerbroadcast messages
			case WM_POWERBROADCAST:
				{
					switch (m.WParam.ToInt32())
					{
						//if msg equals request for standby
						case (int)PBT.PBT_APMQUERYSUSPEND:
							{
								//check if its an hibernate or standby request
								if (m.LParam == (IntPtr)0x1)
								{
									//Set bool to true so our program doesnt cancel hibernate
									hibernate = true;
									//start a 10 second timer which sets hibernate to false again so after the hibernate if you close the lid it will turn of the monitor again
									resethib.Start();
								}
								//check if a hibernate request was made
								if (hibernate == false)
								{
										//create a deny to standby request
										IntPtr deny = new IntPtr(BROADCAST_QUERY_DENY);
										//send the deny command back
										m.Result = deny;
										//Turn off the monitor
										PostMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
									}
								}
								break;
							}

						default:
							break;
					}
					break;
				}
			default:
				break;
		}
	}

Well thats about it, if you dont understand how this works or have any other questions just send me an email/pm and ill see if i can help you.

Share this post


Link to post
Share on other sites
Guest monmon

Hi.. I'd just like to ask what you mean by "if (m.LParam == (IntPtr)0x1)".

Is it to check whether it's a hibernate request or a standby request? Can it be used to differentiate hibernate from standby?

I'm creating a program that logs power management functions so I need to distinguish between the 2(standby and hibernate).

Thanks!! :)

Hey guys,

(skip this if you dont care about the backstory :) )

Last wensday i received my inspiron 1720 and well i wanted to install windows xp, So i did and i download nvidia drivers like i always do and it didnt work well after a bit of googling i came here downloaded the moddef inf and it worked perfectly. But later i noticed that my monitor didnt turn off when i closed it after yet again googling i found out that its becuase im using these drivers not made by dell.

Now maby theres a solution to this and i didnt find any so i just created my own and i tought i'd share it with you all.

This code is writen in c# and will cost you your standby function of your laptop.

It works like this, I configured my laptop to go into standby when i close the lid my program will then cancel the standby and turn off the monitor instead any mouse or keyboard movement will turn the monitor back on.

//create an override function which receives messages send to your program
protected override void WndProc(ref Message m)
[tab][/tab]   {

[tab][/tab][tab][/tab]  base.WndProc(ref m);

[tab][/tab][tab][/tab]  switch (m.Msg)
[tab][/tab][tab][/tab]  {
[tab][/tab][tab][/tab][tab][/tab] //Powerbroadcast messages
[tab][/tab][tab][/tab][tab][/tab] case WM_POWERBROADCAST:
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]{
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]	switch (m.WParam.ToInt32())
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]	{
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]   //if msg equals request for standby
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]   case (int)PBT.PBT_APMQUERYSUSPEND:
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]  {
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] //check if its an hibernate or standby request
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] if (m.LParam == (IntPtr)0x1)
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] {
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]//Set bool to true so our program doesnt cancel hibernate
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]hibernate = true;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]//start a 10 second timer which sets hibernate to false again so after the hibernate if you close the lid it will turn of the monitor again
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]resethib.Start();
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] }
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] //check if a hibernate request was made
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] if (hibernate == false)
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] {
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	//create a deny to standby request
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	IntPtr deny = new IntPtr(BROADCAST_QUERY_DENY);
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	//send the deny command back
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	m.Result = deny;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	//Turn off the monitor
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	PostMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]}
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] }
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] break;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]  }

[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]   default:
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]  break;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]	}
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]	break;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]}
[tab][/tab][tab][/tab][tab][/tab] default:
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]break;
[tab][/tab][tab][/tab]  }
[tab][/tab]   }

Well thats about it, if you dont understand how this works or have any other questions just send me an email/pm and ill see if i can help you.

Share this post


Link to post
Share on other sites
LSudlow

The following line in the driver inf is supposed to do the same thing:

HKLM,"System\CurrentControlSet\Control\GraphicsDrivers\LidCloseSetPower"

In fact, nVidia is now adding it to thier infs, years after we started doing it.

However, it may not work for everyone.

Share this post


Link to post
Share on other sites
k3nny

I also noticed this line which may be involved in this issue:

HKR,, LidBehavior, %REG_DWORD%, 0x11

Share this post


Link to post
Share on other sites
LSudlow

You're absolutely right - it's supposed to. Unfortunately, Microsoft did something in XP Service Pack 2 (and SP3 and Vista) that stopped that from working. That's when we had to resort to the LidCloseSetPower hack.

As long as we're talking about LCD/lid behavior, there's another catche that snags most people: Even if the LCD shuts off when you close the lid, any activity that affects the touchpad or external mouse will turn the LCD back on again. Since the lid is already shut, most people aren't aware that it's on and running down the battery.

Always use the Windows power settings to shurt off the monitor after some period of inactivity.

Share this post


Link to post
Share on other sites
Michael Marley
As long as we're talking about LCD/lid behavior, there's another catche that snags most people: Even if the LCD shuts off when you close the lid, any activity that affects the touchpad or external mouse will turn the LCD back on again. Since the lid is already shut, most people aren't aware that it's on and running down the battery.

It doesn't go back on with my system.....

Share this post


Link to post
Share on other sites
LSudlow

It probably doesn't happen with all systems. Just something worth checking.

Share this post


Link to post
Share on other sites
Michael Marley

Also, for some people 0x11 doesn't work. I have to set it to 0x10 to keep the backlight from coming on again right after it goes off.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×