A short guide to a better understanding of (the) windows


1. Control over the system windows



Introduction

   There are a couple of things that we use in our work with computers day by day, and somehow, because of the fact that we are getting more and more used to them we somehow forget to have a closer look to what makes them work the way they do.

   Sure we've seen CreateWindow API function thousands of times, and we used it to create many and various forms of windows for our applications.

   After creating our own windows we?ve started to manipulate them by moving them or shrinking them or maybe making them invisible.

   Have you ever thought about trying to manipulate system windows? Sure there are system windows: have you noticed the titlebar? Or the today screen? What do you think those are, huh?

Don`t just stand there, do something

   Nice to know that the today (desktop) interface is composed of so many windows. It would be nice to be able to control those also. Why? Well maybe you would like to hide the Titlebar a bit, or who knows, maybe it would be nice to move the Today Screen a bit to the right (any o2 AUI 4 users here? ;) ).

Technical data

   Our task for this article is to move the Titlebar ? to the middle of the screen.
   The titlebar window is created with the class identified by the string "HHTaskbar", and it has no title.

   Thanks to the powerful API functions, we can obtain its handle with a call to FindWindow:

   // Obtain the handle to the titlebar window
   HWND hTaskbar = FindWindow(TEXT("HHTaskbar"),TEXT(""));

   From this point things get quite easy, as working with HWNDs is something we very often do in our common applications.

   In order to place the titlebar to the middle of the screen we need to know the size of the screen first, as well as the size of the titlebar for keeping its proportions:

   // Get size of screen
   RECT g_rt;
   SystemParametersInfo(SPI_GETWORKAREA, NULL, &g_rt, FALSE);

   // Get original size of the titlebar plugin
   RECT c_rt;
   GetClientRect(hTaskbar, &c_rt);

   Now we have everything we need and we can move the titlebar to the middle of the screen:

   // Do the magic: Move the window to the vertical middle of screen
   if (hTaskbar)
      MoveWindow(hTaskbar,
         0, (g_rt.bottom - g_rt.top) / 2,
         c_rt.right - c_rt.left, c_rt.bottom - c_rt.top,
         0);

   Doing the check for validity of the hTaskbar is optional, MoveWindow will ignore the call if hTaskbar is NULL.

   MoveWindow and FindWindow were kind enough to work with handlers of windows belonging to other processes threads. There will be cases when we will not be this lucky, cases where because of the different memory zones we wont be able to interfeer into the foreign code as we did here. But hopefully there are plenty of solutions for those cases also, and also plenty of articles that will follow.

   Here is a snapshot of the screen with the titlebar moved to the middle of the screen:



   Your feedback is most welcomed, please feel free to contact me by mail or on the Teksoft Forums: www.teksoftco.com/forum

   Here is a link to a sample with the code descriebed in this article. You can run this code on WM2003 and WM2005. You can click to download: [sample.zip]


   Radu Motisan
   www.teksoftco.com
   5th of May, 2006