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



c# 怎么画用鼠标拉出来的虚线框????


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


c# 怎么画用鼠标拉出来的虚线框????[已结贴,结贴人:wuyt2007]
发表于:2007-10-11 23:27:41 楼主
想画那个十分常见的用鼠标拖出来虚线框,      
    我的panel里原来画了一个几何模型,      
    如果我在ondraw里面画虚线框的画,不是把原来的模型也重画了一遍吗?      
只想单纯的画出一个矩形框,新手,大虾见谅!!!
发表于:2007-10-12 08:56:581楼 得分:0
自己顶一下!
发表于:2007-10-12 17:54:042楼 得分:0
刚看了你的问题   写了一个!

命名空间
using   system.drawing;

定义两个变量
bool   mouseisdow=false;
rectangle   mouserect   =   rectangle.empty;

定义三个方法
private   void   resizetorectangle(point   p)
{
      drawrectangle();
      mouserect.width   =   p.x   -   mouserect.left;
      mouserect.height   =   p.y   -   mouserect.top;
      drawrectangle();
}
private   void   drawrectangle()
{
      rectangle   rect   =   this.rectangletoscreen(mouserect);
      controlpaint.drawreversibleframe(rect,   color.white,   framestyle.dashed);
}

private   void   drawstart(point   startpoint)
{
      this.capture   =   true;
      cursor.clip   =   this.rectangletoscreen(this.bounds);
      mouserect   =   new   rectangle(startpoint.x,   startpoint.y,   0,   0);
}

在鼠标按下事件里写(一定是鼠标按下事件mousedown   因为我的参数e是鼠标数据对象
(不过你也可以传坐标))
mouseisdown   =   true;
drawstart(e.location);

在鼠标移动(mousemove)事件里写    
if   (mouseisdown)
      resizetorectangle(e.location);


在鼠标释放(mouseup)事件里写      
this.capture   =   false;
cursor.clip   =   rectangle.empty;
mouseisdown   =   false;
drawrectangle();
mouserect   =   rectangle.empty;

这样就没有重绘控件  
发表于:2007-10-12 19:00:233楼 得分:0
up
发表于:2007-10-15 08:53:024楼 得分:0
看了你的问题   写了一个!

命名空间
using   system.drawing;

定义两个变量
bool   mouseisdow=false;
rectangle   mouserect   =   rectangle.empty;

定义三个方法
private   void   resizetorectangle(point   p)
{
      drawrectangle();
      mouserect.width   =   p.x   -   mouserect.left;
      mouserect.height   =   p.y   -   mouserect.top;
      drawrectangle();
}
private   void   drawrectangle()
{
      rectangle   rect   =   this.rectangletoscreen(mouserect);
      controlpaint.drawreversibleframe(rect,   color.white,   framestyle.dashed);
}

private   void   drawstart(point   startpoint)
{
      this.capture   =   true;
      cursor.clip   =   this.rectangletoscreen(this.bounds);
      mouserect   =   new   rectangle(startpoint.x,   startpoint.y,   0,   0);
}

在鼠标按下事件里写(一定是鼠标按下事件mousedown   因为我的参数e是鼠标数据对象
(不过你也可以传坐标))
mouseisdown   =   true;
drawstart(e.location);

在鼠标移动(mousemove)事件里写    
if   (mouseisdown)
      resizetorectangle(e.location);


在鼠标释放(mouseup)事件里写      
this.capture   =   false;
cursor.clip   =   rectangle.empty;
mouseisdown   =   false;
drawrectangle();
mouserect   =   rectangle.empty;

这样就没有重绘控件  
发表于:2007-10-15 08:56:035楼 得分:0
晕!   看不见帖子!以为前天没发出去!
现在发了又全部显示出来     怪!  
发表于:2007-10-15 10:26:566楼 得分:0
非常感谢你!
发表于:2007-10-15 10:44:527楼 得分:0
还存在问题,鼠标up后并不释放这个矩形,还在继续画???
发表于:2007-10-15 13:50:318楼 得分:0
怎么不响应up事件????      
发表于:2007-10-15 14:11:309楼 得分:0

mouseisdown  
我定义时写错了
mouseisdow
发表于:2007-10-15 14:12:0910楼 得分:0
其他没问题     呵呵
发表于:2007-10-15 14:15:0211楼 得分:0
不是的     那个地方我改了,你调试过吗?     程序一跑起来就不响应up了     断点调试的时候就有   很奇怪
发表于:2007-10-15 14:20:1712楼 得分:0
  代码看看!!
发表于:2007-10-15 14:22:5013楼 得分:0
看了       都调了一会了     还是通不过!
发表于:2007-10-15 14:25:0114楼 得分:0
这是我这边试的
没问题哦~
using   system;
using   system.collections.generic;
using   system.componentmodel;
using   system.data;
using   system.drawing;
using   system.text;
using   system.windows.forms;

namespace   probation
{
        public   partial   class   frmmain   :   form
        {
                bool   mouseisdown   =   false;
                rectangle   mouserect   =   rectangle.empty;  
                public   frmmain()
                {
                        initializecomponent();
                        this.mousedown+=new   mouseeventhandler(frmmain_mousedown);
                        this.mousemove+=new   mouseeventhandler(frmmain_mousemove);
                        this.mouseup   +=new   mouseeventhandler(frmmain_mouseup);
                }
                void     frmmain_mouseup(object   sender,   mouseeventargs   e)
                {
                        this.capture   =   false;  
                        cursor.clip   =   rectangle.empty;  
                        mouseisdown   =   false;  
                        drawrectangle();
                        mouserect   =   rectangle.empty;
                }
                void     frmmain_mousemove(object   sender,   mouseeventargs   e)
                {
                        if   (mouseisdown)  
                              resizetorectangle(e.location);  
                }
                void     frmmain_mousedown(object   sender,   mouseeventargs   e)
                {
                        mouseisdown   =   true;
                        drawstart(e.location);  
                }
                private   void   resizetorectangle(point   p)  
                {  
                      drawrectangle();  
                      mouserect.width   =   p.x   -   mouserect.left;  
                      mouserect.height   =   p.y   -   mouserect.top;  
                      drawrectangle();  
                }  
                private   void   drawrectangle()  
                {  
                      rectangle   rect   =   this.rectangletoscreen(mouserect);  
                      controlpaint.drawreversibleframe(rect,   color.white,   framestyle.dashed);  
                }  
                private   void   drawstart(point   startpoint)  
                {  
                      this.capture   =   true;
                      cursor.clip   =   this.rectangletoscreen(new   rectangle(0,   0,   clientsize.width,   clientsize.height));
                        mouserect   =   new   rectangle(startpoint.x,   startpoint.y,   0,   0);  
                }  
        }
}
发表于:2007-10-15 14:26:4615楼 得分:0
呵呵   我是说给我看看你的代码
发表于:2007-10-15 14:34:3316楼 得分:0
using   system;
using   system.collections.generic;
using   system.componentmodel;
using   system.data;
using   system.drawing;
using   system.text;
using   system.windows.forms;

namespace   windowsapplication1
{
        public   partial   class   form1   :   form
        {

                public   form1()
                {
                        initializecomponent();
                }

                bool   mouseisdown   =   false;
                rectangle   mouserect   =   rectangle.empty;

                private   void   panel1_mouseup(object   sender,   mouseeventargs   e)
                {
                        this.capture   =   false;
                        cursor.clip   =   rectangle.empty;
                        mouseisdown   =   false;
                        drawrectangle();
                        mouserect   =   rectangle.empty;
                }

                private   void   panel1_mousemove(object   sender,   mouseeventargs   e)
                {
                        if   (mouseisdown)
                                resizetorectangle(e.location);
                }

                private   void   panel1_mousedown(object   sender,   mouseeventargs   e)
                {
                        mouseisdown   =   true;
                        drawstart(e.location);
                }

                private   void   resizetorectangle(point   p)
                {
                        drawrectangle();
                        mouserect.width   =   p.x   -   mouserect.left;
                        mouserect.height   =   p.y   -   mouserect.top;
                        drawrectangle();
                }

                private   void   drawrectangle()
                {
                        rectangle   rect   =   this.rectangletoscreen(mouserect);
                        controlpaint.drawreversibleframe(rect,   color.red,   framestyle.thick);
                }

                private   void   drawstart(point   startpoint)
                {
                        this.capture   =   true;
                        cursor.clip   =   this.rectangletoscreen(this.bounds);
                        mouserect   =   new   rectangle(startpoint.x,   startpoint.y,   0,   0);
                }

        }
                       
}
这个哪错了吗?   真晕...就过家个panel
发表于:2007-10-15 14:47:0717楼 得分:0
您找到问题了没?是不是panel控件的问题???
发表于:2007-10-15 14:49:4218楼 得分:0
呵呵!
如果是加panel的话

把drawstart()方法里的cursor.clip   =   this.rectangletoscreen(this.bounds);
改为cursor.clip   =   this.rectangletoscreen(this.panel1.clientrectangle));
this.capture   =   true;改为   this.panel1.capture   =   true;
mouseup里的   this.capture   =   false;改为
this.panel1.capture   =   false;

发表于:2007-10-15 14:54:4919楼 得分:0
这是设置鼠标筐选时鼠标的移动区域   和控件对鼠标的捕获
cursor.clip   =   this.rectangletoscreen(this.bounds);
这是设置鼠标筐选时鼠标的移动区域   根据你的需要自己去设置
发表于:2007-10-15 14:56:3020楼 得分:0
哦,又学到了不少东西.非常感谢,耽误了你这么多时间!!!
发表于:2007-10-15 15:01:1821楼 得分:0
你最好是写在   窗体里
如果要写panel   里   就得继承panel  
发表于:2007-10-15 15:02:3922楼 得分:0
好的,知道了!


快速检索

最新资讯
热门点击