如题
解决方案 »
- 状态栏上的图标不能显示ToolTip
- 请高手帮忙看看 我这个程序 为什么结果总是负数。。
- GridView1:System.Web.UI.WebControls.DataControlFieldCollection 必须具有类型
- XML获取节点问题?
- 在一个user control显示emf格式的文件的问题
- 关于文件下载的问题
- vs2008自带报表的line不显示
- 如何连数据库?
- 急救!是否能动态设置responseEncoding属性..
- 安装visual stuidio 7.0都需要更新那些组件,怎么配置,100分想送~
- 输入一串字符,如"this is test",将这串字符反向输出,也就是"test is this",请问大哥大姐怎么去实现它,请贴出代码,谢谢.
- 关于C# 性能的问题
.main1field
{
font-family: "Arial" , "Helvetica" , "sans-serif";
font-size: 14px;
color: #0660B0;
background-image: url(../images/main_1_fieldbg.gif);
background-repeat: no-repeat;
border: none;
height: 21px;
width: 217px;
padding-top: 3px;
padding-right: 8px;
padding-bottom: 3px;
padding-left: 8px;
background-attachment: fixed;
}
namespace UnderLineBox
{
public class UnderLineBox : TextBox
{
private bool m_underLine; public bool UnderLine
{
get { return m_underLine; }
set
{
if (this.m_underLine != value)
{
if (value)
{
this.BorderStyle = BorderStyle.None;
}
m_underLine = value;
}
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf || m.Msg == 0x14 || m.Msg == 0x85)
{
if (this.BorderStyle == BorderStyle.None)
{
if (m_underLine)
{
using (Graphics g = Graphics.FromHwnd(this.Handle))
{
g.DrawLine(SystemPens.ControlText, 0, this.Height - 1, this.Width - 1, this.Height - 1);//这里写你要绘制的形状。
}
}
}
}
}
}}
自定义一个继承TextBox的自定义控件,重绘TextBox
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;namespace testApplication1
{
public partial class RounderTextBox : TextBox
{
#region Declare private States state = States.Normal;
private Pen borderPen;
private Brush textBrush;
private Rectangle mainRect;
private PointF txtLoc; private enum States
{
Normal,
Focused,
Disabled
} #endregion #region DLL Import
[DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);
#endregion public RounderTextBox()
{
} protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case 15:
Rectangle rect = new Rectangle(0, 0, base.Width, base.Height);
IntPtr hDC = GetWindowDC(this.Handle);
Graphics g = Graphics.FromHdc(hDC);
if (this.Enabled)
{
g.Clear(Color.White);
}
else
{
g.Clear(Color.FromName("control"));
} DrawBorder(g);
DrawText(g);
ReleaseDC(this.Handle, hDC);
g.Dispose();
break;
case 7:
case 8:
UpdateState();
break;
} base.WndProc(ref m);
} #region Draw Method
private void DrawBorder(Graphics g)
{
mainRect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
switch (state)
{
case States.Focused:
borderPen = new Pen(Color.Blue);
break;
case States.Disabled:
borderPen = new Pen(Color.DarkGray);
break;
case States.Normal:
borderPen = new Pen(Color.DimGray);
break;
} TekenRondeRechthoek(g, borderPen, mainRect, 3);
} private void TekenRondeRechthoek(Graphics g, Pen pen, Rectangle rectangle,
float radius)
{
float size = radius * 2;
GraphicsPath gp = 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();
} private void DrawText(Graphics g)
{
string text = "";
switch (state)
{
case States.Normal:
case States.Focused:
textBrush = new SolidBrush(this.ForeColor);
break;
case States.Disabled:
textBrush = new SolidBrush(Color.DarkGray);
break;
}
if (g.MeasureString(this.Text, this.Font).Width > this.Width - 30)
{
int i = -1;
while (g.MeasureString(text, this.Font).Width > this.Width - 30)
{
i++;
text += this.Text.Substring(i, 1);
}
}
else
{
text = this.Text;
}
float temp;
if (this.RightToLeft == RightToLeft.No)
{
temp = 1;
}
else
{
temp = this.Width - g.MeasureString(text, this.Font).Width;
}
txtLoc = new PointF(temp, 4);
g.DrawString(text, this.Font, textBrush, txtLoc);
} #endregion private void UpdateState()
{
States temp = state;
if (this.Enabled)
{
if (ClientRectangle.Contains(PointToClient(Control.MousePosition)))
{
this.state = States.Focused;
}
else if (this.Focused)
{
this.state = States.Focused;
}
else
{
this.state = States.Normal;
}
}
else
{
this.state = States.Disabled;
}
if ((state & temp) != state)
{
this.Invalidate();
}
}
}
}大致的翻译了一下那个VB.NET的Source
你自己在根据需要改改就可以了
主要的思路就是继承一个TextBox
然后监视他的WndProc消息
在WM_Paint消息的时候重绘这个TextBox
如果是Button或者窗口的话就容易的多
直接利用GraphicsPath修改Region就可以了
GraphicsPath p = new GraphicsPath();
int width = this.ClientSize.Width;
int height = this.ClientSize.Height;
p.AddClosedCurve(new Point[]{new Point(width/2, height/10),
new Point(width,0), new Point(width, height/3),
new Point(width-width/3, height),
new Point(width/7, height-height/8)});
this.Region = new Region(p);
this.Invalidate();
这个改变的很清楚
当然你要调整相应的DrawText代码