edit an the text inside an drawing shape in c# - c#

how i can edit the text of a drawing rectangle (class digram) when i double click on it ? this the code in c# how i draw the class digram
g1.DrawRectangle(pen, x1, y1, width, height);
g1.DrawString("class Name", new Font("Arial", 10), new SolidBrush(Color.Black), x1,(y1 +5)); //TextRenderingHint
g1.DrawLine(pen, x1, y1 + 23, x1 + width, y1 + 23);
g1.DrawString("+ attribute :type ", new Font("Arial", 10), new SolidBrush(Color.Black), x1, (y1 + 30));
g1.DrawLine(pen, x1, y1 + 48, x1 + width, y1 + 48);
g1.DrawString("+ operation (args list):return", new Font("Arial", 10), new SolidBrush(Color.Black), x1, (y1 + 55));

Well, you can't.
You are not working with variables, but you are rendering an output on the screen -> you painted something. You would need to locate the area, erase it and redraw your string.
If you want to edit your content, you would need to integrate controls like Textboxes or labels into your diagram.

Related

Recursively draw rectangles in WinForms

I'm currently coding a Sierpinski Carpet. Now i have the problen, that it works per iteration only once and not at every square on my whole screen.
This is the code i use:
private void Carpet(Graphics g, SolidBrush brush, Single
x, Single y, Single width, Single height, int iteration)
{
if (iteration == 0) {}
else
{
for (int i = 0; i < iteration + 1; i++)
{
width = Convert.ToSingle(Width / Math.Pow(3,i-1));
height = Convert.ToSingle(Height / Math.Pow(3, i-1));
Single x1 = x + width/3;
Single x2 = x1 + width;
Single x3 = x2 + width;
Single y1 = y + height/3;
Single y2 = y1 + height;
Single y3 = y2 + height;
g.FillRectangle(brush, x1, y1, width / 3, height / 3);
g.FillRectangle(brush, x1, y2, width / 3, height / 3);
g.FillRectangle(brush, x1, y3, width / 3, height / 3);
g.FillRectangle(brush, x2, y1, width / 3, height / 3);
g.FillRectangle(brush, x2, y3, width / 3, height / 3);
g.FillRectangle(brush, x3, y1, width / 3, height / 3);
g.FillRectangle(brush, x3, y2, width / 3, height / 3);
g.FillRectangle(brush, x3, y3, width / 3, height / 3);
}
}
}
How can i enhance my code, so it recursively draws Rectangles everywhere and not just on the first part of my Screen.
You draw only the smallest rectangles and nothing else. But to find the position of all of them you have to have a recursive function. So, determine coordinates and call itself recursively in all iterations except the last one. If it's the last iteration then draw just one rectangle that fills the whole area given with (x, y, width, height).
Instead of the for loop you have to have eight recursive calls. And in case of the last iteration you call only one g.FillRectangle. But now the problem is how to determine which iteration is the last one? When you call Carpet recursively call it with iteration - 1, and the last iteration is then the one with value zero.

Increasing the height of Panel according to wrapped string Label in Panel

i am trying to develop a win form for chatting purpose. I am developing Chat bubbles using pure inbuilt functions of .Net Framework, No fancy UI, No third party libraries.
Now let's have a look on how every thing is being done.
My following function is responsible for generating a Panel dynamically for each chat message received, the Pain event is used to draw rounded rectangle and color is transparent. A picture box is used to show static avatar.
private void SetRemoteMessage(string msg)
{
PictureBox pb = new PictureBox();
pb.Bounds = new Rectangle(0, 0, 72, 72);
pb.Image = Base64ToImage(avatar_his);
Panel p = new Panel();
Label lb = new Label();
lb.BackColor = Color.Transparent;
lb.ForeColor = Color.Blue;
lb.Text = msg;
lb.Font = new Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Point);
p.Bounds = new Rectangle(rX, rY, (Width / 2) - 25, pb.Height);
p.BackColor = Color.Transparent;
lb.Size = new Size(p.Width - pb.Width, p.Height);
lb.Paint += _control_Paint;
lb.Location = new Point(pb.Width + 5, 0);
p.Controls.Add(pb);
p.Controls.Add(lb);
SetPanel(p);
rY += p.Height + 20;
mY += p.Height + 20;
}
following is the Paint event binded to the parent "Container" Panel so that a simple rounded rectangle is shown for each bubble
private void _control_Paint(object sender, PaintEventArgs e)
{
Control c = (Control)sender;
if(!c.Name.Equals("lb"))
{
Graphics v = e.Graphics;
DrawRoundRect(v, Pens.Blue, e.ClipRectangle.Left, e.ClipRectangle.Top, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1, 10);
}
else
{
using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
{
e.Graphics.DrawString(c.Text, font1, Brushes.Blue, c.Bounds);
}
}
base.OnPaint(e);
}
and the following function to actually generate rounded rectangles
private void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius)
{
GraphicsPath gp = new GraphicsPath();
gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
gp.CloseFigure();
g.DrawPath(p, gp);
}
The chat message is not wrapped automatically so to wrap it, the Pain method is used as it can be seen in Paint event . .the output is good as expected instead of one thing
The Problem can be seen easily the Panel height is not increasing according when a large message is given to display in bubble.
What i tried already is
Measuring string length with Graphics class, but i was not able to implement it with success
Enabling Scrollbars, Yes this approach worked but i am not interested to use this behavior
Counting string length and increasing height, This works but not efficient, specially when form is resized all calculations of measurement then become invalid.

Joining bezier curve to other drawings

I have implemented a rounded rectangle extension method, defined here.
public static Graphics DrawRRectangle(this Graphics g, Pen p, int x, int y, int width, int height, int feathering)
{
g.DrawLine(p, x, y + feathering, x, y + height - feathering);
g.DrawBezier(p, new Point(x, y + height - feathering),
new Point(x, y + height - feathering / 2), new Point(x + feathering / 2, y + height),
new Point(x + feathering, y + height));
g.DrawLine(p, x + feathering, y + height , x + width - feathering, y + height);
g.DrawBezier(p, new Point(x + width - feathering, y + height),
new Point(x + width - feathering / 2, y + height), new Point(x + width, y + height - feathering / 2),
new Point(x + width, y + height - feathering));
g.DrawLine(p, x + width, y + height - feathering, x + width, y + feathering);
g.DrawBezier(p, new Point(x + width, y + feathering),
new Point(x + width, y + feathering / 2), new Point(x + width - feathering / 2, y),
new Point(x + width - feathering, y));
g.DrawLine(p, x + width - feathering, y, x + feathering, y);
g.DrawBezier(p, new Point(x + feathering, y),
new Point(x + feathering / 2, y), new Point(x, y + feathering / 2),
new Point(x, y + feathering));
return g;
}
However when I use this method like so
g.DrawRRectangle(p, 100, 100, 1000, 1000, 100);,
I do not get the outcome I wanted, each of the corners are either misaligned of their pixels do not match up As seen in the images below.
Any suggestions anybody could offer would be helpful, I am unsure if this is a problem with the equations used to generate my curves however this is the first time I am dabbling with graphics, so it could just be my thinking. Thanks.
Whilst I can't comment on your implementation, you're going to run into problems further down the road with this. Your implementation will give the appearance of drawing a rounded rectangle, but say for example in future you want to fill the shape, you won't be able to because GDI/GDI+ won't see the drawn shapes as a single consecutive shape.
In this respect you should use a GraphicsPath.
See here for a complete solution for drawing rounded rectangles using a GraphicsPath.

Lost trying to draw a line of x length y degrees

I think its my maths that's letting me down!
I'm trying to draw a line of x length at Y degrees on a picturebox control with a North South East west, lines.
the below works but draws the line line at 250 degrees not 290!?
Bitmap bmp = new Bitmap(400, 400);
Graphics g = Graphics.FromImage(bmp);
// smooth graphics
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawLine(new Pen(Color.Black, 2), 205, 20, 205, 385);
g.DrawLine(new Pen(Color.Red, 2), 20, 205, 390, 205);
// let's draw a coordinate equivalent to (20,30) (20 up, 30 across)
g.DrawString("N", new Font("Calibri", 12 , FontStyle.Bold), new SolidBrush(Color.Black), 197, 0);
g.DrawString("S", new Font("Calibri", 12, FontStyle.Bold), new SolidBrush(Color.Black), 197, 385);
g.DrawString("W", new Font("Calibri", 12, FontStyle.Bold), new SolidBrush(Color.Black), 0, 195);
g.DrawString("E", new Font("Calibri", 12, FontStyle.Bold), new SolidBrush(Color.Black), 390, 195);
//Draw Wind line
int x = 205, y = 205;
int wSpeed = 30*3; //length of line
int angle = 290; //angle to draw line at.
int startX = x;
int startY = y;
int endX = Convert.ToInt32(Math.Round(x + wSpeed * Math.Sin(Calcs.Radians(angle))));
int endY = Convert.ToInt32(Math.Round(y + wSpeed * Math.Cos(Calcs.Radians(angle))));
g.DrawLine(new Pen(Color.Blue, 2), startX, startY, endX, endY);
PictureBox display = pictureBox1;
this.Controls.Add(display);
display.Image = bmp;
public static double Radians(double dDegrees)
{
double result = Math.PI * dDegrees / 180.0;
return result;
}
If you look at the results: its -20 degrees from 270, instead of +20 expected, meaning you v got correct Y but wrong X. I suggest check(change) the sign of X coord. Graphics is very often trial-error unless you are expert.
int endX = Convert.ToInt32(Math.Round(x - wSpeed * Math.Sin(Calcs.Radians(angle))));

drawing circle in the middle of the desktop

heyho folks,
could you please help me to draw a circle in the middle of the desktop.
i am very frustrated about the desktop coordinates.
this is for an overlay
here is my code:
Graphics g = default(Graphics);
g = pictureBox1.CreateGraphics();
//g.DrawEllipse(Pens.Red, 10, 10, 200, 200);
int screenmiddlx = this.Width / 2;
int screenmiddly = this.Height / 2;
int kscreenmoddly = Height;
int kscreenmoddlx = Width;
g.DrawLine(Pens.Red, screenmiddlx - 10, screenmiddly, screenmiddlx + 10, screenmiddly);
g.DrawLine(Pens.Red, screenmiddlx, screenmiddly - 10, screenmiddlx, screenmiddly + 10);
g.DrawEllipse(Pens.Red,,,,);
this is my code:
Graphics kp = e.Graphics;
kp.DrawEllipse(Pen, x, y, width, height);
// exemple for you:
kp.DrawEllipse(Pens.Red, screenmiddlx, screenmiddly, 20, 20);

Categories