Category: NETCF

  • Multiline Button

    On the newsgroup somebody asked how to have multiple lines of text on a .NETCF Button control. The native control supports this but the appropriate style is not set by default. The following code snippet shows how to enable this:-


    private const int BS_MULTILINE = 0x00002000;
    private const int GWL_STYLE = -16;

    [System.Runtime.InteropServices.DllImport(“coredll”)]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [System.Runtime.InteropServices.DllImport(“coredll”)]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    public static void MakeButtonMultiline(Button b)
    {
        IntPtr hwnd = b.Handle;
        int currentStyle = GetWindowLong(hwnd, GWL_STYLE);
        int newStyle = SetWindowLong(hwnd, GWL_STYLE, currentStyle | BS_MULTILINE);
    }


    The usage is simple, just pass the specific Button control to MakeButtonMultiline:-


    MakeButtonMultiline(button1);


    Here is what the amended button looks like on the left (default appearance on the right)




    The designer fully supports entering multiple lines for the Button Text property, just click the dropdown at the end of the field and it will pop up a window into which you can add linebreaks with your text.

  • More Facebook Progress

    I’m now a member of the CodePlex workspace for the Facebook Developer Toolkit. I’ve been working on porting across my modifications into the codebase. The .NETCF v2.0 version uses a project called Facebook.Compact but refers to the existing source files from the desktop project. Then some conditional compilation is used to hide a few unsupported features from .NETCF and implement some workarounds for missing functionality. This will be a familiar technique if you’ve been to Daniel‘s sessions (or read his blog posts on the subject). It’s not quite working yet since my code made use of a couple of my own libraries for speed, so I’ll need to implement a few of the features within those in the Facebook.Compact project.

  • Facebook API and the Compact Framework

    The Facebook API allows third-party web and desktop applications to interact with Facebook features. There is an excellent shared-source library for .NET to wrap the Facebook calls but currently it only supports the full framework. I did some work converting this source to compile and run on .NETCF v2.0. There are some example screens here of the login process, and pulling back information about our book group.


       


    Since the login screen uses the same page as the desktop it’s not a great fit on a Pocket PC screen, so that’s an area for improvement.

  • Guid.NewGuid Performance

    João Paulo was investigating Guid performance when used as identifiers in a SQL Compact database and discovered that the performance of Guid.NewGuid() left a lot to be desired. The reason for this is that the function uses lowest-common-denominator support, using a random number generator and setting a couple of specific bits to conform to guid standards. Another method I’ve been recommending for some time is to use the native CoCreateGuid() function – see http://www.peterfoot.net/NeedAGUIDInAHurry.aspx. This method is supported in all devices running Windows Mobile 2003 and above, and in Windows CE 4.x. I’ve been using it in the InTheHand.Guid2 class in Mobile In The Hand (Will be renamed to GuidHelper in the next version). I did some tests creating 100,000 guids in a row with both methods. Guid.NewGuid() took 139 seconds, and GuidHelper.NewGuid() took a mere 9 seconds. As you can see that is quite a significant difference.

  • Waiting for a Process

    There have been numerous occasions where I’ve needed to write code to launch another process and wait for it to complete, usually taking some action based on the exit code. Therefore I’ve got a handy helper method to do this. It has changed a few times and is made much simpler in .NETCF v2.0 by taking advantage of the Process class. Most recently I used the code in one of the book chapters to show automatic installation of the Microsoft Message Queue (MSMQ) components onto a Windows Mobile device. The method is named ShellWait, as the name implies it launches a process and waits, the return value is the exit code from the process. If the application doesn’t exist it returns -1 immediately.


    [C#]


    //helper function to launch a process and wait for the result code
    private static int ShellWait(string app, string args)
    {
       if (!File.Exists(app))
       {
          return -1;
       }



       Process p = Process.Start(app, args);
       p.WaitForExit();



       return p.ExitCode;
    }


     


    [VB]


    Private Shared Function ShellWait(ByVal app As String, ByVal args As String) As Integer
       If Not File.Exists(app) Then
          Return -1
       End If

       Dim p As Process = Process.Start(app, args)
       p.WaitForExit()

       Return p.ExitCode
    End Function

  • New Sounds sample in Windows Mobile 6 SDK Refresh

    The Windows Mobile 6 SDK Refresh is available today. It includes some documentation enhancements and new samples. One of these is called RingtoneManager and is a C# application which wraps all the Sound APIs for ringtone management and sound playback, the latter of which were introduced with Windows Mobile 6. This is great because it gives you all the P/Invokes etc you need to use this functionality yourself. However expect to see these Windows Mobile specific APIs in the next version of Mobile In The Hand.

  • TomTom In The Hand 6.1

    I’ve been a bit quiet over the past few weeks, on vacation and at the Microsoft MVP Summit. I’ve also been working on the TomTom In The Hand library and today have released a new version.


    It turns out that TomTom SDK 6 shipped with a known bug that prevents you from passing negative co-ordinates into any of the methods (returned values were okay). At the moment I believe that our library is the only one which has a fix for this bug which was a major headache for developers. You can now use TomTom through this library wherever you reside in the world (assuming you have map coverage of course) without making any changes to your existing code.


    This release also adds some new functionality – The RawGpsReceived event on the Navigator object allows you to receive NMEA data from TomTom which allows you to share this data if you are not already using a multiplexer such as the Windows Mobile 5.0 GPS service or Franson GPS Gate or similar.

  • Programmatically Activating Speakerphone

    Windows Mobile devices, both Smartphone and Pocket PC support Speakerphone functionality. When operating through the Phone application the end user can toggle the state, up until now there has been no documented way to achieve this through code. Last year I investigated the issue on behalf of one of my readers and failed on several attempts at simulating a press-and-hold of the green phone button. It turns out that the device issues a specific key constant to change the state, and this can be simulated through code. It also turns out that the constant is equivalent to VK_F16 (thats Keys.F16 for managed code). I’ve wrapped up the necessary P/Invoke into the following code:-


    public sealed class SpeakerPhone
    {
    private SpeakerPhone() { }

    /// <summary>
    /// Toggles the SpeakerPhone state.
    /// </summary>
    public static void Toggle()
    {
    //keydown
    NativeMethods.keybd_event((byte)Keys.F16, 0, 0, 0);
    //keyup
    NativeMethods.keybd_event((byte)Keys.F16, 0, NativeMethods.KEYEVENTF_KEYUP, 0);
    }

    internal static class NativeMethods
    {
    internal const int KEYEVENTF_KEYUP = 0x0002;


    [System.Runtime.InteropServices.DllImport(“coredll.dll”)]
    internal static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
    }
    }


     


    Since this just toggles the state, you can’t determine the current state at any particular time. This code has been tested on Windows Mobile 5.0 and beyond (It possibly works on Windows Mobile 2003 but I haven’t tested), it doesn’t work on the Windows Mobile 5.0 Emulators as Speakerphone isn’t implemented. I was able to establish a call on speakerphone using the following code:


    Microsoft.WindowsMobile.Telephony.Phone p = new Microsoft.WindowsMobile.Telephony.Phone();
    p.Talk(“01234567890”);
    SpeakerPhone.Toggle();


    Once the call is ended the speakerphone state is restored on subsequent calls.

  • Richard Jones is Blogging

    Richard has just started blogging, starting with a sample for maximising screen space by creating a Vista style textbox with integral label, and a description of what he has been up to in the fight against “Monkey Code“. I’ve subscribed to his RSS feed and added him to the blogroll.

  • What’s New For Managed Developers In Windows Mobile 6 (Part 2)

    When I previously posted the list of additional system properties I was using the Windows Mobile 6 SDK Documentation as a reference. Well it turns out that there are even more new properties, you can view them using Object Browser in your project but they aren’t included in the documentation. The other new properties are:-



    • CameraEnabled – This in addition to existing CameraPresent property
    • CellularSystemAvailable1xrtt
    • CellularSystemAvailableEdge
    • CellularSystemAvailableEvdo
    • CellularSystemAvailableEvdv
    • CellularSystemAvailableGprs
    • CellularSystemAvailableHsdpa
    • CellularSystemAvailableUmts
    • CellularSystemConnected1xrtt
    • CellularSystemConnectedEdge
    • CellularSystemConnectedEvdo
    • CellularSystemConnectedEvdv
    • CellularSystemConnectedGprs
    • CellularSystemConnectedHsdpa
    • CellularSystemConnectedUmts
    • ClamshellClosed
    • DeviceLocked
    • KeyLocked
    • LockStates – A combination of flags for Device, Key and Sim locks
    • SimLocked
    • PhoneTalkingCallStartTime