Pretty Tooltips with WinForms

1. August 2010

For the current project at SKOV, I was in need of a tooltip window that can show a lot of textual information.

Apart from this requirement, the tooltip should support a gradient background, have round borders, be semi transparent and cast a soft shadow. To achieve this, one must use alpha blending to "softly" blend the border, the shadow and the transparency to the underlying surface.

WinForms have by default support for alpha blending on a drawing surface, e.g. a Form, so drawing a semi transparent gradient tooltip that casts a soft shadow is no problem. However, blending the tooltip on top of another window, such as the desktop or an underlying Window is rather troublesome, as WinForms apparently has no direct support for this. Instead one must rely on native Win32 to achieve this, using layered windows which were introduced in Windows 2000, as described in this MSDN post: http://msdn.microsoft.com/en-us/library/ms997507.aspx.

I have implemented the stuff described in the MSDN post, and made a quick test application showing the effect. The tooltip is drawn as a rectangle with round corners with a 2 pixel border. A soft shadow is also drawn.

image

The procedure (simplified) is as follows:

  1. Apply the WS_EX_LAYERED style to the Window, to make it a Layered Window.
  2. Create a 32bit Argb bitmap which encompasses the whole Window including shadow.
  3. Set the alpha value of the border and background colors to the desired opacity value.
  4. Draw the shadow, background and border to to the bitmap.
  5. Draw the content, e.g. text showing the mouse position.
  6. Create a compatible device context (DC) and copy the bitmap to it.
  7. Call UpdateLayeredWindow with the DC, blending mode, etc. as arguments.

 

I have included a demo project, with C# code, showing the tooltip in action.  Anyone is free to use the code as they see fit.

The demo project can be downloaded from here: APrettyTooltip 1.0.zip (43.11 kb)

Enjoy!

Programming in General , , , , , , , , ,

CppUnitLite - a simple unit testing framework

9. April 2010

I am currently implementing a ray tracer for Windows Mobile 6.0 that we intend to use for our semester project. Our semester project deals with Distributed Rendering on Mobile Devices, using ray tracing. 

We have chosen to follow the Test Driven Development (TDD) method where test cases are written before the actual coding begins. TDD "requires" that we employ some form of automated testing framework to automatically run tests to verify the test cases. As we are using C++, we have a multitude of choices with regard to the Unit testing framework.

I have come across CppUnitLite which is a very simple unit testing framework for C++, without any fancy features (a comparison of different frameworks can be found here: http://gamesfromwithin.com/exploring-the-c-unit-testing-framework-jungle)

Simply put, CppUnitLite allows one to fast and easily write unit tests, though not very object orientated (not a always a bad thing) as the example shows:

TEST (GetLength_0_1,Block)
{
    Block block = Block(0,0,100,100);
    CHECK(block.GetLength() == 10000);
}

The TEST macro is used to denote a unit test. The macro takes two arguments, the name of the test and the category of the test. In the example above, I have a Block class which has a GetLength method. The CHECK macro allows assertions to be made, e.g. to assert that the given Block has the length of 10000.

If any of the assertions fail, the console will tell me at exactly what line the assertion failed. This even works on Windows Mobile 6.0, as long as a debugger is attached to the device where the unit testing framework is running.

Programming in General ,