具有不透明度的 WinForm 控件
我有一个本身有一些控件的表单(btnCreateReport,pnlDarkLayer)。我有一个适合表单的面板(Dock = Fill),它位于所有控件的背面。当用户单击 btnCreateReport 按钮时,我调用 pnlDarkLayer BringToFront 方法,经过一些计算后,我调用按钮的 SendToBack() 方法。我想在表单控件上绘制一个深色图层并禁用表单上的所有控件。 是否可以?谢谢。
也许这段代码可以帮助您理解我的目的:
private void btnCreateReport_Click(object sender, EventArgs e)
{
pnlDarkLayer.BackColor = Color.FromArgb(100, Color.Gray);
pnlDarkLayer.BringToFront();
btnCreateReport.Enabled = false;
Thread ProcessReport = new Thread(new ThreadStart(ProcessingReport));
ProcessReport.Start();
while (ProcessReport.IsAlive)
{
Application.DoEvents();
}
pnlDarkLayer.SendToBack();
btnCreateReport.Enabled = true;
}
这段代码隐藏了所有控件,但我不想隐藏表单上的控件。我想在它们上绘制一个深色层。并且用户必须可以看到控件。 我需要类似表单控件的不透明属性之类的东西。
我已经测试了这个:
pnlDarkLayer.CreateGraphics().CompositingMode=System.Drawing.Drawing2D.CompositingMode.SourceOver;
更新:我已经测试了这个:(使用表单代替面板)
private void btnCreateReport_Click(object sender, EventArgs e)
{
btnCreateReport.Enabled = false;
frmProgress ProgressForm = new frmProgress();
ProgressForm.TopLevel = false;
ProgressForm.Parent = this;
ProgressForm.BringToFront();
this.Controls.Add(ProgressForm);
ProgressForm.Show();
Thread ProcessReport = new Thread(new ThreadStart(ProcessingReport));
ProcessReport.Start();
while (ProcessReport.IsAlive)
{
Application.DoEvents();
}
ProgressForm.Close();
btnCreateReport.Enabled = true;
}
但我在表单中看不到 ProgressForm。
I have a form that has some controls on itself(btnCreateReport,pnlDarkLayer).I have a panel that fit to form(Dock = Fill) and it is on the back of all controls.when user click on the btnCreateReport button ,I call pnlDarkLayer BringToFront method and after some calculation I call SendToBack() method of the button.I want to draw a dark layer on form controls and disable all of controls on the form.
Is it possible? Thanks.
Maybe this code help u to understand my purpose:
private void btnCreateReport_Click(object sender, EventArgs e)
{
pnlDarkLayer.BackColor = Color.FromArgb(100, Color.Gray);
pnlDarkLayer.BringToFront();
btnCreateReport.Enabled = false;
Thread ProcessReport = new Thread(new ThreadStart(ProcessingReport));
ProcessReport.Start();
while (ProcessReport.IsAlive)
{
Application.DoEvents();
}
pnlDarkLayer.SendToBack();
btnCreateReport.Enabled = true;
}
This code hide all of controls but i don't want to hide controls on the form.I want to draw a dark layer on them.And User must can see controls.
I need something like opacity property of forms for their controls.
I have test this:
pnlDarkLayer.CreateGraphics().CompositingMode=System.Drawing.Drawing2D.CompositingMode.SourceOver;
Update: I have test this one: (use a form instead of panel)
private void btnCreateReport_Click(object sender, EventArgs e)
{
btnCreateReport.Enabled = false;
frmProgress ProgressForm = new frmProgress();
ProgressForm.TopLevel = false;
ProgressForm.Parent = this;
ProgressForm.BringToFront();
this.Controls.Add(ProgressForm);
ProgressForm.Show();
Thread ProcessReport = new Thread(new ThreadStart(ProcessingReport));
ProcessReport.Start();
while (ProcessReport.IsAlive)
{
Application.DoEvents();
}
ProgressForm.Close();
btnCreateReport.Enabled = true;
}
But I can't see the ProgressForm in my form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自
http:// support.microsoft.com/kb/943454
该页面还提供了一个代码示例(遗憾的是,在 vb 中)来展示这是如何完成的。
From
http://support.microsoft.com/kb/943454
The page also provides a code example (in vb, sadly) to show how this is done.
如果我理解正确的话,您想在操作运行时将表单的内容“变暗”。
正如有人之前说过的,要做到正确是非常棘手的。但有一种方法可以轻松完成,只需进行一次预订(见下文)。
看一下这个源代码:
Form1类有两个主要方法,Shroud()和Unshroud()。
Shroud() 方法获取表单的快照,并将其复制到位图中,然后将其“变暗”。然后隐藏控件,并将位图绘制在窗体上。
UnShroud() 方法恢复控件,并告诉窗体不再绘制位图。
它需要两个私有变量:一个用于存储位图,另一个用于维护当前状态的标志。
它还重写了 OnPaint(),因为它需要在“隐藏”时绘制背景图像。
注意:屏蔽是通过截取表单的屏幕截图来实现的。这意味着该表单必须是覆盖点的最顶层表单。如果该表单被其他表单遮挡,那么它们将包含在屏幕截图中。我希望这对您来说不会成为问题。
注意:如前所述,在 Windows 中实现透明性的唯一方法是所有相关控件的充分合作,而这是一项艰巨的任务。其他任何东西(包括这个解决方案)实际上都只是骗局。
If I understand correctly, you want to 'darken' the contents of the form while an operation is running.
As someone's said before here, it's very tricky to do right. But there is a way to get it done easily, with one reservation (see below).
Look at this source code:
The Form1 class has two main methods, Shroud() and Unshroud().
The Shroud() method takes a snapshot of the form, and copies it into a bitmap, which is then 'darkened'. The controls are then hidden, and the bitmap is painted on the form.
The UnShroud() method restores the controls, and tells the form to no longer draw the bitmap.
It requires two private variables: one to store the bitmap, and a flag that maintains the current state.
It also overrides OnPaint() because it needs to draw the background image when it is 'shrouded'.
Note: The shrouding works by taking a screenshot of the form. This means that the form MUST BE the top-most form at the point of shrouding. If the form is obscured by other forms then they will be included in the screenshot. I hope that this won't be a problem for you.
Note: As said before, the only way to achieve transparency in Windows is full cooperation from all controls involved, and that's an arduous task. Anything else (including this solution) is really just trickery.