alacrity in action
| Tags: , , ,

Ever since I started writing windows applications, even though most of them were not in C or C++, almost every time I would refer back to Charles Petzold’s Programming Windows. Whether it was Visual Basic, Delphi, .NET or even Java, there would eventually come a point, where the only way to achieve something would be to dig into the Win32 API and hook into the message loop directly.

I don’t know why I thought writing a WPF application in .NET 4 for Windows 7 would be any different.

If you start with a default empty window it is rendered nicely with a drop shadow around it (like all other windows).

However, I wanted to achieve an alternative layout, without the frame but with the shadow still on. Unfortunately setting WindowStyle="None" creates a rather blunt rectangle.

There is always the option to do something inside the client window to pretend your window actually does have a shadow but it is somewhat messy and just not as pretty (or perhaps I wasn’t ready to tinker with the Canvas and Effects enough). So will say it also comes with a performance penalty (since you would require transparent background).

Finally, with a little bit of Interop spice by calling Desktop Window Manager APIs I got the desired effect:

Mind you it only works if DWM is available (that is in Vista and Windows7) but I’m fine with that.

Here is the required code:

[DllImport("dwmapi.dll", PreserveSig = true)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);

void ShellWindow_SourceInitialized(object sender, EventArgs e)
{
var helper = new WindowInteropHelper(this);
int val = 2;
DwmSetWindowAttribute(helper.Handle, 2, ref val, 4);
var m = new Margins
{
bottomHeight = -1,
leftWidth = -1,
rightWidth = -1,
topHeight = -1
};

DwmExtendFrameIntoClientArea(helper.Handle, ref m);
}

If you want to dig deeper there is WPF Shell Integration Library that does some of this heavy lifting for you.

P.S. Seems like Java folks have similar challenges.


| Tags: ,

Photo by 500CPMI’m sure you have, many times, seen recruitment for software development positions that expected candidates to have x number of years experience with a particular technology. Best if it’s a litany of technologies (ignore when they ask for 5 years with a technology that has only been out for 2). If you need a developer that’s what you do. If you’re a recruitment agent that’s all you can do (OK, with some exceptions).

Ultimately, employers who state such requirements make one fundamental mistake. They think the only factor limiting how quickly they create software is how quickly their developers can type.

Yes, if you have worked with NHibernate for 2 years you will write the mapping file more quickly and you might already know what fetch strategy to use. You will not, however, deliver good, valuable software more quickly. The speed at which you type has never been, is not, and I don’t think will ever be a limiting factor, ever. It’s like thinking Picasso would have created more works of art if only he could have made his brush strokes more quickly, or that Pollock would have made a bigger impact on the art world if he could pour paint more quickly. Wrong.

There are many other things to look for in a good developer (more on that another time perhaps) but for now, start with people who are smart and get things done not the ones who have experienced a specific technology for more years and thus can write the code for that technology more quickly.


| Tags: , ,

I found myself today needing some simple DTO classes for unit tests to extend existing services and controllers. We’re using .NET 3.5 so Object and Collection Initializers come in really handy for mocking out services and providing data. If the object graph is somewhat bigger crafting the data by hand is quick but somewhat cumbersome.

Yet we usually have that data somewhere and getting a hand on an instance that might just need a bit of tweaking later is easy. I really wanted something that would serialise an instance of an object (perhaps obtained while debugging) into object initialiser syntax that I can paste back into the code.

I was disappointed I couldn’t google anything useful, but then perhaps my googling skills are not as good as my coding skills so I wrote something quickly instead. A bit of reflection and recursion and it’s there.

You can download it from CodePlex http://objectserialiser.codeplex.com/

It is extremely sketchy but does the job for me.

Is anything better out there? Ideally, this could now be hooked into Visual Studio somehow to make the job even easier. Unfortunately Code Visualisers won’t do the job as they can’t be registered for “object”.