Does anyone know how to dock a window to a desktop? Pretty much, I want to be able to have my form object attach itself to the left or right and increase its height to match the screen's height when it's within a specified margine (right now I have it at 10px), otherwise go back to its old size.
Right now, I have a Form object called DockedForm, that implements an event handler on moving:
private void DockedForm_Move(object sender, System.EventArgs e)
{
if (m_storedSize == Size.Empty)
{
m_storedSize = Size;
m_docked = false;
}
if (Screen.AllScreens.Length == 1)
{
if ((Left <= m_mgn) &&
((this.m_pos & WindowDockPositions.Left) != 0))
{
Left = 0;
if (!m_docked)
{
if (m_storedSize.Height != Screen.PrimaryScreen.Bounds.Height)
{
if (Size.Height != Screen.PrimaryScreen.Bounds.Height)
{
m_storedSize = Size;
}
}
else
{
m_storedSize = Size;
}
m_docked = true;
}
Invoke(new SetSizeCallback(setSize),
new object[] { new Size(Width, Screen.PrimaryScreen.Bounds.Height) });
Top = 0;
}
else if ((Right >= (Screen.PrimaryScreen.Bounds.Width - m_mgn)) &&
((m_pos & WindowDockPositions.Right) != 0))
{
Left = (Screen.PrimaryScreen.Bounds.Width - Width);
if (!m_docked)
{
m_storedSize = Size;
m_docked = true;
}
Top = 0;
Invoke(new SetSizeCallback(setSize),
new object[] { new Size(Width, Screen.PrimaryScreen.Bounds.Height) });
}
else
{
if (m_docked)
{
#if DEBUG
T.WriteLine("Undocking", "Status");
#endif
Invoke(new SetSizeCallback(setSize),
new object[] { m_storedSize });
this.Width = m_storedSize.Width;
this.Height = m_storedSize.Height;
m_docked = false;
Invalidate();
}
}
}
}
Two problems with this:
1.) It flickers when docking to the side. By "flickers" I mean it looks like it can't decide whether to keep its old size or new.
2.) Once it's been resized, dragging the window away from the side does nothing. I see through a trace that the old size is actually set to the window's size. However, the window's actual size does not shrink.
Any thoughts?
I'm pretty sure there is an API for this, at least it seems like it is something that should be handled by the window manager. A quick search on google reveals http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_int/shell_int_programming/appbars.asp