【问题标题】:Show a form in two different opacity以两种不同的不透明度显示表格
【发布时间】:2011-08-17 00:17:19
【问题描述】:

我的情况是这样的。

我有一个作为 frmPopup 的表单,它有一个作为 pnlCtrlHolder 的面板。我将使用此表单弹出窗口并在此面板中显示第三个表单作为控件。

在表格 X 上。

dim frm as frmPopup
''Set the properties for this frmPopup
frm.Opacity=60

Dim frmContent as frmContent
''Set the properties for this frmPopup
frm.Opacity=100

frm.SetForm(frmContent)
frm.ShowDialog(me.toplevelControl)

在 frmPopup 中:

Public Sub SetForm(frm as Windows.Forms.Form)
pnlCtrlHolder.Controls.Clear()
pnlCtrlHolder.Controls.add(frm)
End Sub

现在我的问题, 这使得整个表单具有不透明度 = 60 的 frmContent,但我只需要在 frmPopup 上而不是在 frmContent 上。

我正在开发 vb.net Winforms 应用程序。我知道我要在不透明度为 60 的表单上添加一个表单作为控件。但是有什么方法可以达到预期的结果。 .我错过了什么吗?

【问题讨论】:

  • 此代码不能像发布的那样工作,必须将表单的 TopLevel 属性设置为 false,然后才能将其视为子窗口。在这一点上,它也不再可能修补不透明度,这仅适用于顶级窗口。
  • 这不是最终代码,只是一个概念。这可能有几个语法错误。
  • 你为什么使用“表单作为控件”你应该只创建一个用户控件
  • 我想创建类似“灯箱”的东西。我必须使用它,因为这是一个实际的表单,其中包含一组不同的逻辑,具体取决于用户的响应。如果您有更好的想法,请建议我。

标签: vb.net winforms opacity


【解决方案1】:

怎么样:

dim frm as New frmPopup   
frm.Opacity=60
Dim frmContent as New frmContent
frmContent.Opacity=100

【讨论】:

  • 嗨约翰,我的意思和你写的一样。感谢您的回复,但这不起作用。
  • 对不起我的意思是写:>> dim frm as New frmPopup frm.Opacity = 0.6 Dim frmContent as New frmContent frmContent.Opacity = 1
  • DIM frm as frmPopup 是一个声明。下一条语句不能工作,因为 frm 是 NOTHING。发布更多代码,因为这部分无法编译。错误:在赋值之前使用了 frm。运行时可能会导致 Null 引用异常。
【解决方案2】:

对不起,我的意思是 OPACITY 是一个介于 0 和 1 之间的值。

我知道这行得通,因为我刚刚尝试过。 :-)

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.Opacity = 1
        Dim newForm As New Form
        newForm.Text = "NewForm"
        newForm.Opacity = 0.6
        newForm.Show()

    End Sub

End Class

试试这样的:>>

dim frm as New frmPopup   
frm.Opacity=0.6
Dim frmContent as New frmContent
frmContent.Opacity=1

【讨论】:

  • 在 frm 上添加一个面板并在该面板中设置 frmContent。结果将是一个弹出窗体,其面板中添加了 frmContent,frm 的不透明度为 0.6。试试这个,然后请找我。
  • 如何在面板中设置 frmContent?您正在使用 SetParent API 调用吗?
【解决方案3】:

如果您尝试设置放置在面板中的表单的不透明度,那么它看起来不会显示,抱歉。

2 个表单一个面板 添加到新项目中,然后试试这个:>>

Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1

    Public Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    Friend WithEvents someForm As New Form2

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        someForm.Show()
        someForm.BackColor = Color.White
        someForm.Opacity = 1
        'System.Threading.Thread.Sleep(2000)
        SetParent(someForm.Handle, Panel1.Handle)

    End Sub

End Class

我正在开发 vb.net Winforms 应用程序。我明白我是 在不透明度为 60 的表单上添加一个表单作为控件。但是有没有 任何达到预期结果的方法。 .我错过了什么吗?

我什至自己也试过这样,没有你想要的效果。

Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1

    Friend WithEvents frmPopUp As New Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        frmPopUp.Opacity = 0.6
        frmPopUp.TopLevel = False
        frmPopUp.Text = "frmPopUp"
        Me.Controls.Add(frmPopUp)
        frmPopUp.Show()

    End Sub

End Class

【讨论】:

  • 三个不同的答案?您可能应该只使用更新的信息编辑您的原始答案。
  • LarsTech,对不起,我还不太习惯这些论坛。
猜你喜欢
  • 1970-01-01
  • 2011-08-31
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 2016-12-02
  • 2014-01-05
  • 2012-03-25
  • 1970-01-01
相关资源
最近更新 更多