c# – 绘制时如何控制圆的大小和位置

c# – 绘制时如何控制圆的大小和位置,第1张

概述我需要帮助解决一个我不知道如何解决问题的问题.我有自己的类从PictureBox延伸,我可以正确显示我的矩形,我可以毫无问题地移动它. 但是当我尝试在矩形周围画一个圆圈时,我遇到了问题.我想在用户制作一个mouseclickevent时绘制圆圈.它绘制一个圆,但在矩形上.我不知道你是否理解我…我把我的代码放在下面,也是我的结果和我想要的结果. 我的代码: OnClick事件: //Métodos 我需要帮助解决一个我不知道如何解决问题的问题.我有自己的类从PictureBox延伸,我可以正确显示我的矩形,我可以毫无问题地移动它.

但是当我尝试在矩形周围画一个圆圈时,我遇到了问题.我想在用户制作一个mouseclickevent时绘制圆圈.它绘制一个圆,但在矩形上.我不知道你是否理解我…我把我的代码放在下面,也是我的结果和我想要的结果.

我的代码:

OnClick事件:

//Métodos para mover la unIDad    bool unIDadPulsada = false;    private Point MouseDownLocation;    protected overrIDe voID OnMouseDown(MouseEventArgs e)    {        unIDadPulsada = true;        base.OnMouseDown(e);        MouseDownLocation = e.Location;        Dibujarlimites();    }

设置pictureBox的方法:

public voID Colocar(Control control,UnIDad unIDad,Point p)    {        unIDad.Location = p;        control.Controls.Add(unIDad);    }

绘制方法:

public voID Dibujarlimites()    {        using (Graphics g = CreateGraphics())        {            using (Pen pen = new Pen(color.Red,2))            {                float[] dashValues = { 5,2,15,4 };                pen.DashPattern = dashValues;                DrawCircle(g,pen,20);            }        }    }    public voID DrawCircle(Graphics g,Pen pen,float centerX,float centerY,float radius)    {        g.DrawEllipse(pen,centerX - radius,centerY - radius,radius + radius,radius + radius);    }

结果我有:
左边的矩形是带有圆圈绘图的pictureBox.右边的矩形是没有圆的pictureBox,原始的矩形.

我希望得到的结果:
圆圈围绕矩形绘制.

为Youri编辑:

using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;using System.Drawing;using System.windows.Forms;using WHF.PropertIEs;namespace WHF{public class UnIDad : PictureBox{    //Constructor    public UnIDad(string nombre,string tipo,int movimIEnto,int ha,int hp,int fuerza,int resistencia,int herIDas,int iniciativa,int ataques,int lIDerazgo,int coste,string rutaimagen)    {        tipoUnIDad = tipo;        movimIEntoUnIDad = movimIEnto;        nombreUnIDad = nombre;        costeUnIDad = coste;        haUnIDad = ha;        hpUnIDad = hp;        fuerzaUnIDad = fuerza;        resistenciaUnIDad = resistencia;        iniciativaUnIDad = iniciativa;        ataquesUnIDad = ataques;        lIDerazgoUnIDad = lIDerazgo;        rutaimagenUnIDad = rutaimagen;    }    //PropIEdades    public string nombreUnIDad { get; set; }    public string tipoUnIDad { get; set; }    public int movimIEntoUnIDad { get; set; }    public int costeUnIDad { get; set; }    public int haUnIDad { get; set; }    public int hpUnIDad { get; set; }    public int fuerzaUnIDad { get; set; }    public int resistenciaUnIDad { get; set; }    public int herIDasUnIDad { get; set; }    public int iniciativaUnIDad { get; set; }    public int ataquesUnIDad { get; set; }    public int lIDerazgoUnIDad { get; set; }    public string rutaimagenUnIDad { get; set; }    //Método para dibujar unIDad    public voID Colocar(Control control,Point p)    {        unIDad.Location = p;        control.Controls.Add(unIDad);    }    //Métodos para mover la unIDad    bool unIDadPulsada = false;    private Point MouseDownLocation;    protected overrIDe voID OnMouseDown(MouseEventArgs e)    {        unIDadPulsada = true;        base.OnMouseDown(e);        MouseDownLocation = e.Location;        //Dibujarlimites();        float x = Location.X + e.X;        float y = Location.Y + e.Y;        Graphics graphics = CreateGraphics();        PointF center = new PointF(x,y);//this.ClIEntSize.WIDth / 2F,this.ClIEntSize.Height / 2F);        float radius = 100;        PointF rectOrigin = new PointF(center.X - radius,center.Y - radius);        RectangleF r = new RectangleF(rectOrigin,new Sizef(radius * 2F,radius * 2F));        using (Pen p = new Pen(color.Red,4))        {            p.DashStyle = System.Drawing.drawing2d.DashStyle.Dot;            graphics.DrawEllipse(p,r);        }    }    protected overrIDe voID OnMouseMove(MouseEventArgs e)    {        base.OnMouseMove(e);        if (unIDadPulsada)        {            left = e.X + left - MouseDownLocation.X;            top = e.Y + top - MouseDownLocation.Y;        }    }    protected overrIDe voID onmouseup(MouseEventArgs e)    {        unIDadPulsada = false;        base.OnMouseDown(e);        limpiarlimites();    }    //Método para dibujar la zona límite de movimIEnto de la unIDad    public voID Dibujarlimites()    {        using (Graphics g = CreateGraphics())        {            using (Pen pen = new Pen(color.Red,20);            }        }    }    //Método para limpiar el dibujo de la zona límite    public voID limpiarlimites()    {    }    public voID DrawCircle(Graphics g,radius + radius);    }    public voID FillCircle(Graphics g,Brush brush,float radius)    {        g.FillEllipse(brush,radius + radius);    }}}
解决方法 好的,这是一个例子,我尝试以与你相同的方式应用,我创建了自己的图片框,

class MyPBox : PictureBox    {    public MyPBox()    {        this.Backcolor = color.Red; // for see better        this.Location = new Point(50,50); // set location at form    }    protected overrIDe voID OnPaint(PaintEventArgs pe)    {        if (this.Parent != null)        {            this.Parent.Paint += Parent_Paint; // pictureBox's paint means it added to parent so we need to trigger parent's paint event        }        base.OnPaint(pe);    }    bool clickPerformed = false; // to catch control has mouse down    protected overrIDe voID OnMouseDown(MouseEventArgs e)    {        base.OnMouseDown(e);        clickPerformed = true; // set mouse down        Control tempSender =  this.Parent; // get sender        tempSender.InvalIDate(); // invalIDate to trigger paint event    }    private voID Parent_Paint(object sender,PaintEventArgs e)    {        if (clickPerformed)        {            using (Graphics g = e.Graphics)            {                using (Pen pen = new Pen(color.Black,2))                {                    float locationX = this.Location.X + this.Size.WIDth / 2;                    float locationY = this.Location.Y + this.Size.Height / 2;                    float radius = (this.Size.Height + this.Size.WIDth) / 2;                    float[] dashValues = { 5,4 };                    pen.DashPattern = dashValues;                    DrawCircle(g,locationX,locationY,radius); // draw circle                     clickPerformed = false; // process done so set it to false                }            }        }        base.OnPaint(e);    }    protected overrIDe voID onmouseup(MouseEventArgs e)    {        this.Parent.InvalIDate(); // mouse up circle should be erased,so invalIDate again to trigger paint,but this time clickPerformed is false        // so it won't draw circle again        base.OnMouseDown(e);    }    public voID DrawCircle(Graphics g,radius + radius);    }}

结果(我点击图片框顺便说一句:));

希望有帮助,

总结

以上是内存溢出为你收集整理的c# – 绘制时如何控制圆的大小位置全部内容,希望文章能够帮你解决c# – 绘制时如何控制圆的大小和位置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1232757.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存