software you can take with you

Peter Foot

Microsoft Windows Phone Development MVP

February 2008 - Posts

  • 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")> _
    PublicSharedFunction GetSystemPowerState(ByVal pBuffer As System.Text.StringBuilder, ByVal Length AsInteger, ByRef pFlags As PowerState) AsInteger
    EndFunction

    <Flags()> _
    PublicEnum 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.
    EndEnum


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

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

    TextBox1.Text = sb.ToString()
    TextBox2.Text = flags.ToString()
    EndSub

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

    Posted Feb 23 2008, 11:29 AM by Peter Foot
    Filed under: ,
  • Tech-Ed 2008

    I was delighted to find out this week that my session on Personal Area Networking was selected for Tech-Ed Developers this year. The session will cover a range of networking topics including of course Bluetooth. There will be a selection of demos which will support audience participation. It looks like I'll be in good company too, the Windows Mobile track will have a wide range of topics which will enable you to build compelling applications with the latest tools and technologies. You can search the session list here.

    Posted Feb 21 2008, 01:45 PM by PeterFoot
    Filed under:
  • 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;
        }
      }



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

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

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

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

    privatevoid 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")]
    internalstaticextern IntPtr GetWindow(IntPtr hWnd, GW uCmd);

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

    //scrollbar messages
    internalconstint WM_HSCROLL = 0x0114;
    internalconstint WM_VSCROLL = 0x0115;

    //constants for scrollbar actions
    internalconstint SB_LINEUP = 0;
    internalconstint SB_LINEDOWN = 1;
    internalconstint SB_PAGEUP = 2;
    internalconstint 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;

    privatevoid 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.

    Posted Feb 12 2008, 06:00 PM by Peter Foot
    Filed under: ,
  • Great XNA Book Coming Soon

    Rob Miles has finished his book on XNA development and it will be out in the wild soon. Although we don't have XNA support for mobile devices it is a cool framework which allows you to build games for both PC and XBox 360 with managed code. You can see a couple of sample chapters from the book which will help you get started with XNA Game Studio from Rob's site Very Silly Games which showcases some of the interesting things you can do with the framework. Not to mention the great puns in the Silly Ideas list.

    Posted Feb 01 2008, 04:43 PM by Peter Foot
    Filed under:
Copyright © 2001-2013 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy. OrcsWeb's Windows Cloud Server Hosting