| 发表于:2007-09-18 13:46:1314楼 得分:0 |
using system; 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就可以了 | | |
|