none
[VB.NET][GDI+]关于在WinForm和PictureBox上绘制带有alpha通道的PNG图片 RRS feed

  • 问题

  •  

    想请教一下高手,我想在窗体上绘制一个透明的png图片,整个窗体就只显示这个图片,其它的都是透明的(类似于Adobe 的欢迎画面)。请问应该如何实现?或者绘制一个填充的圆,只显示这个透明的圆。

     

    另外,像Windows Vista中的一些图标都是256x256的,但是用Visual Studio打开,会发现里面的图标居然是png格式的,请问如何能把那个png格式的图像从ico中弄出来?

    2008年6月16日 13:19

答案

  • 不好意思,我弄出来了,只不过代码写得很乱。下面的代码只是按照我的方法去实现,有些方法的源代码没有贴上来。

    现在还有两个问题没有解决,就是重绘的问题(画面闪烁和重复绘制),另外一个就是我将图都绘制到了窗体上,如何让绘制到窗体上的图像响应用户的动作,比如单击等。是不是需要绘到PictureBox上?如果需要绘到PictureBox上的话,绘制的方法是否与窗体一样?

     

    Public Class frmMain
        Public DrawTrue As Boolean = False

        Public Declare Function DwmExtendFrameIntoClientArea Lib "dwmapi.dll" (ByVal hwnd As Long, ByVal margin As MARGINS) As Long
        Public Declare Function DwmIsCompositionEnabled Lib "dwmapi.dll" (ByRef enabledptr As Long) As Long
        Public Structure MARGINS
            Public m_Left As Long
            Public m_Right As Long
            Public m_Top As Long
            Public m_Button As Long
        End Structure
        Private Sub frmMain_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
            Me.Close()
        End Sub

        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim mg As MARGINS, en As Long
            mg.m_Left = -1
            mg.m_Button = -1
            mg.m_Right = -1
            mg.m_Top = -1
            DwmIsCompositionEnabled(en)
            If en Then
                DwmExtendFrameIntoClientArea(Me.Handle, mg)
            End If
                  Me.Visible = False
        End Sub

           Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

              Dim bsh As SolidBrush = New SolidBrush(Color.FromArgb(255, Color.Black))
            e.Graphics.FillRectangle(bsh, 0, 0, Me.Width, Me.Height)
            bsh.Dispose()

            '==========================================================================================================================
            If DrawTrue = False Then
                e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                  DrawLinearGradientBrush(Me, 0, 0, 0, 0, 550, 550, 550, 550, Color.Black, Color.Transparent)
                DrawCircleOnForm(Me, 0, 0, 550, 550, 70, 255, 255, 255)
                DrawCircleOnForm(Me, 3, 3, 544, 544, 45, 255, 255, 255)
                DrawObject.DrawCircleOnForm(Me, 147, 147, 256, 256, 20, 0, 0, 0)
                DrawCircleOnForm(Me, 211, 10, 128, 128, 128, 0, 0, 0)
                DrawImageOnForm(Me, "SYMCUW_128.png", 211, 10, 128, 128)

                DrawObject.DrawImageOnForm(Me, "My Media Center.png", 147, 147, 256, 256)
            Else
                Exit Sub
            End If
            Me.Visible = True

     

            '==========================================================================================================================
        End Sub

    End Class

    2008年6月17日 15:11

全部回复

  • 这个我觉得在程序里面不太好实现,你可以考虑用专业的制图工具来实现。

     

    2008年6月17日 6:12
  • 不好意思,我弄出来了,只不过代码写得很乱。下面的代码只是按照我的方法去实现,有些方法的源代码没有贴上来。

    现在还有两个问题没有解决,就是重绘的问题(画面闪烁和重复绘制),另外一个就是我将图都绘制到了窗体上,如何让绘制到窗体上的图像响应用户的动作,比如单击等。是不是需要绘到PictureBox上?如果需要绘到PictureBox上的话,绘制的方法是否与窗体一样?

     

    Public Class frmMain
        Public DrawTrue As Boolean = False

        Public Declare Function DwmExtendFrameIntoClientArea Lib "dwmapi.dll" (ByVal hwnd As Long, ByVal margin As MARGINS) As Long
        Public Declare Function DwmIsCompositionEnabled Lib "dwmapi.dll" (ByRef enabledptr As Long) As Long
        Public Structure MARGINS
            Public m_Left As Long
            Public m_Right As Long
            Public m_Top As Long
            Public m_Button As Long
        End Structure
        Private Sub frmMain_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
            Me.Close()
        End Sub

        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim mg As MARGINS, en As Long
            mg.m_Left = -1
            mg.m_Button = -1
            mg.m_Right = -1
            mg.m_Top = -1
            DwmIsCompositionEnabled(en)
            If en Then
                DwmExtendFrameIntoClientArea(Me.Handle, mg)
            End If
                  Me.Visible = False
        End Sub

           Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

              Dim bsh As SolidBrush = New SolidBrush(Color.FromArgb(255, Color.Black))
            e.Graphics.FillRectangle(bsh, 0, 0, Me.Width, Me.Height)
            bsh.Dispose()

            '==========================================================================================================================
            If DrawTrue = False Then
                e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                  DrawLinearGradientBrush(Me, 0, 0, 0, 0, 550, 550, 550, 550, Color.Black, Color.Transparent)
                DrawCircleOnForm(Me, 0, 0, 550, 550, 70, 255, 255, 255)
                DrawCircleOnForm(Me, 3, 3, 544, 544, 45, 255, 255, 255)
                DrawObject.DrawCircleOnForm(Me, 147, 147, 256, 256, 20, 0, 0, 0)
                DrawCircleOnForm(Me, 211, 10, 128, 128, 128, 0, 0, 0)
                DrawImageOnForm(Me, "SYMCUW_128.png", 211, 10, 128, 128)

                DrawObject.DrawImageOnForm(Me, "My Media Center.png", 147, 147, 256, 256)
            Else
                Exit Sub
            End If
            Me.Visible = True

     

            '==========================================================================================================================
        End Sub

    End Class

    2008年6月17日 15:11