Tuesday, July 26, 2011

New Component Available: Task Bar Docker

 
Taking a breather for a moment and since I decided to take up blogging again, here is a little piece about a component I wrote last night.  I am working on yet another tool and this one lives in the system tray.  I wanted the main window to dock to the task bar. I looked around these here interwebs and found a lot of half-way-there implementations but I couldn’t find one that handled multiple monitors,  changing the primary monitor, moving multiple monitors around, etc.  Most of them ended up with incorrect window coordinates.  So I slapped one together.  You can download it from here.

I have a dual monitor system.  Here is how I have them setup:

image 

The docker has to support multiple monitors, switching main monitors, and handle redocking correctly if the task bar moves.  Here goes…

First, the sample app (which ships with the component) shown undocked on my monitor space:

image

Notice the whitespace.  That is because my monitors are at different resolutions and I have them lined up so swing the mouse to and from monitors crosses the barrier between the two monitors correctly.

Dock Scenario #1 = Taskbar docked, bottom, main monitor

image

Passed!

Dock Scenario #2 = Taskbar docked, right, main monitor

image

Passed!

Dock Scenario #3 = Taskbar docked, top, main monitor

image

Passed

Dock Scenario #4 = Taskbar docked, left, main monitor

image

Passed!

Dock Scenario #5 = Taskbar docked, bottom, secondary monitor

image

Dock Scenario #6 = Taskbar docked, right, secondary monitor

image

Passed!

…Okay so you probably don’t need to see all the scenarios and you get the idea.  Here is the math:

   1: private void PostionLeft()
   2: {
   3:     Point taskBarLocation = TaskBar.GetTaskBarLocation();
   4:     Screen taskBarScreen = Screen.FromHandle(TaskBar.GetHandle());
   5:     Size size = TaskBar.GetTaskBarSize();
   6:  
   7:     _Form.Left = taskBarScreen.Bounds.X + size.Width - (_BorderSize * 2);
   8:     _Form.Top = (taskBarScreen.WorkingArea.Height + taskBarLocation.Y + (_BorderSize * 2)) - _Form.Height;
   9: }
  10:  
  11: private void PostionTop()
  12: {
  13:     Point taskBarLocation = TaskBar.GetTaskBarLocation();
  14:     Screen taskBarScreen = Screen.FromHandle(TaskBar.GetHandle());
  15:  
  16:     _Form.Top = taskBarScreen.WorkingArea.Y + _BorderSize;
  17:  
  18:     _Form.Left = (taskBarScreen.Bounds.Width + taskBarLocation.X) - (_Form.Width - (_BorderSize * 2));
  19: }
  20:  
  21: private void PostionRight()
  22: {
  23:     Point taskBarLocation = TaskBar.GetTaskBarLocation();
  24:     Screen taskBarScreen = Screen.FromHandle(TaskBar.GetHandle());
  25:  
  26:     _Form.Left = (taskBarLocation.X) - (_Form.Width - (_BorderSize * 2));
  27:     _Form.Top = (taskBarLocation.Y + taskBarScreen.Bounds.Height + (_BorderSize * 2)) - _Form.Height;
  28: }
  29:  
  30: private void PostionBottom()
  31: {
  32:     Point taskBarLocation = TaskBar.GetTaskBarLocation();
  33:     Screen taskBarScreen = Screen.FromHandle(TaskBar.GetHandle());
  34:  
  35:     _Form.Left = (taskBarScreen.Bounds.Width + taskBarLocation.X) - (_Form.Width - (_BorderSize * 2));
  36:  
  37:     _Form.Top = taskBarLocation.Y - (_Form.Height + _BorderSize);
  38: }

Enjoy!

Colby

No comments:

Disclaimer

Content on this site is provided "AS IS" with no warranties and confers no rights. Additionally, all content on this site is my own personal opinion and does not represent my employer's view in any way.