您的位置:程序门 -> .net技术 -> c#



如何改变textbox形状


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何改变textbox形状[已结贴,结贴人:zf_ben]
发表于:2007-09-18 09:42:27 楼主
如题
发表于:2007-09-18 09:49:051楼 得分:0
用css可以     类似这个     那个图片就是椭圆的
.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;
}
发表于:2007-09-18 09:49:202楼 得分:0
前台显示的时候就是椭圆的了
发表于:2007-09-18 09:50:303楼 得分:0
winform   or   web   form?
发表于:2007-09-18 09:52:024楼 得分:0
不好意思,我是要winform的
发表于:2007-09-18 10:05:525楼 得分:0
顶一下
发表于:2007-09-18 10:39:246楼 得分:20
http://www.codeproject.com/useritems/roundedcornertextbox.asp
发表于:2007-09-18 10:55:327楼 得分:0
自己画,   或者直接使用wpf
发表于:2007-09-18 11:01:138楼 得分:0
下个免费的控件吧。
发表于:2007-09-18 11:25:559楼 得分:0
有c#的吗?找了好久也没有找到这样的控件,vb的我看的不是很懂
发表于:2007-09-18 12:15:0410楼 得分:0
前面看到一个高手写的:
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);//这里写你要绘制的形状。
                                                }
                                        }
                                }
                        }
                }
        }

}
发表于:2007-09-18 12:24:2711楼 得分:0
mark
发表于:2007-09-18 12:25:4212楼 得分:0
如楼上所示
自定义一个继承textbox的自定义控件,重绘textbox
发表于:2007-09-18 13:34:4113楼 得分:0
那样做只是在textbox里画了一个形状,我是希望textbox的外形都变了,textbox不支持onpaint事件
发表于: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就可以了
发表于:2007-09-18 13:57:2315楼 得分:0
直接在

在wm_paint消息的时候,把drawborder换成如下source
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代码
发表于:2007-09-18 14:16:5816楼 得分:0
我改成菱形,结果一个长方形的白块,郁闷
发表于:2007-09-18 14:49:0017楼 得分:0
可以了,谢谢!!马上给分
发表于:2007-09-18 15:48:3218楼 得分:0
若有冒犯,敬请原谅!

程序世界6,7,8群已定成立,给大家提供了技术知识交流的平台,欢迎加入.
宗旨:
1,我为人人,人人为我!!!!!!!!!
2,交流技术   拒绝色情   拒绝暴力   拒绝不文明!
3,述说程序员的心声,相互促进,共同发展!
群号:
20273994
20274022
20273919
发表于:2007-09-18 16:57:0219楼 得分:0
mark
发表于:2007-09-19 12:18:4020楼 得分:0
mark!
发表于:2007-10-10 13:46:1821楼 得分:0
学习学习
发表于:2007-10-10 14:20:1522楼 得分:0
椭圆,学习
发表于:2007-10-10 14:42:4023楼 得分:0
重画后光标有尾迹
发表于:2007-10-10 14:56:4924楼 得分:0
把效果图贴出来看看呢。
发表于:2007-10-10 16:29:5525楼 得分:0
路过.但是真的很好..谢谢
发表于:2007-10-10 16:32:4326楼 得分:0
mark
发表于:2007-10-11 11:17:3327楼 得分:0
值   得学习一下
发表于:2007-10-11 17:51:5228楼 得分:0
mark   study


快速检索

最新资讯
热门点击