VB2008 Button外觀 以及一些問題

Home Home
引用 | 編輯 a894985459
2013-02-14 20:20
樓主
推文 x0
如題 有沒有一些檔案或者是教學
可以改變Button的外觀 ..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 ebolaman
2013-08-07 22:37
1樓
  
1.
.Net 的 Button 預設就是長那樣,也改不了到哪去
想要做不規則形的按鈕,或是圖片的按鈕,可以自己製作一個 Class 或 Usercontrol

參考 MSDN http://msdn.microsoft.com/en-us/library/ms172532(v=vs.90).aspx

這是我做的範例 UserControl: 一個簡單的圓型按鈕 (用 G D I + 畫的,沒有 PictureBox)

完整的專案 在附件

建立一個 .Net 3.5 專案,再新增一個 UserControl 取名 CustomButton.vb
代碼如下

先編譯過後,工具列才會出現 CustomButton 可加入到 Form1 表單



複製程式
Imports System.Windows
Imports System.Drawing.Drawing2D


Public Class CustomButton


    Private pow2 As Func(Of Double, Double) =
        Function(a As Double) As Double
            Return a * a
        End Function


    Private m_mouseOn As Boolean = False
    Private m_mouseDown As Boolean = False




    Private Property _MouseOn As Boolean
        Get
            Return m_mouseOn
        End Get
        Set(ByVal value As Boolean)
            If value <> m_mouseOn Then
                m_mouseOn = value
                ' redraw
                Me.Invalidate()
                Me.Update()
                ' cursor
                If m_mouseOn Then
                    Me.Cursor = Cursors.Hand
                Else
                    Me.Cursor = Cursors.Default
                End If
            Else
                m_mouseOn = value
            End If
        End Set
    End Property


    Private Property _MouseDown As Boolean
        Get
            Return m_mouseDown
        End Get
        Set(ByVal value As Boolean)
            If value <> m_mouseDown Then
                m_mouseDown = value
                ' redraw
                Me.Invalidate()
                Me.Update()
            Else
                m_mouseDown = value
            End If
        End Set
    End Property




    Private Sub Form_Load() Handles MyBase.Load
        'Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or
        '            ControlStyles.UserPaint Or
        '            ControlStyles.OptimizedDoubleBuffer Or
        '            ControlStyles.ResizeRedraw, True)
    End Sub


    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        Me.Size = New Size(101, 101)


        MyBase.OnResize(e)
    End Sub


    Protected Overrides Sub OnPaint(ByVal e As Forms.PaintEventArgs)
        Dim rect As New Rectangle(1, 1, 98, 98)
        Dim img As New Bitmap(Me.DisplayRectangle.Width,
                              Me.DisplayRectangle.Height)
        Dim gImg As Graphics = Graphics.FromImage(img)


        ' set smoothing mode
        gImg.SmoothingMode = SmoothingMode.HighQuality


        ' fill background
        Using b As New SolidBrush(Me.BackColor)
            gImg.FillRectangle(b, rect)
        End Using


        ' is mouse down?
        If m_mouseDown Then
            If m_mouseOn Then
                Using b As New SolidBrush(Color.Green)
                    gImg.FillEllipse(b, rect)
                End Using
            End If
        Else
            ' is mouse on?
            If m_mouseOn Then
                Using b As New SolidBrush(Color.LawnGreen)
                    gImg.FillEllipse(b, rect)
                End Using
            End If
        End If


        ' draw button border
        Using p As New Pen(Brushes.Black, 2)
            gImg.DrawEllipse(p, rect)
        End Using


        ' draw text
        Using f As New Font(SystemFonts.DefaultFont.FontFamily,
                            16.0F, FontStyle.Bold)
            Dim s As String = "ABC"
            Dim fSize As Size = gImg.MeasureString(s, f).ToSize


            gImg.DrawString(s, f,
                            Brushes.Black,
                            49 - fSize.Width \ 2,
                            49 - fSize.Height \ 2)
        End Using


        ' draw image
        e.Graphics.DrawImage(img, 0, 0)


        ' dispose
        img.Dispose()
        gImg.Dispose()


        MyBase.OnPaint(e)
    End Sub


    Protected Overrides Sub OnMouseMove(ByVal e As Forms.MouseEventArgs)
        Dim vecX As Double = CDbl(e.X) - 50.0
        Dim vecY As Double = CDbl(e.Y) - 50.0


        If Math.Sqrt(pow2(vecX) + pow2(vecY)) <= 50.0 Then
            _MouseOn = True
        Else
            _MouseOn = False
        End If


        MyBase.OnMouseMove(e)
    End Sub


    Protected Overrides Sub OnMouseDown(ByVal e As Forms.MouseEventArgs)
        If m_mouseOn Then
            _MouseDown = True
        End If


        MyBase.OnMouseDown(e)
    End Sub


    Protected Overrides Sub OnMouseUp(ByVal e As Forms.MouseEventArgs)
        _MouseDown = False


        MyBase.OnMouseUp(e)
    End Sub


    Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)
        _MouseOn = False


        MyBase.OnMouseLeave(e)
    End Sub


End Class






2.
就我所知有 3 種方法讀取 DLL:
a. 專案屬性 -> References ,使用時用 Imports,最簡單的方法 (只限 Managed dlls)
b. DllImport, 有很多 PInvoke functions 可用
c. Assembly.LoadFrom("<file name>") 讀取一個 dll file

.net 甚至還可以從網路上下載 dll 來載入表單,達到自動更新的效果

3.
ini 檔網路上有些 讀取 ini 檔的 class
或是用 API GetPrivateProfileString, WritePrivateProfileString
或是自己寫一個 class,其實不難,簡單的 ini 格式都可以手動讀取

.net 大部分格式偏向 xml (.net 有內建 XML parser),我個人比較喜愛的是 json
不管哪種格式,網路上都有一大堆 class 來幫助你輕鬆 讀取/寫入

本帖包含附件
檔名: zip WindowsApplication1.rar   (2022-06-09 14:21 / 73 KB)   下載次數:0


獻花 x0