带圆角的文本框 [英] Textbox with rounded corners
问题描述
如何在winform c#中创建文本框的曲线边缘
示例文本框:http://www.codeproject.com/KB/edit/RoundedCornerTextbox/RoundedTextbox.png
这段代码VB
Protected Overrides Function ProcessCmdKey(ByRef msg _作为 System.Windows.Forms.Message, _ByVal keyData As System.Windows.Forms.Keys) As Boolean如果 msg.WParam.ToInt32() = CInt(Keys.Enter) 那么SendKeys.Send("{Tab}")返回真ElseIf msg.WParam.ToInt32() = CInt(Keys.Decimal) 然后SendKeys.Send(",")返回真万一结束函数
<块引用>
下一个方法是实际覆盖 WM_Paint 事件,在该事件中完成重绘.它利用 API 函数 GetWindowDC 和 ReleaseDC 来获取控件的实际图形,包括非客户区.
Protected Overrides Sub WndProc(ByRef m As _System.Windows.Forms.Message) 处理 MyBase.WndProc(m)选择案例 m.MsgCase &HF 'WM_PAINTDim rect As New Rectangle(0, 0, MyBase.Width, MyBase.Height)Dim hDC As IntPtr = GetWindowDC(Me.Handle)Dim g As Graphics = Graphics.FromHdc(hDC)如果我启用那么g.Clear(颜色.白色)别的g.Clear(Color.FromName("control"))万一画框(g)绘制文本(g)ReleaseDC(Me.Handle, hDC)g.Dispose()案例 &H7, &H8, &H200, &H2A3'CMB_DROPDOWN, CMB_CLOSEUP, WM_SETFOCUS,'WM_KILLFOCUS, WM_MOUSEMOVE, 'WM_MOUSELEAVE更新状态()结束选择结束子
<块引用>
为了得到圆角,使用如下所示的方法.
Private Sub TekenRondeRechthoek(ByVal g As Graphics, _ByVal pen As Pen, ByVal rectangle As Rectangle, _ByVal 半径为单个)Dim size As Single =(半径 * 2.0!)Dim gp As GraphicsPath = New GraphicsPathgp.AddArc(rectangle.X, rectangle.Y, size, size, 180, 90)gp.AddArc((rectangle.X + (rectangle.Width - size)), _矩形.Y, 大小, 大小, 270, 90)gp.AddArc((rectangle.X + (rectangle.Width - size)), _(rectangle.Y + (rectangle.Height - size)), _大小,大小,0, 90)gp.AddArc(rectangle.X, (rectangle.Y + _(rectangle.Height - size)), size, size, 90, 90)gp.CloseFigure()g.DrawPath(pen, gp)gp.Dispose()结束子
如何在c#中创建
http://www.developerfusion.com/tools/convert/vb-to-csharp/ 是一个将 VB.net 代码转换为 C#(或其他方式)的优秀站点.应该使您更容易进行更改,并在 VB.net 中查看它是如何进行的
How to create curved edges of the textbox in winform c#
Sample TextBox: http://www.codeproject.com/KB/edit/RoundedCornerTextbox/RoundedTextbox.png
This code 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
The next method is the actual overriding of the WM_Paint event, in which the redrawing is done. It makes uses of the API functions GetWindowDC and ReleaseDC to get the actual graphics of the control, including the non-client area.
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
To get the rounded corners, the method shown below is used.
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
How to create in c#
http://www.developerfusion.com/tools/convert/vb-to-csharp/ is an excellent site to convert VB.net code to C# (or the other way around). Should make it easier for you to do the alterations and see how it does it in VB.net
这篇关于带圆角的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!