How To: Get System Power State Name and Flags

A question came up on our forums and so I investigated writing a wrapper for the GetSystemPowerState API function. This allows you to retrieve the power state name, and also a bitmask of flags – Is the backlight on, is the device password protected etc. This is the result in VB.NET. We will add it to the wish list for the next version of the library.


<DllImport(“coredll.dll”)> _
Public Shared Function GetSystemPowerState(ByVal pBuffer As System.Text.StringBuilder, ByVal Length As Integer, ByRef pFlags As PowerState) As Integer
End Function

<Flags()> _
Public Enum PowerState
[On] = &H10000 ‘// on state
Off = &H20000 ‘ // no power, full off
Critical = &H40000 ‘// critical off
Boot = &H80000 ‘ // boot state
Idle = &H100000 ‘ // idle state
Suspend = &H200000 ‘ // suspend state
Unattended = &H400000 ‘ // Unattended state.
Reset = &H800000 ‘ // reset state
UserIdle = &H1000000 ‘ // user idle state
BackLightOn = &H2000000 ‘ // device screen backlight on
Password = &H10000000 ‘ // This state is password protected.
End Enum


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim sb As New System.Text.StringBuilder(260)
Dim flags As PowerState = 0
Dim ret As Integer = GetSystemPowerState(sb, sb.Capacity, flags)

TextBox1.Text = sb.ToString()
TextBox2.Text = flags.ToString()
End Sub


The last method is just a very simple example of calling the function and displaying the result.

Advertisements

How To: Programmatically Scroll Controls

A number of controls within .NETCF have built in ScrollBars. Occasionally you may want to operate these programmatically on behalf of the user. When you do this you want both the control to scroll and the scrollbars to correctly reflect the current position. Faced with this requirement I found a solution in the WM_VSCROLL (and equivalent HSCROLL) message. You can send this message to the native handle of your control along with a number of present constants to offer hands-free scrolling. Along the way I discovered that to work you must have the handle of the native control which implements the scroll bars. In the case of the WebBrowser this is a grand-child of the outer managed control so we have to use the native GetWindow API call to get down to the right HWND. I wrapped this up in a class I’ve called ScrollBarHelper which allows the user to move left, right, up and down. The code for the class is:-


/// <summary>
/// Helper class to programmatically operate scrollbars.
/// </summary>
public class ScrollBarHelper
{
  private IntPtr handle;


  public ScrollBarHelper(Control c)
  {
    if (c is WebBrowser)
    {
      //special case for complex control
      //get the inner IE control
      IntPtr hInternetExplorer = NativeMethods.GetWindow(c.Handle, NativeMethods.GW.CHILD);
      //get the first child (status bar)
      IntPtr hStatus = NativeMethods.GetWindow(hInternetExplorer, NativeMethods.GW.CHILD);
      //get the html body area
      handle = NativeMethods.GetWindow(hStatus, NativeMethods.GW.HWNDNEXT);
    }
    else
    {
      handle = c.Handle;
    }
  }



public void LineRight()
{
  SendMessage(NativeMethods.WM_HSCROLL, NativeMethods.SB_LINEDOWN);
}
public void LineLeft()
{
  SendMessage(NativeMethods.WM_HSCROLL, NativeMethods.SB_LINEUP);
}

public void PageRight()
{
  SendMessage(NativeMethods.WM_HSCROLL, NativeMethods.SB_PAGEDOWN);
}
public void PageLeft()
{
  SendMessage(NativeMethods.WM_HSCROLL, NativeMethods.SB_PAGEUP);
}

public void LineDown()
{
  SendMessage(NativeMethods.WM_VSCROLL, NativeMethods.SB_LINEDOWN);
}
public void LineUp()
{
  SendMessage(NativeMethods.WM_VSCROLL, NativeMethods.SB_LINEUP);
}

public void PageDown()
{
  SendMessage(NativeMethods.WM_VSCROLL, NativeMethods.SB_PAGEDOWN);
}
public void PageUp()
{
  SendMessage(NativeMethods.WM_VSCROLL, NativeMethods.SB_PAGEUP);
}

private void SendMessage(int msg, int value)
{
  Microsoft.WindowsCE.Forms.Message m = Microsoft.WindowsCE.Forms.Message.Create(handle, msg, (IntPtr)value, handle);
  Microsoft.WindowsCE.Forms.MessageWindow.PostMessage(ref m);
}

[DllImport(“coredll.dll”)]
internal static extern IntPtr GetWindow(IntPtr hWnd, GW uCmd);

internal enum GW : int
{
  HWNDFIRST = 0,
  HWNDLAST = 1,
  HWNDNEXT = 2,
  HWNDPREV = 3,
  OWNER = 4,
  CHILD = 5,
}

//scrollbar messages
internal const int WM_HSCROLL = 0x0114;
internal const int WM_VSCROLL = 0x0115;

//constants for scrollbar actions
internal const int SB_LINEUP = 0;
internal const int SB_LINEDOWN = 1;
internal const int SB_PAGEUP = 2;
internal const int SB_PAGEDOWN = 3;

}


In order to use the control you create a new instance passing it the control of your choice. Then call methods to scroll the control e.g.


private ScrollBarHelper wsbh;
private ScrollBarHelper tsbh;

private void Form1_Load(object sender, EventArgs e)
{
  wsbh = new ScrollBarHelper(webBrowser1);
  tsbh = new ScrollBarHelper(textBox1);
}


private void button1_Click(object sender, EventArgs e)
{
  tsbh.PageUp();
}


The control contains methods to Scroll via single lines or pages at a time, I didn’t get around to looking at setting the explicit value of the scrollbar control, but this should be possible also – refer to the documentation for WM_VSCROLL for how to pass the value.

WirelessManager sample

Mobile In The Hand 3.0 has just been released. This is the latest version of our .NET Compact Framework library for working with all aspects of Windows Mobile. This latest version is optimised for .NET Compact Framework 2.0 and 3.5 and introduces a number of new classes. One of these is the WirelessManager which allows you to toggle the radio mode of your Phone, WiFi and Bluetooth just like the built in Wireless Manager application. I have prepared this sample to demonstrate how to use the class. It exposes a single form with checkboxes for each of the radio types, toggling the checkbox changes the radio mode for the specific device. You will need to have Mobile In The Hand 3.0 installed to use this sample (The Evaluation Edition can be used). The sample can be downloaded here:-


http://inthehand.com/files/folders/resources/entry4227.aspx

How To: Use the WebRequestMethods class

Networking In The Hand introduces a repository for all the various method types for FTP and HTTP operations. While the common ones are easy to remember (“GET”,”POST” etc) many others are not, and so this class (like its desktop equivalent) provides a central place to refer to them, without dotting your code with hard-coded strings. WebRequestMethods contains two static classes – Ftp and Http which as the name suggests contain the two sets of operations. Here we find one used in the previous sample:-


InTheHand.Net.WebRequestMethods.Ftp.GetDateTimestamp


As with all the functionality in the namespace, you’ll find the full details in the online documentation library.

How To: Use the FtpWebRequest

Networking In The Hand includes a full desktop-compatible implementation of the FtpWebRequest class. This plugs into the WebRequest class so that calling WebRequest.Create() with an FTP Uri will create an object of type FtpWebRequest. Because FTP support isn’t built into the Compact Framework you have to register the class with this prefix using the the following code (only required once in your code):-


InTheHand.Net.FtpWebRequest.RegisterPrefix();


We have already looked at performing simple operations with the WebClient class. for more complex FTP operations you can use the FtpWebRequest directly. For example retrieving the modification date of a specific file:-


FtpWebRequest requestDate = (FtpWebRequest)FtpWebRequest.Create(requestUri);
requestDate.Method = WebRequestMethods.Ftp.GetDateTimestamp;
if (wc.Credentials != null)
{
   requestDate.Credentials = wc.Credentials;
}
FtpWebResponse responseDate = null;

responseDate = (FtpWebResponse)requestDate.GetResponse();

if (responseDate != null)
{
   MessageBox.Show(responseDate.LastModified.ToString());
   responseDate.Close();
}

How To: Use the WebClient

Networking In The Hand includes the WebClient class which is a helper class which makes it easier to do uploading and downloading of data using HTTP and FTP transports. For example rather than creating an HttpWebRequest, setting a number of properties, getting the response and reading the response stream and copying the data into a file, why not use DownloadFile to perform a single operation to write the data from a specific Uri to a local file:-


WebClient wc = new WebClient();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
   wc.DownloadFile(uriTarget, saveFileDialog1.FileName);
}


Additional methods provide the ability to download a string, and download a byte array containing the data. There are a similar set of operations for uploading:-


if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
   wc.UploadFile(uriDestination, openFileDialog1.FileName);
}


Not only does this work with the HTTP transport, but also with the FTP support which is also part of the library – the only difference is the Uri that you pass in. If your site requires a username/password you can set the Credentials property of the WebClient once and they will be used for all subsequent operations.