从表单的.resx文件VB.NET访问资源

时间:2014-05-28 02:52:53

标签: vb.net winforms embedded-resource

我正在尝试构建自定义GUI表单。我正在使用pictureBoxes来保持关闭/最小化/最大化按钮。我已将.png资源添加到表单的.resx文件中,并设置要嵌入的资源。

在表格背后的代码文件中,我有以下内容:

Private Property resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(woot))

以下是应该替换其中一个pictureBoxes

中的图像的代码
Private Sub closeBtn_MouseEnter(sender As Object, e As EventArgs) Handles closeBtn.MouseEnter
    Me.closeBtn.Image = CType(Me.resources.GetObject("closeBtn_Over.Image"), System.Drawing.Image)
End Sub

基本上,当鼠标在窗口上移动时,.image属性设置为空。点击事件仍然可以正常工作。

所以我的问题是,我如何访问我在表单资源文件中嵌入的图像?我可以将它们嵌入到项目的.resx文件中,但我的目标是创建一个可以作为独立模板导出的自包含表单。


修改

所以,新的症状。忘掉上面的一切。每当我更改设计器中主窗体上的属性时,我添加到窗体的.resx文件中的图像将被删除。因此,为什么图像被设置为空。由于这似乎与设计师所做的更改有关,因此我使用的是Visual Studio 2012 Ultimate。此外,因为它可能与表格的整个代码隐藏文件相关。

Public Class woot
#Region "FORM PROPERTY DECLARATIONS"
    Private Property resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(woot))
    Private Property _HelpButton As Boolean
    Public Overloads Property HelpButton As Boolean
        Get
            Return _HelpButton
        End Get
        Set(value As Boolean)
            Me._HelpButton = value
            setGUIButtonVisability(Me.helpBtn, value)
        End Set
    End Property
    Private Property _MinimizeBox As Boolean
    Public Overloads Property MinimizeBox As Boolean
        Get
            Return _MinimizeBox
        End Get
        Set(value As Boolean)
            Me._MinimizeBox = value
            setGUIButtonVisability(Me.minBtn, value)
        End Set
    End Property
    Private Property _MaximizeBox As Boolean
    Public Overloads Property MaximizeBox As Boolean
        Get
            Return _MaximizeBox
        End Get
        Set(value As Boolean)
            Me._MaximizeBox = value
            setGUIButtonVisability(Me.maxBtn, value)
        End Set
    End Property
    Private Property _Text As String
    Public Overloads Property Text As String
        Get
            Return _Text
        End Get
        Set(value As String)
            Me._Text = value
            Me.TitleLabel.Text = value
        End Set
    End Property
    Private Property _CloseButton As Boolean = True
    Public Property CloseButton As Boolean
        Get
            Return _CloseButton
        End Get
        Set(value As Boolean)
            Me._CloseButton = value
            setGUIButtonVisability(Me.closeBtn, value)
        End Set
    End Property
#End Region
#Region "GUI STUFF"
    Private Const WM_NCLBUTTONDOWN As Integer = &HA1S
    Private Const WM_NCHITTEST = &H84S
    Private Const WM_NCCALCSIZE = &H83S
    Private Const HTBORDER As Integer = 18
    Private Const HTBOTTOM As Integer = 15
    Private Const HTBOTTOMLEFT As Integer = 16
    Private Const HTBOTTOMRIGHT As Integer = 17
    Private Const HTCAPTION As Integer = 2
    Private Const HTLEFT As Integer = 10
    Private Const HTRIGHT As Integer = 11
    Private Const HTTOP As Integer = 12
    Private Const HTTOPLEFT As Integer = 13
    Private Const HTTOPRIGHT As Integer = 14

    Private Sub closeBtn_Click(sender As Object, e As EventArgs) Handles closeBtn.Click, iconBox.DoubleClick
        Me.Dispose()
    End Sub
    Private Sub closeBtn_MouseDown(sender As Object, e As MouseEventArgs) Handles closeBtn.MouseDown
        Me.closeBtn.Image = CType(Me.resources.GetObject("closeBtn_Down"), System.Drawing.Image)
    End Sub
    Private Sub closeBtn_MouseEnter(sender As Object, e As EventArgs) Handles closeBtn.MouseEnter
        Me.closeBtn.Image = CType(Me.resources.GetObject("closeBtn_Over"), System.Drawing.Image)
    End Sub
    Private Sub closeBtn_MouseLeave(sender As Object, e As EventArgs) Handles closeBtn.MouseLeave
        Me.closeBtn.Image = CType(Me.resources.GetObject("closeBtn_Normal"), System.Drawing.Image)
        Me.closeBtn.Refresh()
    End Sub
    Private Sub closeBtn_MouseUp(sender As Object, e As MouseEventArgs) Handles closeBtn.MouseUp
        Me.closeBtn.Image = CType(Me.resources.GetObject("closeBtn_Normal"), System.Drawing.Image)
    End Sub

    Private Sub maxBtn_Click(sender As Object, e As EventArgs) Handles maxBtn.Click
        If Me.WindowState = FormWindowState.Normal Then
            Dim thisScreen As Screen = Screen.FromPoint(Me.Location)
            Me.MaximumSize = thisScreen.WorkingArea.Size
            Me.WindowState = FormWindowState.Maximized
        Else
            Me.WindowState = FormWindowState.Normal
        End If
    End Sub
    Private Sub maxBtn_MouseDown(sender As Object, e As MouseEventArgs) Handles maxBtn.MouseDown
        If Me.WindowState = FormWindowState.Normal Then
            maxBtn.Image = CType(resources.GetObject("maxBtn_Down"), System.Drawing.Image)
        Else
            maxBtn.Image = CType(resources.GetObject("maxBtn_Down_2"), System.Drawing.Image)
        End If
    End Sub
    Private Sub maxBtn_MouseEnter(sender As Object, e As EventArgs) Handles maxBtn.MouseEnter
        If Me.WindowState = FormWindowState.Normal Then
            maxBtn.Image = CType(resources.GetObject("maxBtn_Over"), System.Drawing.Image)
        Else
            maxBtn.Image = CType(resources.GetObject("maxBtn_Over_2"), System.Drawing.Image)
        End If
    End Sub
    Private Sub maxBtn_MouseLeave(sender As Object, e As EventArgs) Handles maxBtn.MouseLeave
        If Me.WindowState = FormWindowState.Normal Then
            maxBtn.Image = CType(resources.GetObject("maxBtn_Normal"), System.Drawing.Image)
        Else
            maxBtn.Image = CType(resources.GetObject("maxBtn_Normal_2"), System.Drawing.Image)
        End If
    End Sub
    Private Sub maxBtn_MouseUp(sender As Object, e As MouseEventArgs) Handles maxBtn.MouseUp
        If Me.WindowState = FormWindowState.Normal Then
            maxBtn.Image = CType(resources.GetObject("maxBtn_Normal"), System.Drawing.Image)
        Else
            maxBtn.Image = CType(resources.GetObject("maxBtn_Normal_2"), System.Drawing.Image)
        End If
    End Sub

    Private Sub minBtn_Click(sender As Object, e As EventArgs) Handles minBtn.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub
    Private Sub minBtn_MouseDown(sender As Object, e As MouseEventArgs) Handles minBtn.MouseDown
        minBtn.Image = CType(resources.GetObject("minBtn_Down"), System.Drawing.Image)
    End Sub
    Private Sub minBtn_MouseEnter(sender As Object, e As EventArgs) Handles minBtn.MouseEnter
        minBtn.Image = CType(resources.GetObject("minBtn_Over"), System.Drawing.Image)
    End Sub
    Private Sub minBtn_MouseLeave(sender As Object, e As EventArgs) Handles minBtn.MouseLeave
        minBtn.Image = CType(resources.GetObject("minBtn_Normal"), System.Drawing.Image)
    End Sub
    Private Sub minBtn_MouseUp(sender As Object, e As MouseEventArgs) Handles minBtn.MouseUp
        minBtn.Image = CType(resources.GetObject("minBtn_Normal"), System.Drawing.Image)
    End Sub

    Private Sub helpBtn_MouseDown(sender As Object, e As MouseEventArgs) Handles helpBtn.MouseDown
        helpBtn.Image = CType(resources.GetObject("helpBtn_Down"), System.Drawing.Image)
    End Sub
    Private Sub helpBtn_MouseEnter(sender As Object, e As EventArgs) Handles helpBtn.MouseEnter
        helpBtn.Image = CType(resources.GetObject("helpBtn_Over"), System.Drawing.Image)
    End Sub
    Private Sub helpBtn_MouseLeave(sender As Object, e As EventArgs) Handles helpBtn.MouseLeave
        helpBtn.Image = CType(resources.GetObject("helpBtn_Normal"), System.Drawing.Image)
    End Sub
    Private Sub helpBtn_MouseUp(sender As Object, e As MouseEventArgs) Handles helpBtn.MouseUp
        helpBtn.Image = CType(resources.GetObject("helpBtn_Normal"), System.Drawing.Image)
    End Sub

    Private Sub TitleBar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TitleLabel.MouseDown
        If e.Clicks = 2 Then
            If Me.WindowState = FormWindowState.Normal Then
                Dim thisScreen As Screen = Screen.FromPoint(Me.Location)
                Me.MaximumSize = thisScreen.WorkingArea.Size
                Me.WindowState = FormWindowState.Maximized
            Else
                Me.WindowState = FormWindowState.Normal
            End If
        ElseIf e.Button = MouseButtons.Left And e.Clicks = 1 Then
            sender.Capture = False
            ' Create and send a WM_NCLBUTTONDOWN message.
            Dim msg As Message = _
                Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _
                    New IntPtr(HTCAPTION), IntPtr.Zero)
            Me.DefWndProc(msg)
        End If

    End Sub
    Private Sub BPanel_MouseDown(sender As Object, e As MouseEventArgs) Handles BPanel.MouseDown
        If e.Button = MouseButtons.Left Then
            Me.BPanel.Capture = False

            ' Create and send a WM_NCLBUTTONDOWN message.
            Dim msg As Message = _
                Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _
                    New IntPtr(HTBOTTOM), IntPtr.Zero)
            Me.DefWndProc(msg)
        End If
    End Sub
    Private Sub BRPanel_MouseDown(sender As Object, e As MouseEventArgs) Handles BRPanel.MouseDown
        If e.Button = MouseButtons.Left Then
            Me.BRPanel.Capture = False

            ' Create and send a WM_NCLBUTTONDOWN message.
            Dim msg As Message = _
                Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _
                    New IntPtr(HTBOTTOMRIGHT), IntPtr.Zero)
            Me.DefWndProc(msg)
        End If
    End Sub
    Private Sub BLPanel_MouseDown(sender As Object, e As MouseEventArgs) Handles BLPanel.MouseDown
        If e.Button = MouseButtons.Left Then
            Me.BLPanel.Capture = False

            ' Create and send a WM_NCLBUTTONDOWN message.
            Dim msg As Message = _
                Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _
                    New IntPtr(HTBOTTOMLEFT), IntPtr.Zero)
            Me.DefWndProc(msg)
        End If
    End Sub
    Private Sub RPanel_MouseDown(sender As Object, e As MouseEventArgs) Handles RPanel.MouseDown
        If e.Button = MouseButtons.Left Then
            Me.RPanel.Capture = False

            ' Create and send a WM_NCLBUTTONDOWN message.
            Dim msg As Message = _
                Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _
                    New IntPtr(HTRIGHT), IntPtr.Zero)
            Me.DefWndProc(msg)
        End If
    End Sub
    Private Sub LPanel_MouseDown(sender As Object, e As MouseEventArgs) Handles LPanel.MouseDown
        If e.Button = MouseButtons.Left Then
            Me.LPanel.Capture = False

            ' Create and send a WM_NCLBUTTONDOWN message.
            Dim msg As Message = _
                Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _
                    New IntPtr(HTLEFT), IntPtr.Zero)
            Me.DefWndProc(msg)
        End If
    End Sub

    Private Sub setGUIButtonVisability(ByVal button As PictureBox, ByVal value As Boolean)
        button.Visible = value
    End Sub
#End Region
#Region "GUI SUPPORT FUNCTIONS"
    Private Sub helpBtn_Click(sender As Object, e As EventArgs) Handles helpBtn.Click
        Try
            Throw New NotImplementedException
        Catch ex As Exception
            MsgBox(String.Format("The control {0} has not been implmented yet." & vbCrLf & vbCrLf & "{1}", sender.Name, ex.Message))
        End Try
    End Sub
#End Region


    Private Sub CustomGUIForm1_Load(sender As Object, e As EventArgs) Handles Me.Load

    End Sub

End Class

1 个答案:

答案 0 :(得分:1)

假设您处于项目级别,请使用My代替Me。您还需要添加MouseLeave事件。像这样:

Private Sub closeBtn_MouseEnter(sender As Object, e As EventArgs) Handles closeBtn.MouseEnter
    closeBtn.Image = My.Resources.MyCloseImageName
End Sub

MouseLeave事件:

Private Sub closeBtn_MouseLeave(sender As Object, e As EventArgs) Handles closeBtn.MouseLeave
    closeBtn.Image = My.Resources.MyNormalImageName
End Sub

当您输入“My.Resources。”时,第二个点将启用智能感知,您将看到资源文件中的任何图像。除非您尝试做我没有看到的事情,否则不需要ComponentResourceManager引用。