Using Visual Studio 2008 and VB.NET ...
I've created a form (OpaqueForm), which is an intermediary form between other forms which I will open with ShowDial开发者_开发知识库og. The idea is that when I want to show a form using .ShowDialog, this OpaqueForm, with an opacity other than 100%, sits in between the main form and the dialog form, effectively "greying out" the underlying main form.
The OpaqueForm has the FormBorderStyle property set to None, and accepts in the constructor a Form object on which it invokes .ShowDialog. The effect works fine, but there is one caveat. The task bar is also covered by the OpaqueForm; I am assuming because it has a FormBorderStyle of None and a WindowState of Maximized.
I don't want the OpaqueForm to cover the Task Bar, because it would be impolite to have my modal form lock out a user from switching between tasks. How could I go about preventing OpaqueForm from covering the Task Bar, too, while still using a FormBorderStyle of None?
Not sure I see how this could happen. Just make sure that the overlay is displayed with Show(owner) so that it is always on top and that it has the exact same Size and Location as the overlayed form.
You'll find sample code for such an overlay in my answer in this thread.
Why not put an "opaque" panel over the top of the other form. It doesn't make sense to make the whole user window opaque. Because if the application isn't running maximized, they'll want to click to other applications.
Set the size of the form's size to the screen's working area.
Dim f as New Form()
f.FormBorderStyle = FormBorderStyle.None
f.Location = New Point(0, 0)
f.Size = My.Computer.Screen.WorkingArea.Size
this will do the trick.
Edit
If you need to place the Opaque Form on the Primary Screen, use the following code:
For Each scr In Screen.AllScreens
If scr.Primary = True Then
Dim f As New Form()
f.FormBorderStyle = FormBorderStyle.None
f.Location = New Point(0, 0)
f.Size = scr.WorkingArea.Size
End If
Next
If you want to place a form on every screen, just skip checking for the primary screen by removing the conditional.
I had a .ShowDialog() statement that caused the child form to show up big enough that it covered the taskbar.
As it turns out, the problem was that I had both MaximizeBox set to False in the child form code. Not sure why, but changing it to have MaximizeBox = True made the maximized form stop encroaching into the taskbar area.
Please try this piece of code:
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.SuspendLayout();
//
// TransparentForm
//
this.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.ControlBox = false;
this.DoubleBuffered = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Location = new System.Drawing.Point(70, 70);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "TransparentForm";
this.Opacity = 0D;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.Text = "TransparentForm";
this.TransparencyKey = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.Load += new System.EventHandler(this.TransparentForm_Load);
this.ResumeLayout(false);
}
Now include the following code in your TransparentForm.cs file:
private void TransparentForm_Load(object sender, EventArgs e)
{
Form f = Application.OpenForms[<yourapplication's main form>];
this.Width = f.Width;
this.Height = f.Height;
this.Location = f.Location;
this.Opacity = 0.01D;
}
Hope this helps..
Muthaiah B
精彩评论