Tweak DWM from your programs, Part 2

Following the first part of the Tweak DWM from your programs series, we are going to explore the actual usage of the Desktop Window Manager API in a Windows Forms-based program. The test application–included in the source package–also makes use of glassbarGlassBar, an extension to the existing control set.

Using GlassLib in your program

projectGlassLib is going to be main focus of this part of the series. It supersedes projectDwmWrapper by implementing a framework around glassbarGlassBar which can be used as a complement to your visually-rich forms. In order to use this library in your project, you have to do the following:

  1. Include a) a reference to the DLL in your project from Visual Studio: Project menu => Add reference, or b) the actual code files to your C# project (adding them as links (shortcuts) as opposed to copying them is more suitable if you wish include the files in multiple projects and/or wish to make changes to the code yourself).
  2. Remember that all of the functionality lies in the namespace_GlassLib namespace. You can use the using or Imports clause in the beginning of your C# or VB.NET code file, respectively, or the global imports if you code exclusively in VB.NET.

Exploring the GlassLib Test app

Let’s begin exploring the framework by opening the project file for projectGlassLibTest. Included in the GlassLib solution, it depends on the projectGlassLib project reference, which on its part has all the projectDwmWrapper code. Open the formfrmMain.cs file which is the form which is to be executed right at start up. As described in the first part, the first thing the class constructor public frmMain() should do is to set Dwm.ThrowExceptionTypes to DwmExceptionTypes.None in order to avoid any exceptions from being raised when DWM is not enabled or available on the OS, or if any function failed. The next thing the program does is to enable dragging from the Glass-extended area.

Dwm.Glass[this].DragExtendedArea = true; is useful if you want the extended area to act as the caption bar does, like in Windows Media Player. This is what the glassbarGlassBar toolbar does as well (more on this later in this part). toolbox Switch to Designer mode (View => Designer) and you can see the layout of the form: the usual Windows Forms buttons, combo and text boxes et cetera, but also glassbarGlassBar. To add the latter to your own forms, open the Toolbox (View => Toolbox) and drag it to your form (see figure on right). The toolbar will automatically dock to the bottom of it and extend Glass from this side of the form. You are free to dock it to the whole form (or container for that matter) or one of the other sides. Because DWM only supports Glass on top-level windows, in designer mode glassbarGlassBar falls back to UXTheme API in order to get the theme-specific background image or brush which is suitable for all non-Aero Glass themes such as Aero Basic, Luna (on Windows XP) or Windows Classic on any supported Windows version. On top of this, you can choose to write your own classGlassBarRenderer.

This feature provides two key benefits to your application’s design:

  • For Windows Vista systems and future OS with support for Glass: rich, seamless Glass UI
  • For Windows Vista systems where DWM is disabled or unavailable, and all previous Windows releases: seamless UI deriving from the appropriate Windows theme, including custom themes created with software such as WindowBlinds.

GlassBar

Managing the GlassBar items

Adding your items to GlassBar is quite easy. While in Design Mode, make sure that the Properties pane is visible (View menu => Properties Window to invoke it), select Items and expand the Collection Editor. You can add, remove and rearrange the items. For each one of them you can customize their:

property_thumb1Name Used in code only (does not appear in UI)
property_thumb1Enabled Enables/disables the item from the UI
property_thumb1Icon Preferably translucent PNG image 25×25 pixels in size
property_thumb1IsSeparator Denotes whether the item is a separator (its Icon will be disregarded)
property_thumb1Overlay Image, useful for ‘hover’ effects
property_thumb1Text Alternative text shown when the item is hovered
property_thumb1Visible Shows/hides the item

You can of course manage items programmatically using the property_thumb1Items property, for example adding one: Image img = Image.FromFile(“someFile.png”); GlassBarItem gbi = new GlassBarItem(“Sample Text” + i, img); glassBar.Items.Add(gbi); When the logic of your program requires an item be disabled or hidden, e.g. an unavailable feature, you can use property_thumb1Enabled or property_thumb1Visible to prevent users from clicking it.

Custom renderers

Internally GlassBar defines a standard renderer which on Windows Vista with Aero Glass enables draws on a translucent surface and otherwise falls back to the system-defined theme. If you wish to code a custom renderer for your needs, what you have to do is:

  1. Create a class deriving from (inheriting) from one of the following classes:
    • classGlassLib.Rendering.DefaultGlassBarRenderer if you wish to customize only specific parts of the default renderer (images, background etc)
    • classGlassLib.Rendering.GlassBarRenderer if you wish to build one from the ground up. This requires you to implement all of the methods and properties and is unlikely to be your choice.
  2. Decide which functions or images you would like to change. You can assign any image to property_thumb1MainPiece, RightSecondaryStates, AnimationStrip etc.
  3. The method_thumb1DrawBarBackground function is called every time the UI changes and needs to be redrawn. It is here where you can change most of the behavior of the default renderer and implement your own features.
  4. Assign an instance of your custom renderer to glassbarGlassBar
    • glassBar.Renderer = new CustomRenderer(); or to revert to default:
    • glassBar.Renderer = new GlassLib.Rendering.DefaultGlassBarRenderer();

The test app

There is a test program added to the package, project_thumb1GlassLibTest, whose code and compiled binary you can explore and experiment with GlassBar and DWM. If you have any comments, questions, wishes or just an opinion, I would be happy to hear it.


One Response to “Tweak DWM from your programs, Part 2”  

  1. 1 Création site internet bretagne


Leave a Reply