Mouse events on DirectX.AudioVideoPlayback Controls

This is the issue that finally pushed me over the edge to setting up this blog.  Normally when you have a coding problem, you find the answer fairly quickly and move on.  Sometimes though, the solution remains elusive.  Then you put the answer on a page of your own, to help out the next weary traveler.

Which bring me neatly to…

AudioVideoPlayback

I recently posted my lightweight media viewer, Picsie.  As well as images, it also handles some formats of videos.  The video playback was largely on a whim when I stumbled across the wonders of Microsoft.DirectX.AudioVideoPlayback.  The code to set it up and use it is endearingly simple:

Microsoft.DirectX.AudioVideoPlayback.Video videoPlayer
	= new Microsoft.DirectX.AudioVideoPlayback.Video(filename);
videoPlayer.Owner = videoPanel;
videoPlayer.Play();

Give it a video file and a control to draw on, and tell it to start.  Some form of error handling is probably wise too.

All well and good so far.  A few buttons to control playback, and you’ve got a basic but functional video player.  My problem came when I wanted the video to have the same control system as the rest of Picsie – left-double-click to centre, right-click to exit, drag to move.  These all worked for images, but when I came to test it on videos, only the drag code seemed to work.

It was pretty simple to diagnose.  MouseDown, MouseMove and MousePress all continued to function as before.  For reasons unknown, the Video object ate any Click, MouseClick, DoubleClick and MouseDoubleClick events.

At this point the Internet failed me.  No solutions, and I had two features no longer working:

  • Right-click to exit
  • Double-click to centre

Not able to find a way to get my events back, it was fairly trivial, if a little messy, to move the right-click code to the MouseUp event, with a little jiggery pokery to make sure it was a click and not a drag.  On the other hand, did I really want to write all the code to check for and handle double clicks myself?  No, no I didn’t.  Thankfully, I didn’t have to.  MouseEventArgs has a “Clicks” property (when did that appear, and why did nobody tell me?), which you can use to fake a double-click event:

private void Panel_MouseDown(object sender, MouseEventArgs e)
{
	if (e.Button == MouseButtons.Left && e.Clicks == 2) {
		//Double-click
	}
}

And there you go.  I never did find out why the Video object ate my events.  There’s probably a really simple solution somewhere, or I did something boneheaded, but more than one person faced this problem and never found an answer, so here’s mine.  Fake clicks and double-clicks.

tldr/

Problem: AudioVideoPlayback.Video eats MouseClick and MouseDoubleClick events.

Solution: Check for double-clicks using the MouseEventArgs “Clicks” property in the MouseDown/MouseUp events which aren’t eaten.


Picsie

Downloads – Alpha 1.10 (10 November 2015)

  • Installer (~75KB).  This will set up your file associations for you.
  • Standalone .exe (~200KB).  Use this if you already have Picsie installed and you just want the latest version.  Simply drop it over the existing executable.

Picsie is a very lightweight image and video viewer, with an emphasis on speed and minimal interface.   I primarily made it because while in the past I was perfectly happy with XnView, Quick Look on the Mac made it obvious how much Windows was missing something similar.

A while later I saw VJPEG which did pretty much exactly what I wanted.  Unfortunately it lacks a few features I like, so I set about making my own.
Read More →


A bit about me…

It’s a familiar story – I started programming when I was really young and never stopped.  My dad had a Dragon 32 – not the most ancient of computers, but pretty old by anyone’s standards – and let me play on it.  The idea of a toy that didn’t do anything by itself was pretty novel, and I set about learning how it all worked.  As I said, a pretty typical story for a software developer.

Skip forward 20-odd years, past the Spectrum, more PCs than I care to count, and two Macs, past Basic, Pascal, Delphi, a bit of web development and a plethora of languages I poked at university, and we arrive at Java and C#.  C# for preference.

My degree is in AI, but I spent several years teaching first years Java, then moved onto being a real scientist.  An actual Computer Scientist, rather than a developer.  Evidence-Based Software Engineering is a growing field, and will hopefully influence any and all aspects of software engineering, from agile methods to unit testing, to education.  I urge you to have a look at the website, and cringe at how little evidence we actually have on which to base technical and managerial decisions.

Back on topic, I’m finally moving into the real world, as a C# developer, so you can probably expect this blog’s subject matter to become more enterprisey as time goes by.

The vast majority of problems I’ll be documenting (and posing, when the Internet fails me, as is its wont) come from personal projects, which I’ll also be posting when I think they’re ready for public consumption – basically once they’re past the point of mockery :-)

My most recent project, a lightweight media browser (I know!  How innovative!  Trust me, I made it because I needed it), has spawned half a dozen possible posts, so expect more on that just as soon as I’ve found a name for it.


Just what the world needs…

Another blog.  About programming, no less, because there aren’t many of those on the Internet.

I’ve been intending to start this up for a while now, so now that I’m desperately rushing to finish my PhD, about to start a new job and trying to find time to finish and publish a two-year long case study on unit testing (woo, indeed), it seemed a logical time to start a new commitment.

In short, this blog is like many others, a place for me to put programming problems and solutions that had me scouring the web and picking people’s brains.  So, whenever I eventually find/invent/kludge a solution to a problem, it goes here, with examples, source code and downloads.

And if  I get enough of them then maybe, just maybe, the next person won’t have to search so hard.