我想为叠加效果创建一个半透明的表单。表格应该透露。 这是我尝试这样做的方式,但它没有得到半透明的形式。所以请帮助我。
Form mMask = new Form();
mMask.FormBorderStyle = FormBorderStyle.None;
mMask.BackColor = Color.DarkGray;
mMask.Opacity = 0.10;
mMask.Height = this.ClientRectangle.Height;
mMask.Width = this.ClientRectangle.Width;
mMask.Top = 0;
mMask.Left = 0;
mMask.Text = this.Text;
mMask.AllowTransparency = true;
mMask.ShowInTaskbar = false;
mMask.StartPosition = FormStartPosition.Manual;
mMask.TopLevel = false;
this.Controls.Add(mMask);
mMask.Show();
mMask.BringToFront();
请指导我,谢谢。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace dialog
{
public class MaskedDialog : Form
{
static MaskedDialog mask;
static Form frmContainer;
private Form dialog;
private UserControl ucDialog;
private MaskedDialog(Form parent, Form dialog)
{
this.dialog = dialog;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Black;
this.Opacity = 0.50;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Size = parent.ClientSize;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
parent.Move += AdjustPosition;
parent.SizeChanged += AdjustPosition;
}
private MaskedDialog(Form parent, UserControl ucDialog)
{
this.ucDialog = ucDialog;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Black;
this.Opacity = 0.50;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Size = parent.ClientSize;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
parent.Move += AdjustPosition;
parent.SizeChanged += AdjustPosition;
}
private void AdjustPosition(object sender, EventArgs e)
{
Form parent = sender as Form;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
this.ClientSize = parent.ClientSize;
}
public static DialogResult ShowDialog(Form parent, Form dialog)
{
mask = new MaskedDialog(parent, dialog);
dialog.StartPosition = FormStartPosition.CenterParent;
mask.Show();
DialogResult result = dialog.ShowDialog(mask);
mask.Close();
return result;
}
public static DialogResult ShowDialog(Form parent, UserControl dialog)
{
mask = new MaskedDialog(parent, dialog);
frmContainer = new Form();
frmContainer.ShowInTaskbar = false;
frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frmContainer.StartPosition = FormStartPosition.CenterParent;
frmContainer.Height = dialog.Height;
frmContainer.Width = dialog.Width;
frmContainer.Controls.Add(dialog);
mask.Show();
DialogResult result = frmContainer.ShowDialog(mask);
frmContainer.Close();
mask.Close();
return result;
}
public static void CloseDialog()
{
if (frmContainer != null)
{
frmContainer.Close();
}
}
}
}
Form d = new Form();
d.Width = 400;
d.Height = 300;
MaskedDialog.ShowDialog(this, d);
UserControl1 uc = new UserControl1();
uc.CloseClicked += new UserControl1.CloseComplete(OnCloseClicked);
MaskedDialog.ShowDialog(this, uc);
void OnCloseClicked()
{
MaskedDialog.CloseDialog();
}
答案 0 :(得分:5)
void Main()
{
var f = new Form
{
Width = 800,
Height = 600
};
var d = new Form
{
Width = 400,
Height = 300
};
var tb = new TextBox
{
Width = 250,
Height = 250,
Multiline = true,
Text = "Hello World",
Dock = DockStyle.Top
};
var b = new Button
{
Text = "Display Masked Dialog",
Dock = DockStyle.Top
};
b.Click += (s, e) =>
{
MaskedDialog.ShowDialog(f, d);
};
f.Controls.AddRange(new Control[] { tb, b } );
Application.Run(f);
}
public class MaskedDialog : Form
{
private Form dialog;
private MaskedDialog(Form parent, Form dialog)
{
this.dialog = dialog;
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = System.Drawing.Color.Black;
this.Opacity = 0.50;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Size = parent.ClientSize;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
parent.Move += AdjustPosition;
parent.SizeChanged += AdjustPosition;
}
private void AdjustPosition(object sender, EventArgs e)
{
Form parent = sender as Form;
this.Location = parent.PointToScreen(System.Drawing.Point.Empty);
this.ClientSize = parent.ClientSize;
}
public static DialogResult ShowDialog(Form parent, Form dialog)
{
var mask = new MaskedDialog(parent, dialog);
dialog.StartPosition = FormStartPosition.CenterParent;
mask.Show();
var result = dialog.ShowDialog(mask);
mask.Close();
return result;
}
}