Smart Device Framework 2.2 Released

It’s been a while since I have posted anything but I’ve been keeping busy
with customers, product development, sales, marketing and a new member to the
family.  There have been lots of things happening since my last post and we have definitely been keeping busy!

Recently we released version 2.2 of the Smart Device Framework.  We basically went through a lot of the bugs in our bugzilla database and added some new features (Chris talks about them here).  Currently it is for Visual Studio 2005 and Compact Framework 2.0. We are working on SDF 2.2 for Visual Studio 2008 and SDF3.5 for Visual Studio 2008 and Compact Framework 3.5. 

One of the coolest features we have added is NetworkInformation and MemoryMappedFiles.  Chris has some posts on using NetworkInformation here, here and here.

Remember to download the free community edition to use in your projects.

Trapping Cut,Copy,Paste and Clear Using Compact Framework and Smart Device Framework 2.1

As I mentioned in a previous post, this week we released Smart Device Framework 2.1.  A new feature in the product that we added to support an new upcoming control (that is currently in the final stages of development before we release a beta) are new Cut, Copy, Paste and Clear events for the OpenNETCF.Windows.Forms.TextBox2 class.


You are probably thinking “who cares what do I need that for?!”  Think of a TextBox where you have sensitive information for example a Social Insurance Number, Drivers License Number or Credit Card Number say in a mobile data collection or mobile billing application.  You could have a scrupulous person doing a “Copy” (CTRL-C using the input panel) and pasting (CTRL-V again with the input panel) that information into a webpage opened using Internet Explorer Mobile and emailing that to themselves at the end of the day.  It may be difficult to do on a 240×320 screen but it can be done (and has happened in one of my first projects I worked on).


So using a more lighthearted example, let’s take a look at creating a NumericTextBox.  There are two ways to implement this control.  The first one is to modify the window style of a TextBox using the following.


private void SetNumeric()
{
    int style = OpenNETCF.Win32.Win32Window.GetWindowLong(this.Handle, OpenNETCF.Win32.GWL.STYLE);
    style |= (int)OpenNETCF.Win32.ES.NUMBER;
    OpenNETCF.Win32.Win32Window.SetWindowLong(this.Handle, OpenNETCF.Win32.GWL.STYLE, style);
}


The problem with this was it *only* accepted numbers.  There are some cases where you need a decimal place, but by modifying the TextBox style to ES_NUMBER, all you get is the ability to enter numbers.


The second option is to create your own control and override the OnKeyPress method and enter your custom logic. There are plenty of samples for creating a control like this but there is a great one here on MSDN specifically for the Compact Framework


One major flaw with this implementation is a user could always paste text into the TextBox and have no control over what is displayed.  Using OpenNETCF.Windows.Forms.TextBox2.Paste event allows you to ‘clean’ the pasted text of any unwanted characters.  Here are the new events exposed by TextBox2:


// Summary:
//     Occurs when a WM_CLEAR message is sent to the textbox
public event CancelEventHandler Clearing;
//
// Summary:
//     Occurs when a WM_COPY message is sent to the textbox
public event CancelEventHandler Copying;
//
// Summary:
//     Occurs when a WM_CUT message is sent to the textbox
public event CancelEventHandler Cutting;
//
// Summary:
// Occurs when a WM_PASTE message is sent to the textbox
public event CancelEventHandler Pasting;


 


All events use the CancelEventHandler Delegate which allows you to cancel the whole operation and not allow the WM_* message to flow to the underlying native control.  Let’s take a look at implementing this new functionality for the NumericTextBox. 


Here is the sample form we will be using to test the new functionality:



We have three ways to set text into the NumericTextBox:



  1. Using CTRL-C and CTRL-V keys via the Input Panel
  2. Sending a WM_COPY message to the lower TextBox and then a WM_PASTE to the NumericTextBox
  3. Setting the Text property of the NumericTextBox

First we have the OnKeyDown event.  Here we check to see if a CTRL key is pressed:


//Added this to allow for CTRL keys to be pressed
protected override void OnKeyDown(KeyEventArgs e)
{
    base.OnKeyDown(e);

    if ((e.Modifiers & (Keys.Control | Keys.Alt)) != 0)
    {
        // Let the edit control handle control and alt key combinations
        m_isCtrlKey = true;
    }
}


Next we have the OnKeyPressed event.  Here we validate the text being entered:


// Restricts the entry of characters to digits (including hex), the negative sign,
// the decimal point, and editing keystrokes (backspace).
protected override void OnKeyPress(KeyPressEventArgs e)
{
    base.OnKeyPress(e);
   
    if (!m_isCtrlKey)
    {
        if (!ValidateKey(e.KeyChar))
        {
            e.Handled = true;
            OpenNETCF.Media.SystemSounds.Beep.Play();
        }
    }
    else
        m_isCtrlKey = false;
}


Here is our logic to only allow numeric characters and/or decimals, spaces etc.


 

/// <summary>
///
This was originally in OnKeyPress in the MSDN sample but moved
/// here so we can use in the Paste() method
/// </summary>
///
<param name=”key”></param>
///
<returns></returns>
private bool ValidateKey(char key)
{
NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
string groupSeparator = numberFormatInfo.NumberGroupSeparator;
string negativeSign = numberFormatInfo.NegativeSign;

string keyInput = key.ToString();
bool ret = false;
if (Char.IsDigit(key))
{
// Digits are OK
ret = true;
}
else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
keyInput.Equals(negativeSign))
{
// Decimal separator is OK
ret = true;
}
else if (key == ‘\b’)
{
// Backspace key is OK
ret = true;
}
//else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
//{
// // Let the edit control handle control and alt key combinations
//}
else if (this.allowSpace && key == ‘ ‘)
{
ret = true;
}

return ret;
}

private string CleanString(string value)
{
if (value == null)
return “”;

StringBuilder sb = new StringBuilder();
for (int x = 0; x < value.Length; x++)
{
if (ValidateKey(value[x]))
{
sb.Append(value[x]);
}
}

return sb.ToString();
}


The ValidateKey() method is taken from the MSDN sample.  CleanString() method is used by our overridden Text property and our OnPaste override.


public override string Text
{
    get
    {
        return base.Text;
    }
    set
    {
        value = CleanString(value);
        base.Text = value;
    }
}


protected override void OnPaste(System.ComponentModel.CancelEventArgs e)
{
    //When something is pasted in we need to verify that they are numeric
    string pasted = Clipboard2.GetText();

    //Insert the char where the cursor currently is
    this.Text = this.Text.Insert(this.SelectionStart, CleanString(pasted));

    //Optional. Clear the clipboard so it can’t be used anymore
    Clipboard2.Clear();

    //Optional. Prevent any event handlers from being called
    e.Cancel = true;

    //Call the base
    base.OnPaste(e);
}


The Text override property is straight forward.  OnPaste is the new virtual method available in TextBox2:


protected virtual void OnPaste(CancelEventArgs e);


By overriding OnPaste, we now have the ability to get what has been copied into the clipboard using OpenNETCF.Windows.Forms.Clipboard2 and inserting the ‘cleaned’ text into our new NumericTextBox.


Now we have a nice simple way to stop unwanted characters from being pasted into our TextBox2 control and a nice clean way to know when a Clear, Cutting or Copying operation is being done within our control.


Download the full source for the NumericTextBox here and remember you need the Smart Device Framework 2.1 (either Community Edition or Extensions for Visual Studio) for this sample to work.


Enjoy!

Smart Device Framework 2.1 Released

We have been fairly busy here with consulting work, product development, website upgrades, new online store etc etc.  As I hinted at earlier we were in the process of releasing v2.1 of the Smart Device Framework. 


This week we finally released Smart Device Framework 2.1.  Although it’s a minor release and have cleared all bugs in our bugzilla database we have added some new features.  Nick Randolph decided to take the new release for a spin and talks about his experience here. Even though we have just released v2.1, Chris is already talking about v2.2 features here and here!  Thanks to Neil, the Smart Device Framework Extensions for Visual Studio has a much cleaner install, more integration with Visual Studio and start menu shortcuts to the help, source, samples etc.  As usual, we still offer the Smart Device Framework Community Edition free of charge.


For a list of new changes to the library see here.  One big change we did is separate the OpenNETCF.WindowsCE.Messaging into it’s own assembly OpenNETCF.WindowsCE.Messaging.dll from OpenNETCF.WindowsCE.dll.


We have also reintroduced support for the OpenNETCF.Windows.Forms.Signature control.  Unfortunately it slipped through the cracks with the 2.0 release (mainly my fault as I was responsible for OpenNETCF.Windows.Forms namespace) but it is now back in the 2.1 release with design time support. 


For OpenNETCF.Windows.Forms.OwnerDrawnList we added a DrawMode property to keep it in line with the desktop framework.  If you use the ListBox2 or SmartList and have some custom drawing code you may want to test your code if you are going to use Smart Device Framework 2.1.


Another cool feature we added to OpenNETCF.Windows.Forms.TextBox2 was the ability to be notified when a Cut, Copy, Paste or Clear event is sent to the TextBox.  I have a seperate post on that so if you are interested in that stay tuned 🙂


If you are a current customer of either the Extension or Community Edition, our online store now has the ability to accept product reviews. Let us know what you like, don’t like and want to see in the next version.  We are already adding features to v2.2 but are always interested in what our customers and the community want to see. 

Extracting EXIF Tags using Compact Framework

We got a request from a customer on how to extract EXIF Tags from an image taken with a camera using .NET Compact Framework and the Smart Device Framework. 


The OpenNETCF.Drawing.Imaging namespace in the Smart Device Framework does provide this data but if you are not familiar with the namespace it may be a little tricky getting the information.


Basically what you have to do is get an IImageDecoder object using the ImagingFactory class.  From there call ImageUtils.GetAllProperties(decoder) which returns an array of ImageProperty[] objects.


Here is the bulk of the code:


StreamOnFile st = new StreamOnFile(openFileDialog1.FileName);
IImageDecoder decoder = null;
ImagingFactory factory = new ImagingFactoryClass();
factory.CreateImageDecoder(st, DecoderInitFlag.DecoderInitFlagNone, out decoder);
ImageProperty[] props = ImageUtils.GetAllProperties(decoder);

foreach (ImageProperty prop in props)
{
   //For Specific tags see ImageTag enum. 
   textBox1.Text = prop.Id.ToString() + “: “ + GetValue(prop) + “\r\n” + textBox1.Text;
}
decoder.TerminateDecoder();


And the output:



Download the sample application here.