Windows Vista Sidebar Equivalent - c#

I'm trying to create an application that looks similar to the Windows Vista sidebar. There's an API that allows docking toolbars on the screen (AppBar), but it's not exactly what I'm looking for.
How can I attach a Form to the desktop and dock it to the side of the screen, but without preventing other windows from overlapping it?

With all the following options you get a Sidebar look-a-like (the code below is for a WPF Window):
//width of the sidebar
Width = 300;
//height (remember to add a reference to the System.Windows.Forms dll)
Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
//no window style means no border
WindowStyle = WindowStyle.None;
//not resizable
ResizeMode = ResizeMode.NoResize;
//allow a transparent sidebar
AllowsTransparency = true;
//change the color
Background = new SolidColorBrush(Colors.CadetBlue);
//set the opacity (how much transparent)
Opacity = 0.5d;
//offset from the top
Top = 0;
//offset from the left (calculated so it shows on the right side)
Left = SystemParameters.PrimaryScreenWidth - (double)GetValue(WidthProperty);
//set it the topmost window
Topmost = true;
//hide the icon from the taskbar
ShowInTaskbar = false;
Hope this helps!
Update:
Here's a similar solution for when you're using WindowsForms, altough with WPF you have much more possibilities! The differences are minor, everything explains itself. The last line I added hides the window taskbar-icon. Do not place the code in the constructor of the Form but in the Load-event, otherwise the Location will be wrong. In WPF this doesn't matter.
Width = 300;
Height = Screen.PrimaryScreen.Bounds.Height;
FormBorderStyle = FormBorderStyle.None;
BackColor = Color.CadetBlue;
Opacity = 0.5d;
Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width, 0);
TopMost = true;
ShowInTaskbar = false;

Related

White window flashes when using UsesPerPixelOpacity to make a wpf window transparent

I create a transparent window with the following code here is the link
HwndSourceParameters p = new HwndSourceParameters("TestWindow", 100, 100);
p.UsesPerPixelOpacity = true;
p.WindowStyle |= 0x10000000; // WS_VISIBLE
HwndSource hwndSource = new HwndSource(p);
hwndSource.RootVisual = ellipse;
It works ok. But a white window always flashes before the ellipse appears and the size of white window is same with the parameters of HwndSourceParameters. It seems the code will render a white background window and then refresh it into transparent background and rootvisual. Anything wrong?

Taskbar not hiding when single screen but hiding when multiple C#

I have the below code which I use which includes hiding the taskbar by my form. It works well if there are two screens connected to my computer but if there is only one screen the taskbar shows, I am not sure why?
FormBorderStyle = FormBorderStyle.None;
await Task.Delay(500);
this.WindowState = FormWindowState.Normal;
this.ActiveControl = textBox1;
StartPosition = FormStartPosition.Manual;
Location = new Point(0, 0);
var height = Screen.AllScreens.Max(x => x.WorkingArea.Height + x.WorkingArea.Y);
var width = Screen.AllScreens.Max(x => x.WorkingArea.Width + x.WorkingArea.X);
Size = new Size(width, height);
this.BringToFront();
this.AcceptButton = button1;
this.ControlBox = false;
this.TopMost = true;
this.Size = Size;
Rectangle ru = Rectangle.Union(Screen.AllScreens[0].Bounds , Screen.AllScreens[1].Bounds);
Bounds = ru;
You're getting an "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" error because of AllScreens[1] when you only have one screen connected there is only one value in the AllScreen array so index[1] is out of bound.
Change;
Rectangle ru = Rectangle.Union(Screen.AllScreens[0].Bounds , Screen.AllScreens[1].Bounds);
To;
var ru = Screen.AllScreens.Select(a => a.Bounds).Aggregate(Rectangle.Union);
Now because you're using a Select you wil only getting AllScreens[0] instead of guessing what indexes well be there this code now also works is you have three monitors or any other configuration of monitors.
Edit
Also, as a small side note if you're trying to block a user from doing anything outside of your program. This will not block them fully actions like pressing the windows key will still bring up the taskbar or give control back to the user.

Hide FlowLayoutPanel's Horizontal Scrollbar [duplicate]

I have a FlowLayoutPanel and there are multiple controls on it. I only want to scroll in vertical direction. But when I set AutoScroll = true, I got both Vertical and Horizontal Scroll bars. How could I disable the horizontal scroll bar and only keep the vertical scroll bar working?
Set AutoScroll to true
Set WrapContents to false.
Make sure the size is wider than the
controls' width plus the width of a vertical scrollbar.
The horizontal scrollbar should disappear. If it doesn't, please provide some more information.
Set AutoScroll to true.
Set WrapContents to false.
Set Padding Right to 10.
It's work pretty fine for me.
Here is how I implement to have multiple labels on a FlowLayoutPanel with wrap text(WrapContents = true), verticalscrollbar only.
I have a flowLayoutPanel1 on a form
Set properties of form and flowLayoutPanel1 like below:
form:
AutoScroll = True
FormBorderStyle = Sizable(default)
flowLayoutPanel1:
Anchor = Top, Left, Right
AutoSize = True
FlowDirection = TopDown
WrapContents = true
Implement this code on form class for testing
int coorY = 0;
public Form2()
{
InitializeComponent();
for (int i = 0; i < 100; i++)
{
flowLayoutPanel1.Controls.Add(new Label
{
Location = new Point(0, coorY + 20),
Font = new Font("Segoe UI", 10f),
Text = "I have a FlowLayoutPanel and there are multiple controls on it. I only want to scroll in vertical",
Width = flowLayoutPanel1.Width,
AutoSize = true
});
coorY += 20;
}
}
Vertical scrollbar in action

C#, WinForms - Change FormBorderStyle without the client area moving

I have a small tool window that normally has the FormBorderStyle to FixedDialog with no caption text and no control box so it looks like a border-less form with a raised 3d effect.
When the user moves the mouse over the tool window it changes from this border-less FixedDialog mode to a SizableToolWindow w/ caption text and a control box.
The result is the client area moving.
The following code works but i do not want to hard code the top/left delta and I assume it is different depending on what theme/os the user has
void Reposition()
{
var topDelta = 12; // this number is wrong, i have not found the right number for aero yet
var leftDelta = 3;
if (this.Bounds.Contains(MousePosition))
{
if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow)
{
this.Location = new Point(this.Location.X - leftDelta, this.Location.Y - topDelta);
this.ControlBox = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
}
}
else
{
if (this.FormBorderStyle == System.Windows.Forms.FormBorderStyle.SizableToolWindow)
{
this.Location = new Point(this.Location.X + leftDelta, this.Location.Y + topDelta);
this.ControlBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
}
}
}
Look into SystemParameters class. You will find the values you are hard-coding in your code there.

How to fit Windows Form to any screen resolution?

I work on VS 2008 with C#. This below code does not work for me. My form was designed in 1024 x 768 resolution.
Our clients laptop is in 1366 x 768 resolution. To solve this problem, I set below code in Form Load event:
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
but the form does not resize as per screen resolution and bottom of my form gets hidden or cut or I miss the scroll bar.
Is there any way to solve this problem? Please show me the syntax.
Can't you start maximized?
Set the System.Windows.Forms.Form.WindowState property to FormWindowState.Maximized
If you want to set the form size programmatically, set the form's StartPosition property to Manual. Otherwise the form's own positioning and sizing algorithm will interfere with yours. This is why you are experiencing the problems mentioned in your question.
Example: Here is how I resize the form to a size half-way between its original size and the size of the screen's working area. I also center the form in the working area.
On computers with multiple monitors, the user probably expects the form to open on the same screen that the mouse pointer is on. We can get it with Screen.FromPoint(Cursor.Position).
public MainView()
{
InitializeComponent();
StartPosition = FormStartPosition.Manual;
Rectangle screen = Screen.FromPoint(Cursor.Position).WorkingArea;
int w = Width >= screen.Width ? screen.Width : (screen.Width + Width) / 2;
int h = Height >= screen.Height ? screen.Height : (screen.Height + Height) / 2;
Location = new Point(screen.Left + (screen.Width - w) / 2, screen.Top + (screen.Height - h) / 2);
Size = new Size(w, h);
}
Note that setting WindowState to FormWindowState.Maximized alone does not change the size of the restored window. So the window might look good as long as it is maximized, but when restored, the window size and location can still be wrong. So I suggest setting size and location even when you intend to open the window as maximized.
Probably a maximized Form helps, or you can do this manually upon form load:
Code Block
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
And then, play with anchoring, so the child controls inside your form automatically fit in your form's new size.
Set the form property to open in maximized state.
this.WindowState = FormWindowState.Maximized;
int h = Screen.PrimaryScreen.WorkingArea.Height;
int w = Screen.PrimaryScreen.WorkingArea.Width;
this.ClientSize = new Size(w , h);
You can simply set the window state
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
simply set Autoscroll = true for ur windows form.. (its not good solution but helpful)..
try for panel also(Autoscroll property = true)
You can always tell the window to start in maximized... it should give you the same result... Like this: this.WindowState = FormWindowState.Maximized;
P.S. You could also try (and I'm not recommending this) to subtract the taskbar height.

Categories