Tag Archives: WPF 4

Code

Find the transparent pixels in an image – WPF

Extracted from: http://random.tonywilliams.me.uk/post/392778744/find-the-transparent-pixels-in-an-image-wpf

The other day I had to create an image element that contained a png with transparent sections and have it display a hand cursor when the mouse was over the opaque sections.

The first thing I tried (and failed) was to have an image element with the cursor property set to “Hand” like so:

<Image Source="{Binding SomeImage}" Cursor="Hand" />

That obviously never worked so I was forced to look at each individual pixel and see if it was transparent. What I ended up with was a function that generated a bunch of coordinates.

This is that function:

private static Dictionary<Point, object> FindTransparentPixelCoordinates(BitmapSource source)
{
  var points = new Dictionary<Point, object>();

  // Convert the source if the pixel format is not what we expect
  if (source.Format != PixelFormats.Bgra32)
  {
    source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
  }

  int width = source.PixelWidth;
  int height = source.PixelHeight;
  var bytes = new byte[width * height * 4];

  source.CopyPixels(bytes, width * 4, 0);

  var position = 0;

  for (var y = 0; y < height; y++)
  {
    for (var x = 0; x < width; x++)
    {
      var byteOffset = position;

      // The pixel format is stored as 32-bits (4 bytes) with the last byte being the alpha

      if (bytes[byteOffset + 3] == 0)
      {
        points.Add(new Point(x, y), null);
      }

      position += 4;
    }
  }

  return points;
}
Code

Single instance app in WPF 4 with argument passing

Extracted from: http://random.tonywilliams.me.uk/post/483779269/single-instance-app-in-wpf-4-with-argument-passing

I was building a WPF app that required the use of JumpLists; the thing with JumpLists is that it calls your app’s exe with an argument which in turn creates a new instance of your app.

After a quick bit of googling I found a link [now dead] some code that restricts an app to a signle instance by using a mutex and passes the command argument supplied via named pipes.

Only portions of the code was recoverable, the rest I created my self.

Note that the code uses the .Net 4 Tasks, this can be replaced with threads if you want to use it in .Net 3.5. This also only passes the first argument through but can be easily changed to pass multiple.

namespace Your.App
{
  using System;
  using System.IO;
  using System.IO.Pipes;
  using System.Threading;
  using System.Threading.Tasks;

  public class SingleInstance : IDisposable
  {
    private readonly bool ownsMutex;
    private Mutex mutex;
    private Guid identifier;

    /// <summary>
    /// Occurs when [arguments received].
    /// </summary>
    public event EventHandler<GenericEventArgs<string>> ArgumentsReceived;

    /// <summary>
    /// Initializes a new instance of the <see cref="SingleInstance"/> class.
    /// </summary>
    /// <param name="id">The id.</param>
    public SingleInstance(Guid id)
    {
      this.identifier = id;
      mutex = new Mutex(true, identifier.ToString(), out ownsMutex);
    }

    /// <summary>
    /// Gets a value indicating whether this instance is first instance.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is first instance; otherwise, <c>false</c>.
    /// </value>
    public bool IsFirstInstance
    {
      get
      {
        return ownsMutex;
      }
    }

    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    public void Dispose()
    {
      if (mutex != null && ownsMutex)
      {
        mutex.ReleaseMutex();
        mutex = null;
      }
    }

    /// <summary>
    /// Passes the arguments to first instance.
    /// </summary>
    /// <param name="argument">The argument.</param>
    public void PassArgumentsToFirstInstance(string argument)
    {
      using (var client = new NamedPipeClientStream(identifier.ToString()))
      using (var writer = new StreamWriter(client))
      {
        client.Connect(200);
        writer.WriteLine(argument);
      }
    }

    /// <summary>
    /// Listens for arguments from successive instances.
    /// </summary>
    public void ListenForArgumentsFromSuccessiveInstances()
    {
      Task.Factory.StartNew(() =>
                              {

                                using (var server = new NamedPipeServerStream(identifier.ToString()))
                                using (var reader = new StreamReader(server))
                                {
                                  while (true)
                                  {
                                    server.WaitForConnection();

                                    var argument = string.Empty;
                                    while (server.IsConnected)
                                    {
                                      argument += reader.ReadLine();
                                    }

                                    CallOnArgumentsReceived(argument);
                                    server.Disconnect();
                                  }
                                }
                              });
    }

    /// <summary>
    /// Calls the on arguments received.
    /// </summary>
    /// <param name="state">The state.</param>
    public void CallOnArgumentsReceived(object state)
    {
      if (ArgumentsReceived != null)
      {
        if (state == null)
        {
          state = string.Empty;
        }

        ArgumentsReceived(this, new GenericEventArgs<string>() { Data = state.ToString() });
      }
    }
  }
}

Here is the Generic Event Args class

namespace Your.App
{
  using System;

  public class GenericEventArgs<TEventDataType> : EventArgs
  {
    /// <summary>
    /// Gets or sets the data.
    /// </summary>
    /// <value>The data.</value>
    public TEventDataType Data { get; set; }
  }
}

To use this class create an instance with a GUID for your app like so:

private static readonly SingleInstance SingleInstance = new SingleInstance(new Guid("24D910A1-1F03-44BA-85A0-BE7BC2655FFE"));

Then on Application_Startup run your logic for it

private void Application_Startup(object sender, StartupEventArgs e)
{
  if (SingleInstance.IsFirstInstance)
  {
    SingleInstance.ArgumentsReceived += SingleInstanceParameter;
    SingleInstance.ListenForArgumentsFromSuccessiveInstances();
	// Do your other app logic
  }
  else
  {
    // if there is an argument available, fire it
    if (e.Args.Length > 0)
    {
      SingleInstance.PassArgumentsToFirstInstance(e.Args[0]);
    }

    Environment.Exit(0);
  }
}

static void SingleInstanceParameter(object sender, GenericEventArgs e)
{
  // Inform app of new arguments
}
Code

WPF 4 & Spellchecking

Extracted from: http://random.tonywilliams.me.uk/post/516146370/wpf-4-spellchecking

Using WPF 4?

Want spell checking on your textbox?

BAM: SpellCheck.IsEnabled=”True”

I love it!

Code

WPF 4 Performance Troubles

Extracted from: http://random.tonywilliams.me.uk/post/745359131/wpf-4-performance-troubles

If you’re working with WPF 4 and are having some performance trouble in regards to your app then try this in your xaml:

CacheMode=”BitmapCache”

Works brilliantly with the data templates for items in an ItemsControl/List etc.

More info here: BitmapCache

Code

Working with Touch (in .Net 4.0)

Extracted from: http://random.tonywilliams.me.uk/post/745164436/working-with-touch-in-net-4-0

For the past several months I’ve been hammering away at the touch app for Graphic.ly and I’ve picked up a few hints and tricks along the way.

The first tip is to get a decent touch monitor or at least a touch machine that you can develop on. I’m using the Dell SX2210T monitor and couldn’t be happier; within seconds you’re ready to do some touch magic!

My second tip is to use the Surface Toolkit from Microsoft. This however will limit your app to Windows 7 only where as if you choose to go with other libraries for touch your apps may work in older versions but just not respond to touch.

If you plan to go with this library (and I recommend you do) there here are some helpful links:

As you can see from the Controls link there are a number of controls to play with; the two that are mainly used in the Graphic.ly app are the SurfaceWindow andSurfaceScrollViewer.

The SurfaceWindow provides UI responses to touch via the Touch Visualizations as well as display on a touch monitor in a multi-monitor setup.

The SurfaceScrollViewer simply adds alot of touch magic to the ScrollViewer; anywhere you could use a ScrollViewer then use the SurfaceScrollViewer. There is not much to do in terms of quickly getting touch on your app when you use the control from the toolkit library. In the Graphic.ly app the Store, Collection and Activity Stream are all examples of the SurfaceScrollViewer in action.

The problem I’m having with the SurfaceScrollViewer is having them nested; if you checkout the Store/Collection you’ll see I have rows of SurfaceScrollViewers. Swiping back and forth works fine but I just cannot get it to work up and down. Seems to be the child SurfaceScrollViewer handles all the touch events and won’t pass it along, does this mean nested SufaceScrollViewers will never work? Nope, this just means another touch challenge to conquer 😉

My 3rd and final tip (for the moment) is this

“e.Cancel();”

If you have hooked up some TouchEvents AND some ManipulationEvents then you’ll know that the ManipulationEvents prevents the TouchEvents from firing; in other words you can swipe but not press. In the “ManipulationCompleted” event I check to see if the change is small enough to consider it not a swipe; “e.Cancel()” will cancel this event and let the TouchEvents bubble through.