Click here to Skip to main content
15,867,686 members
Articles / Mobile Apps / Windows Mobile

A Flashlight App for Your SmartPhone or Pocket PC

Rate me:
Please Sign up or sign in to vote.
4.57/5 (8 votes)
2 Aug 2008CPOL3 min read 154.9K   498   37   22
A simple, fully functional flashlight to help you see in the dark.
FlashLight Screenshot

Introduction

This article is about a silly little Windows Mobile application I wrote one dark and stormy night.

Background

So the weather in Minnesota has been pretty volatile this year with some thunderstorms like I haven't seen in a long time. One of the first ones to hit, and a number of them since, have knocked out my power in the middle of the night and since I like to keep the windows open as much as possible I've found myself stumbling around in the dark from room to room using my Pocket PC to provide some illumination.

Needless to say my shins have taken a beating.

"Why not put a real flashlight next to the bed" you ask? "Bah! I'll write a .NET Compact Framework application instead. It's way more fun!" is my answer.

Using the Code

Disabling the Screen Timeout

The all white full screen form is easy enough but the first real requirement of it is that the flashlight has to disable the backlight timeout while running (that first night I was a lot of screen tapping just to keep it as bright as possible).

This is done using the SetPowerRequirement function on the coredll.dll (I like that they named the DLL CoreDll.dll just in case the DLL extension wasn't descriptive enough). SetPowerRequirement allows an application to specify that it needs a certain power level from a particular device while running. The name of the backlight device is BKL1:. Telling the OS that you need full power from the backlight will keep it from timing out.

The only trick to this method is that you need to be sure to call ReleasePowerRequirement when you are done. I wrapped all this up in a PowerRequirement class that implements IDisposable so that it can be easily managed.

C#
protected override void OnLoad(EventArgs e)
{
    ...
    m_powerRequirement = new PowerRequirement("BKL1:", PowerState.FULL);
    ...
}

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        if (m_powerRequirement != null)
            m_powerRequirement.Dispose();
    }
    base.Dispose(disposing);
} 

The prototypes for the CE API to set and release power requirements look like:

C#
[DllImport("coredll.dll")]
private static extern IntPtr SetPowerRequirement(string pvDevice, 
     PowerState DeviceState, int DeviceFlags, IntPtr pvSystemState, int StateFlags);

[DllImport("coredll.Dll")]
private static extern int ReleasePowerRequirement(IntPtr hPowerReq);

Controlling the Screen Brightness

Next I wanted a way to adjust the backlight brightness. This took some digging on the InterWebs but I eventually found enough hints to find something that works. That is to say it works on my HTC Touch running Windows Mobile 6. It sounds like backlight management can be very device and vendor specific, so this may or may not work on your device.

On the Touch the backlight brightness is managed by a registry setting HKCU\ControlPanel\Backlight\Brightness which is straightforward enough. Once you know that, it's just a matter of letting the OS know that you've changed the value. This is done using an event (the Win32 flavor, not the .NET flavor). Oh and one caveat on the brightness adjustment: it doesn't work on the emulator; no matter what you set it to, it stays at full brightness.

C#
public int Brightness
{
     ...
     set
     {
         PowerStatus power = new PowerStatus();
         if (power.PowerLineStatus == PowerLineStatus.Online)
             SetBacklightValue("ACBrightness", value);
         else
             SetBacklightValue("Brightness", value);

         RaiseBackLightChangeEvent();
     }
}

private void SetBacklightValue(string name, int v)
{
     RegistryKey key = 
        Registry.CurrentUser.OpenSubKey(@"ControlPanel\Backlight", true);
     if (key != null)
     {
         key.SetValue(name, v);
         key.Close();
     }
}

private static void RaiseBackLightChangeEvent()
{
     IntPtr hBackLightEvent = 
	CreateEvent(IntPtr.Zero, false, true, "BackLightChangeEvent")
     if (hBackLightEvent != IntPtr.Zero)
     {
         SetEvent(hBackLightEvent);
         CloseHandle(hBackLightEvent);
     }
}

True Fullscreen

You would think that setting the Form to maximized, getting rid of the control boxes, removing the border and making it a topmost window would provide a truly full screen window. This worked sometimes, but not every time. Often times the title bar and start menu would still show up over the flashlight window. To effectively get true fullscreen mode, one has to hit the CE API again.

C#
protected override void OnLoad(EventArgs e)
{
    ...

    SHFullScreen(this.Handle, SHFS_HIDETASKBAR | 
		SHFS_HIDESIPBUTTON | SHFS_HIDESTARTICON);

    base.OnLoad(e);
}

private const int SHFS_SHOWTASKBAR = 0x0001;
private const int SHFS_HIDETASKBAR = 0x0002;
private const int SHFS_SHOWSIPBUTTON = 0x0004;
private const int SHFS_HIDESIPBUTTON = 0x0008;
private const int SHFS_SHOWSTARTICON = 0x0010;
private const int SHFS_HIDESTARTICON = 0x0020;

[DllImport("aygshell")]
static extern bool SHFullScreen(IntPtr hwnd, int dwState);

Points of Interest

The only real catch to this application that I haven't been able to figure out is battery life metrics. I wanted to be able to display estimated battery life remaining (you know for those "emergency, stuck in the elevator" situations where you might need to conserve on the battery). I got some code from OpenNETCF.org that uses the GetSystemPowerStatusEx function but it never returns anything but null data on my Touch and on the Windows Mobile Device Emulator.

If anybody knows how to get meaningful data out of that method, please let me know. I have included the code and functionality in the application on the off chance it works on some other devices.

History

  • 08-02-2008 - First posting
  • 08-04-2008 - Added SHFullScreen call

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions

 
PraiseUseful tool. Pin
Skype: Captain.TechLord2-Feb-18 13:20
Skype: Captain.TechLord2-Feb-18 13:20 
QuestionThank you very much!!! Pin
hanmingjun24-Jul-11 23:05
hanmingjun24-Jul-11 23:05 
GeneralHelloooo Pin
pavands11-Mar-10 18:04
pavands11-Mar-10 18:04 
GeneralRe: Helloooo Pin
Don Kackman12-Mar-10 1:23
Don Kackman12-Mar-10 1:23 
I never tested this on WM5. Just 6 and 6.1. What model device is it?
10 PRINT Software is hard. - D Knuth
20 GOTO 10

GeneralRe: Helloooo Pin
pavands16-Mar-10 23:45
pavands16-Mar-10 23:45 
GeneralRe: Helloooo Pin
Don Kackman17-Mar-10 3:39
Don Kackman17-Mar-10 3:39 
GeneralRe: Helloooo Pin
pavands17-Mar-10 6:13
pavands17-Mar-10 6:13 
Generalgood thing Pin
vast_pirate22-Apr-09 23:54
vast_pirate22-Apr-09 23:54 
GeneralRe: good thing Pin
Don Kackman23-Apr-09 11:34
Don Kackman23-Apr-09 11:34 
GeneralIt's great. Pin
winwnx10-Apr-09 20:36
winwnx10-Apr-09 20:36 
GeneralSMT5800 Issue Pin
bamaboy121729-Mar-09 19:31
bamaboy121729-Mar-09 19:31 
GeneralJust a FYI about coredll.dll name Pin
Spacix One26-Dec-08 13:00
Spacix One26-Dec-08 13:00 
GeneralReally helpful Pin
Dr.Luiji16-Dec-08 0:23
professionalDr.Luiji16-Dec-08 0:23 
GeneralGet Battery Life Pin
ghle6-Aug-08 15:49
ghle6-Aug-08 15:49 
GeneralRe: Get Battery Life Pin
jatkins6-Aug-08 21:09
jatkins6-Aug-08 21:09 
AnswerRe: Get Battery Life Pin
ghle7-Aug-08 1:34
ghle7-Aug-08 1:34 
GeneralRe: Get Battery Life Pin
John Atkins7-Aug-08 2:07
John Atkins7-Aug-08 2:07 
GeneralGetting Battery Life - not very granular Pin
John Atkins5-Aug-08 20:40
John Atkins5-Aug-08 20:40 
GeneralRe: Getting Battery Life - not very granular Pin
Don Kackman6-Aug-08 4:54
Don Kackman6-Aug-08 4:54 
GeneralRe: Getting Battery Life - not very granular Pin
jatkins6-Aug-08 11:11
jatkins6-Aug-08 11:11 
GeneralNeat idea Pin
Josh Smith3-Aug-08 13:15
Josh Smith3-Aug-08 13:15 
GeneralRe: Neat idea Pin
Don Kackman3-Aug-08 13:42
Don Kackman3-Aug-08 13:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.