Category: NETCF

  • Create a Top-Most form

    The following code can be used to force your form to the top of the z-order. Use this functionality with care since it’s bad practice to hog the topmost position which could obscure other important functionality.


    private void Form1_Load(object sender, System.EventArgs e)


    {


          //get handle


          this.Capture = true;


          IntPtr hwnd = OpenNETCF.Win32.Win32Window.GetCapture();


          this.Capture = false;


     


          //set foreground


          OpenNETCF.Win32.Win32Window.SetWindowPos(hwnd, Win32Window.SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, Win32Window.SetWindowPosFlags.SWP_NOMOVE | Win32Window.SetWindowPosFlags.SWP_NOSIZE | Win32Window.SetWindowPosFlags.SWP_SHOWWINDOW);


     


    }


    Note that this code cannot be placed in the form constructor, since the native form has yet to be created so a valid window handle won’t be returned. Here the code is placed into the Form_Load method.


    In .NETCF 2.0 you can set the TopMost property of your form which removes the need for any of this interop (Thanks Daniel Moth for correcting this).

  • .NETCF Chat Tomorrow

    As part of a regular series of .NETCF technical chats there will be an MVP hosted chat tomorrow (14th October) on any aspect of .NET Compact Framework and Smart Device programming. 10-11am PDT, 17-18 GMT


    Add a reminder to your calendar


    More details, and details of other technical chats, here at MSDN.

  • Bring a .NETCF Form to the foreground

    Sometimes calling BringToFront for your form is not enough to bring your app to the foreground. You can P/Invoke SetForegroundWindow which will activate your app and bring the window to the front of the Z-Order:-


    C#

    public void SetForegroundWindow()
    {
        this.Capture = true;
        IntPtr hwnd = OpenNETCF.Win32.Win32Window.GetCapture();
        this.Capture = false;
        OpenNETCF.Win32.Win32Window.SetForegroundWindow(hwnd);
    }

    or VB

    Public Sub SetForegroundWindow
        Me.Capture = True
        Dim hwnd As IntPtr = OpenNETCF.Win32.Win32Window.GetCapture()
        Me.Capture = False
        OpenNETCF.Win32.Win32Window.SetForegroundWindow(hwnd)
    End Sub

  • Fixing ComboBox bugs…

    The standard ComboBox in NETCF has a couple of “issues”, however it’s possible to workaround them with a bit of tweaking. I rolled together a number of these fixes into a ComboBoxEx class. Heres the code (C#), I hope this (with a few more missing features) will be in the next SDF build with designer support like our other controls:-


    namespace OpenNETCF.Windows.Forms


    {


          /// <summary>


          /// Extended ComboBox control.


          /// </summary>


          public class ComboBoxEx : ComboBox, IWin32Window


          {


                //windows messages


                private const int WM_SETREDRAW = 0x0b;


                private const int CB_SETCURSEL = 0x014E;


                private const int CB_DELETESTRING = 0x0144;


                private const int CB_INSERTSTRING = 0x014A;


     


                //native window handle


                private IntPtr m_handle;


     


                //databound collection


                private IBindingList thebindinglist = null;


     


                //is display updatable?


                private bool m_updatable = true;


     


                /// <summary>


                /// Gets the window handle that the control is bound to.


                /// </summary>


                public IntPtr Handle


                {


                      get


                      {


                            if(m_handle == IntPtr.Zero)


                            {


                                  this.Capture = true;


                                  m_handle = Win32Window.GetCapture();


                                  this.Capture = false;


                            }


     


                            return m_handle;


                      }


                }


     


                // Redraw code courtesy of Alex Feinman – http://blog.opennetcf.org/afeinman/PermaLink,guid,9305a1d9-e24e-4310-89e2-f80808076a37.aspx


     


                /// <summary>


                /// Maintains performance when items are added to the <see cref=”ComboBoxEx”/> one at a time.


                /// </summary>


                public void BeginUpdate()


                {


                      m_updatable = false;


     


                      Win32Window.SendMessage(this.Handle, WM_SETREDRAW, 0, 0);


                }


     


                /// <summary>


                /// Resumes painting the <see cref=”ComboBoxEx”/> control after painting is suspended by the <see cref=”BeginUpdate”/> method.


                /// </summary>


                public void EndUpdate()


                {


                      m_updatable = true;


                      Win32Window.SendMessage(this.Handle, WM_SETREDRAW, 1, 0);


                }


     


                //ComboBox doesn’t support ItemChanges in a datasource implementing IBindingList


                //The following workaround forces the list to update if an item is changed


     


                //data source has changed


                protected override void OnDataSourceChanged(EventArgs e)


                {


                      //remove event handler


                      if(thebindinglist != null)


                      {


                            thebindinglist.ListChanged-= new ListChangedEventHandler(ComboBoxEx_ListChanged);


                            //reset our handle to the bound data


                            thebindinglist = null;


                      }


     


                      //get the underlying ibindinglist (if there is one)


                      if(this.DataSource is IListSource)


                      {


                            IList thelist = ((IListSource)this.DataSource).GetList();


                            if(thelist is IBindingList)


                            {


                                  thebindinglist = (IBindingList)thelist;


                            }


                      }


                      else if(this.DataSource is IBindingList)


                      {


                            thebindinglist = (IBindingList)this.DataSource;


                      }


                     


                      if(thebindinglist != null)


                      {


                            //hook up event for data changed


                            thebindinglist.ListChanged+=new ListChangedEventHandler(ComboBoxEx_ListChanged);


                      }


                     


                      base.OnDataSourceChanged (e);


                }


     


                //called when a change occurs in the bound collection


                private void ComboBoxEx_ListChanged(object sender, ListChangedEventArgs e)


                {


                      if(m_updatable)


                      {


                            if (e.ListChangedType == ListChangedType.ItemChanged)


                            {


                                  //update the item


     


                                  //delete old item


                                  Win32Window.SendMessage(this.Handle, CB_DELETESTRING, e.NewIndex, 0);


                                  //get display text for new item


                                  string newval = this.GetItemText(this.Items[e.NewIndex]);


                                  //marshal to native memory


                                  IntPtr pString = MarshalEx.StringToHGlobalUni(newval);


                                  //send message to native control


                                  Win32Window.SendMessage(this.Handle, CB_INSERTSTRING, e.NewIndex, pString);


                                  //free native memory


                                  MarshalEx.FreeHGlobal(pString);


                            }


                      }


                }


     


                /// <summary>


                /// Get or Set the selected index in collection.


                /// </summary>


                /// <remarks>Overridden to overcome issue with setting value to -1 (http://support.microsoft.com/default.aspx?scid=kb;en-us;327244)</remarks>


                public override int SelectedIndex


                {


                      get


                      {


                            return base.SelectedIndex;


                      }


                      set


                      {


                            if(value == -1)


                            {


                                  Win32Window.SendMessage(this.Handle, CB_SETCURSEL, -1, 0);


                            }


                            else


                            {


                                  base.SelectedIndex = value;


                            }


                      }


                }


     


          }


    }



     


    So whats in the above code? Well it integrates Alex Feinman’s recent tip to implement Begin/EndUpdate methods to suspend redrawing when populating the control.


    Secondly if works around a bug in the DataBinding support for ComboBox. If you make an edit to an existing item in the bound collection the combo wont normally update (It does correctly react to additions and deletions).


    Thirdly it overcomes a known issue with resetting the selected index property (by assigning -1) which normally has to be done twice to take effect – details in this KB article – Thanks to Tim Wilson for posting this KB on the newsgroup.


    As a Bonus I’ve added a Handle property so you can get at the native control handle – which in conjunction with Win32Window methods allows you to tweak other aspects of the control.


    UPDATE: Changed the databinding code to update a single item via P/Invoke rather than refreshing the list. Changed the SelectedIndex to set via P/Invoke – avoids raising the SelectedIndexChanged event.

  • Some new OpenNETCF code

    I’ve uploaded to our online source browser some of the new code which will feature in the next Smart Device Framework release. This includes a new library for WindowsCE specific functionality, designed to match the new functionality available in the Microsoft.WindowsCE.Forms assembly in the .NET Compact Framework v2.0 which is currently in Beta with Visual Studio 2005.


    For Smartphone developers this includes the InputModeEditor which allows you to set a specific input mode for an edit control. Therefore when the control receives focus, the required input mode is automatically selected for the user – useful for numeric only fields or a field where you want to capture some form of text but know that T9 would be inappropriate.


    For Pocket PC 2003 Second Edition the SystemSettings class allows you to easily change the screen orientation with a single statement:-


    SystemSettings.ScreenOrientation = ScreenOrientation.Angle90;


    There are also some changes to existing classes – specifically the RegistryKey has had a number of useful changes:-



    • ToString now matches the behaviour on the full .NET Framework (In the current release there is a subtle difference in the format of the string returned)

    • CreateSubKey allows you to create volatile registry keys on supported Windows CE 5.0 systems.

    • GetValue now correctly returns DWORD values as an Int32 rather than UInt32, this is now consistent with the desktop and makes life a lot easier.

    • We have a full implementation of GetValueKind and the RegistryValueKind enumeration – this functionality is part of the full .NET Framework v2.0 Beta but we are making it available now for .NETCF v1.0

    All of this code is currently available in the source tree which you can browse online. Object model documentation has also been updated in the online library – which now exceeds 6000 individual pages. Expect to see all this code built into the next binary release.

  • New Visual Studio 2005 edition announced

    One of the stumbling blocks to getting into .NETCF development today is that the only supported tool for development is Visual Studio 2003 Professional or higher.


    Microsoft announced today at VSLive a new addition to the Visual Studio family in the 2005 version – Visual Studio Standard Edition. This will be a significantly cheaper version of Visual Studio but will include the full device development experience with both VB.NET and C# for managed code and C++ for native code.


    A more detailed comparison of the editions is available here:-


    http://lab.msdn.microsoft.com/vs2005/productinfo/productline/default.aspx


    The full press release can be viewed here:-


    http://www.microsoft.com/presspass/press/2004/sep04/09-13VSLiveOrlandoPR.asp

  • Keep your Smartphone backlight on

    If your application involves displaying screen content you probably have come across the issue where the screen backlight turns off after a few seconds of no keypresses. You can override this behaviour in your application using a couple of underdocumented API Power-Management functions. Here is a VB.NET snippet for .NETCF to keep the backlight on:-


    Namespace OpenNETCF.WindowsCE.Forms


     


    Public Class Backlight


     


    ‘ensure the power requirement is released


    Protected Overrides Sub Finalize()


    Release()


    End Sub


    ‘handle to the power requirement


    Private handle As IntPtr


     


    Private Enum PowerState


    PwrDeviceUnspecified = -1


    ‘full on


    D0 = 0


    ‘low power


    D1 = 1


    ‘standby


    D2 = 2


    ‘sleep


    D3 = 3


    ‘off


    D4 = 4


    PwrDeviceMaximum = 5


    End Enum


     


    ‘keep the backlight lit


    Public Sub Activate()


    ‘request full power


    handle = SetPowerRequirement(“BKL1:”, PowerState.D0, 1, IntPtr.Zero, 0)


    End Sub


     


    ‘release power requirement


    Public Sub Release()


    If handle.ToInt32() <> 0 Then


    Dim result As Integer


    result = ReleasePowerRequirement(handle)


    handle = IntPtr.Zero


    End If


    End Sub


    Private Declare Function SetPowerRequirement Lib “coredll.dll” (ByVal pvDevice As String, ByVal DeviceState As PowerState, ByVal DeviceFlags As Integer, ByVal pvSystemState As IntPtr, ByVal StateFlags As Integer) As IntPtr


    Private Declare Function ReleasePowerRequirement Lib “coredll.dll” (ByVal handle As IntPtr) As Integer


    End Class


    End Namespace


    Thanks to Paul O’Brien from MoDaCo for inspiring and helping test the code.

  • Upcoming Smart Device Chat


    There is a smart device programming chat starting in about 45 minutes from now:-






    MVP chat: .NET Compact Framework and Smart Device Programming
    You know them from the newsgroups! You love them for their immense knowledge! Please join these amazing Microsoft MVPs in this live chat regarding the .NET Compact Framework and the Smart Device Programming features of VS.NET. The .NET Compact Framework is a subset of the .NET Framework designed to allow .NET developer to target smart devices. The Smart Device Programming features of VS.NET allow embedded developers to target devices running the .NETCF.


    August 12, 2004
    10:00 – 11:00 A.M. Pacific time
    1:00 – 2:00 P.M. Eastern time
    17:00 – 18:00 GMT



    Event Reminders


    OutlookAdd to Outlook Calendar
    FriendTell a Friend

  • Casey posts detailed .NETCF v2.0 base class library changes

    The poster announced in the previous post gives a fairly high-level view of API changes between versions of the .NET Compact Framework. Casey Chesnut has posted a very detailed member-by-member comparison of v1 and v2 of the Compact Framework. This includes some exciting stats of the number of types, methods and properties supported in the base class libraries, and a complete alphabetical list of types.

  • New .NET Compact Framework poster available for download

    Its been a bit of a moving target but I’ve finally released a current version of the .NET Compact Framework Versions poster. The aim is to show a fairly detailed view of the functionality available in .NET Compact Framework v1.0, that which is expected in v2.0, and the functionality available in the next version of Windows Mobile. This is contrasted against the functionality available now in the OpenNETCF Smart Device Framework v1.2.


    The poster is divided into two main sections, the functionality which is unique to the Smart Device Framework is down the righthand side in the blue section. The increasing functionality in the .NET Compact Framework from v1.0, v2.0 and functionality specific to future Windows Mobile devices is arranged down the left in increasing intensities of green.


    A number of our Smart Device Framework classes are not wholly unique and either extend those available in the Compact Framework v1.0, or directly match those which will form a part of the Compact Framework v2.0. These are shown on the diagram in Blue – To avoid naming conflicts these have the “Ex” prefix on their name, and our root namespace is “OpenNETCF” as apposed to “System” or “Microsoft”.


    All of the future functionality is subject to change since these are based on functionality which has been announced and things may be added, removed or re-architected between now and the Compact Framework v2.0 (and future Windows Mobile) being released. Therefore it is likely that this diagram will be periodically updated to keep it accurate. Of course it is likely that during this time the Smart Device Framework may gain additional functionality too, so this just represents a current snapshot of the functionality available to developers. It is worth noting that the Compact Framework has a growing community of third-party controls and components which are not included on this diagram, many of these also add functionality to the v1.0 framework which either pre-empts v2.0 or future Windows Mobile specific functionality, or adds whole new functionality than that show on this diagram. There is a section in the Wiki which lists some of these (and you are welcome to contribute to the wiki)







    .NET Compact Framework Versions Poster .NET Compact Framework Versions (PDF 422kb)
    Compares the functionality available in the .NET Compact Framework v1.0, v2.0, Windows Mobile v.Next and the OpenNETCF Smart Device Framework.
    Since .NET Compact Framework v2.0 and Windows Mobile v.Next are not yet released these details are subject to change and are based on recent public announcements and the functionality available in Visual Studio 2005 Beta 1.