带圆角的文本框

时间:2012-05-14 08:02:03

标签: c# winforms system.drawing

如何在winform c#

中创建文本框的弯曲边缘

示例TextBox: http://www.codeproject.com/KB/edit/RoundedCornerTextbox/RoundedTextbox.png

此代码VB

 Protected Overrides Function ProcessCmdKey(ByRef msg _
                    As System.Windows.Forms.Message, _
                    ByVal keyData As System.Windows.Forms.Keys) As Boolean 
    If msg.WParam.ToInt32() = CInt(Keys.Enter) Then 
        SendKeys.Send("{Tab}")
        Return True 
    ElseIf msg.WParam.ToInt32() = CInt(Keys.Decimal) Then 
        SendKeys.Send(",")
        Return True 
    End If 
End Function
  

下一个方法是WM_Paint事件的实际覆盖,其中重绘完成。它使用API​​函数GetWindowDC和ReleaseDC来获取控件的实际图形,包括非客户区域。

Protected Overrides Sub WndProc(ByRef m As _
          System.Windows.Forms.Message) Handles MyBase.WndProc(m)
    Select Case m.Msg
        Case &HF 'WM_PAINT 
            Dim rect As New Rectangle(0, 0, MyBase.Width, MyBase.Height)
            Dim hDC As IntPtr = GetWindowDC(Me.Handle)
            Dim g As Graphics = Graphics.FromHdc(hDC)
            If Me.Enabled Then 
                g.Clear(Color.White)
            Else 
                g.Clear(Color.FromName("control"))
            End If 
            DrawBorder(g)
            DrawText(g)
            ReleaseDC(Me.Handle, hDC)
            g.Dispose()
        Case &H7, &H8, &H200, &H2A3
        'CMB_DROPDOWN, CMB_CLOSEUP, WM_SETFOCUS, 
        'WM_KILLFOCUS, WM_MOUSEMOVE, 'WM_MOUSELEAVE 
            UpdateState()
    End Select 
End Sub
  

要获得圆角,请使用下面显示的方法。

Private Sub TekenRondeRechthoek(ByVal g As Graphics, _
            ByVal pen As Pen, ByVal rectangle As Rectangle, _
            ByVal radius As Single)
    Dim size As Single = (radius * 2.0!)
    Dim gp As GraphicsPath = New GraphicsPath
    gp.AddArc(rectangle.X, rectangle.Y, size, size, 180, 90)
    gp.AddArc((rectangle.X + (rectangle.Width - size)), _
               rectangle.Y, size, size, 270, 90)
    gp.AddArc((rectangle.X + (rectangle.Width - size)), _
              (rectangle.Y + (rectangle.Height - size)), _
              size, size, 0, 90)
    gp.AddArc(rectangle.X, (rectangle.Y + _
             (rectangle.Height - size)), size, size, 90, 90)
    gp.CloseFigure()
    g.DrawPath(pen, gp)
    gp.Dispose()
End Sub

如何在c#中创建

1 个答案:

答案 0 :(得分:1)

http://www.developerfusion.com/tools/convert/vb-to-csharp/是将VB.net代码转换为C#(或其他方式)的绝佳网站。应该让您更容易进行更改,并在VB.net中查看它是如何做到的