I have this code and when I run it, it has moved but has two label display on screen, one is no move, one has move
public partial class Form1: Form
{
int x = 25, y = 1;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.SetBounds(x, y, 255, 255);
x++;
if (x >= 800)
{
x = 1;
}
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "hii";
label1.Font = new Font(" ", 22, FontStyle.Bold);
timer1.Interval = 10;
timer1.Start();
}
}
You have to add one more label from toolbox to form. In timer set new label's bounds also like this:
int x = 25, y = 1;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick_1(object sender, EventArgs e)
{
label1.SetBounds(x, y, 255, 255);
label2.SetBounds(x, 100, 255, 255);
x++;
if (x >= 800)
{
x = 1;
}
}
private void Form1_Load_1(object sender, EventArgs e)
{
label1.Text = "hii";
label1.Font = new Font(" ", 22, FontStyle.Bold);
label2.Text = "hii2";
label2.Font = new Font(" ", 22, FontStyle.Bold);
timer1.Interval = 10;
timer1.Start();
}
Related
Again, I am asking a question about a USB Printer. This time however, the issue is more on the programming end of the spectrum.
I have a windows forms application in which I generate a receipt, which is then supposed to be sent to the printer. The Following things work: Generating the receipt, Sending the data to the printer.
The data however is only an empty strip of paper. I have the feeling that the Software is ignoring the Panel contents and just sends the empty panel to the printer.
The Sourcecode is as follows:
namespace MakeyMakey
{
public partial class Form1 : Form
{
public void Gridsettings()
{
GVReceipt.ScrollBars = ScrollBars.None;
GVReceipt.RowHeadersVisible = false;
GVReceipt.ColumnCount = 3;
GVReceipt.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
GVReceipt.CellBorderStyle = DataGridViewCellBorderStyle.None;
}
public Form1()
{
InitializeComponent();
pCompanyImage.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap Logo = new Bitmap(Bitmap.FromFile(#"C:/Users/manue/source/repos/MakeyMakey/MakeyMakey/logo.png"));
pCompanyImage.Image = MakeBW(Logo);
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("Hier kann so gut wie alles stehen", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);
pQRCode.Image = qrCodeImage;
panel1.Height = GVProduct.Height + 220;
GVReceipt.Columns.Add("Name", "Name");
GVReceipt.Columns.Add("QTY", "QTY");
GVReceipt.Columns.Add("Price", "Price");
GVReceipt.Columns[0].Width = 50;
GVReceipt.Columns[1].Width = 15;
GVReceipt.Columns[2].Width = 50;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string[] row = new string[] {txtName.Text, txtQty.Text, txtPrice.Text };
GVReceipt.Rows.Add(row);
GVProduct.Rows.Add(row);
Gridsettings();
Calc();
txtName.Clear();
txtQty.Clear();
txtPrice.Clear();
txtName.Focus();
}
private void GVResize()
{
//MessageBox.Show(GVReceipt.Height.ToString());
GVReceipt.Height = GVReceipt.ColumnHeadersHeight + GVReceipt.RowCount * GVReceipt.Rows[0].Height;
//MessageBox.Show(GVReceipt.Height.ToString());
panel1.Height += GVReceipt.Rows[0].Height;
//MessageBox.Show(panel1.Height.ToString());
GVReceipt.ClearSelection();
Refresh();
}
private void GVReceipt_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
}
private void GVReceipt_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
panel1.Height = 389;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
public Bitmap MakeBW(Bitmap Map)
{
Bitmap newbmp = new Bitmap(Map.Width, Map.Height); // New image
for (int row = 0; row < Map.Width; row++) // Indicates row number
{
for (int column = 0; column < Map.Height; column++) // Indicate column number
{
var colorValue = Map.GetPixel(row, column); // Get the color pixel
var averageValue = ((int)colorValue.R + (int)colorValue.B + (int)colorValue.G) / 3; // get the average for black and white
newbmp.SetPixel(row, column, Color.FromArgb(averageValue, averageValue, averageValue)); // Set the value to new pixel
}
}
return newbmp;
}
public void Calc()
{
decimal total = 0;
for(int i = 0; i < GVReceipt.RowCount-1; i++)
{
total += Convert.ToDecimal(GVReceipt.Rows[i].Cells[2].Value) * Convert.ToDecimal(GVReceipt.Rows[i].Cells[1].Value);
}
//Console.WriteLine("HELLO");
//MessageBox.Show(total.ToString());
lbltotal.Text = total.ToString() + "€";
Refresh();
GVResize();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Print(panel1);
}
Bitmap MemoryImage;
PrintDocument printdoc1 = new PrintDocument();
PrintPreviewDialog previewdlg = new PrintPreviewDialog();
public void GetPrintArea(Panel pnl)
{
MemoryImage = new Bitmap(pnl.Width, pnl.Height);
Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
}
public void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(MemoryImage, 0, 0);
}
public void Print(Panel pnl)
{
GetPrintArea(pnl);
printdoc1.PrinterSettings.PrinterName = "Generico";
printdoc1.DefaultPageSettings.PaperSize = new PaperSize("Custom", panel1.Width, panel1.Height);
previewdlg.Document = printdoc1;
previewdlg.ShowDialog();
printdoc1.Print();
}
private void btnPrinter_Click(object sender, EventArgs e)
{
printDocument1.Print();
GVProduct.Rows.Clear();
GVReceipt.Rows.Clear();
Calc();
}
private void GVReceipt_RowsAdded_1(object sender, DataGridViewRowsAddedEventArgs e)
{
//GVResize();
}
}
I have also included two images, one shows what the receipt looks like, the other what the print preview generated.
I'd highy appreciate any help on that topic.
I am a novice in programming and i need Your advice, please.
I was tasked with writing a programm that will be counting number of items needed according to length of a track. This track we need to draw by ourselves, but I don't want to make this application full screen, so I need a Zooming function and for a life of me I can't get how should i do it.
Please help!
And here's the code.
namespace RFIDGovno
{
public partial class Form1 : Form
{
List<Line> lines = new List<Line>();
List<Dot> dots = new List<Dot>();
bool isVert;
Point sPlace;
Point ePlace;
bool downed=false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Point temp = e.Location;
if (downed)
{
if (!(Math.Abs(e.X - sPlace.X) > Math.Abs(e.Y - sPlace.Y)))
{
temp.X = sPlace.X;
}
else
{
temp.Y = sPlace.Y;
}
downed = false;
ePlace = temp;
dots.Add(new Dot(ePlace));
lines.Add(new Line(sPlace,ePlace,isVert, isVert ? Math.Abs(sPlace.Y-ePlace.Y) : Math.Abs(sPlace.X - ePlace.X)));
}else
{
if (!dots.Any<Dot>())
{
downed = true;
sPlace = e.Location;
dots.Add(new Dot(sPlace));
}
else if(dots.Where(t => t.fuse).Any<Dot>())
{
downed = true;
sPlace = dots.Where(t => t.fuse).First().place;
}
}
drawing(null);
}
private void Zooming()
{
}
private void drawing(Point? mouse)
{
Graphics g = pictureBox1.CreateGraphics();
Bitmap im = new Bitmap(pictureBox1.Width-1, pictureBox1.Height-1);
Graphics q = Graphics.FromImage(im);
q.Clear(Color.White);
lines.ForEach(line =>
{
q.DrawLine(new Pen(Brushes.Black, 2), line.pStart, line.pEnd);
q.DrawString(line.length.ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, line.isVert ? line.pStart.X : line.pStart.X + ((line.pEnd.X - line.pStart.X) / 2) - 5, line.isVert ? line.pStart.Y + ((line.pEnd.Y - line.pStart.Y) / 2) - 5 : line.pStart.Y);
});
if (downed)
{
q.DrawLine(new Pen(Brushes.Black, 2), sPlace, mouse ?? sPlace);
if ((isVert)&&(mouse.HasValue))
{
q.DrawString(Math.Abs(sPlace.Y-mouse.Value.Y).ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, sPlace);
}
else if ((isVert==false) && (mouse.HasValue))
{
q.DrawString(Math.Abs(sPlace.X - mouse.Value.X).ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, sPlace);
}
}
dots.ForEach(dot =>
{
q.DrawEllipse(new Pen(Brushes.Black, 2),dot.place.X-1, dot.place.Y - 1, 2, 2);
if (dot.fuse)
{
q.DrawEllipse(new Pen(Brushes.Red, 2), dot.place.X - 2, dot.place.Y - 2, 4, 4);
}
});
g.DrawImage(im, 1, 1);
q.Dispose();
im.Dispose();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Point temp = e.Location;
if (downed)
{
if (!(Math.Abs(e.X - sPlace.X) > Math.Abs(e.Y - sPlace.Y)))
{
temp.X = sPlace.X;
isVert = true;
}
else
{
temp.Y = sPlace.Y;
isVert = false;
}
}
else
{
dots.ForEach(t => t.fuse = false);
List<Dot> tDots = dots.Where(dt => Math.Abs(e.Location.X - dt.place.X) < 20 && Math.Abs(e.Location.Y - dt.place.Y) < 20).ToList();
if (tDots.Any<Dot>())
{
double min = 0;
min = tDots.Min(t => distance(t.place, e.Location));
tDots.Where(t => distance(t.place, e.Location) == min).First().fuse = true;
}
}
drawing(temp);
}
private double distance(Point p1, Point p2)
{
double dist=0;
dist = (Math.Sqrt(Math.Abs(p1.X-p2.X)+Math.Abs(p1.Y-p2.Y)));
return dist;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void tabPage1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void tabPage2_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
int between = 0;
string curr = comboBox1.SelectedItem.ToString();
if (curr == "Short")
{
between = 1250;
}
else if (curr == "Medium")
{
between = 1875;
}
else
{
between = 3750;
}
double wut = (double.Parse(textBox1.Text) / between * 300 + double.Parse(textBox3.Text) + double.Parse(textBox2.Text) * 2);
string answer = Convert.ToString(Math.Ceiling(wut));
label4.Text = answer;
}
}
public class Line
{
public Point pEnd;
public Point pStart;
public bool isVert;
public int length;
public Line(Point pStart, Point pEnd, bool isVert,int length)
{
this.pStart = pStart;
this.pEnd = pEnd;
this.isVert = isVert;
this.length = length;
}
}
public class Dot
{
public bool fuse;
public Point place;
public Dot(Point place)
{
this.place = place;
}
}
}
I'm trying to learn C# by developing a small little breakout game. It is coming along fine but I am struggling with moving the ball up, down, left and right based on the user clicks. I don't even know where to start for it as my code is that messy. Here is my code:
public partial class Form2 : Form
{
Graphics drawingArea, paperBall, paperBat;
Pen penBall;
SolidBrush brushBAt = new SolidBrush(Color.Black);
private int x, y, xChange, yChange;
Random ranNum;
public Form2()
{
InitializeComponent();
drawingArea = playground.CreateGraphics();
paperBall = playground.CreateGraphics();
paperBat = playground.CreateGraphics();
lblLives.Text = "5";
lblScore.Text = "0";
ranNum = new Random();
drawBricks();
}
private void btnYes_Click(object sender, EventArgs e)
{
drawBricks();
drawBat();
drawBall();
}
public void drawBricks()
{
SolidBrush blackpen = new SolidBrush(Color.Black);
SolidBrush redpen = new SolidBrush(Color.Red);
SolidBrush yellowpen = new SolidBrush(Color.Yellow);
SolidBrush bluepen = new SolidBrush(Color.Blue);
SolidBrush greenpen = new SolidBrush(Color.Green);
drawingArea.FillRectangle(blackpen, 0, 0, 80, 25);
drawingArea.FillRectangle(redpen, 150, 0, 80, 25);
drawingArea.FillRectangle(yellowpen, 350, 0, 80, 25);
drawingArea.FillRectangle(bluepen, 550, 0, 80, 25);
drawingArea.FillRectangle(greenpen, 750, 0, 80, 25);
}
public void drawBat()
{
paperBat = playground.CreateGraphics();
playground.MouseMove += new
System.Windows.Forms.MouseEventHandler(picDraw_MouseHover);
//drawBall();
}
private void picDraw_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
//paperBat.Clear(Color.White);
}
private void picDraw_MouseHover(object sender, System.Windows.Forms.MouseEventArgs e)
{
paperBat.FillRectangle(brushBAt, e.X + -30, playground.Height - 15, 75, 10); //First one: high up it is Second one: how long it is Third one:
drawBricks();
}
public void drawBall()
{
penBall = new Pen(Color.Red);
penBall.Width = 10;
timer1.Interval = 50;
timer1.Enabled = true;
x = ranNum.Next(-10, playground.Height); //MAkes the ball spawn randomly
y = ranNum.Next(-10, playground.Width); //MAkes the ball spawn randomly
xChange = ranNum.Next(1, 20); yChange = ranNum.Next(1, 20); //Changes speed
//drawBricks();
//drawBat();
}
private void timer1_Tick(object sender, EventArgs e)
{
x = x + xChange;
y = y + yChange;
if (x >= playground.Width)
xChange = -xChange;
if (y >= playground.Height)
yChange = -yChange;
if (x <= 0)
xChange = -xChange;
if (y <= 0)
yChange = -yChange;
// paperBall.Clear(Color.White);
paperBat.Clear(Color.White);
paperBall.DrawEllipse(penBall, x, y, 10, 10); //Height and width of ball
drawBricks();
}
private void btnUp_Click(object sender, EventArgs e)
{
}
private void btnRight_Click(object sender, EventArgs e)
{
}
private void btnLeft_Click(object sender, EventArgs e)
{
}
private void btnDown_Click(object sender, EventArgs e)
{
}
}
}
Can anyone help me please?
You will need to add a KeyDown event handler to your picDraw, then code what each key does (changing the ball position).
Here's an example from Microsoft: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeypress(v=vs.110).aspx
You migth need to look up ascii key codes for specific keys if i remember correctly.
In the future consider using Unity as at the moment you are not using the correct technology for what you are trying to achieve.
first sorry for my English. I am programming application in windows forms. It is something like Packet Tracer. I have four buttons. When I click on them, they dynamicaly create pictureboxes with picture of Router or Switch,.... Each time I click on the button, new picture box(Switch or Router,...), is created. I can also move with this pictureboxes by mouse.
I need to create a button, which connects selected pictureboxes with line(Cable). This pictureboxes should be selected by click on them. It sholud be able to move with this objects(movable line).
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
PictureBox[] picturebox = new PictureBox[100];
public Form1()
{
InitializeComponent();
}
private void router_Click(object sender, EventArgs e)
{
++a;
picturebox[a] = new PictureBox();
picturebox[a].Name = "picturebox" + a;
picturebox[a].Location = new Point(0 + (a-1) *100,100);
picturebox[a].Size = new Size(70, 70);
picturebox[a].BorderStyle = BorderStyle.None;
picturebox[a].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[a]);
picturebox[a].Image = Image.FromFile(#"D:\\router.jpg");
picturebox[a].Refresh();
picturebox[a].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[a].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[a].MouseUp += new MouseEventHandler(picMouseUp);
}
private void Form1_Load(object sender, EventArgs e)
{
}
int x = 0;
int y = 0;
bool drag = false;
private void picMouseDown(object sender, MouseEventArgs e)
{
// Get original position of cursor on mousedown
x = e.X;
y = e.Y;
drag = true;
}
private void picMouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
PictureBox pb = (PictureBox)sender;
// Get new position of picture
pb.Top += e.Y - y;
pb.Left += e.X - x;
pb.BringToFront();
}
}
private void picMouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void switch1_Click(object sender, EventArgs e)
{
++b;
picturebox[b] = new PictureBox();
picturebox[b].Name = "picturebox" + b;
picturebox[b].Location = new Point(0 + (b - 1) * 100, 180);
picturebox[b].Size = new Size(70, 70);
picturebox[b].BorderStyle = BorderStyle.None;
picturebox[b].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[b]);
picturebox[b].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
picturebox[b].Refresh();
picturebox[b].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[b].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[b].MouseUp += new MouseEventHandler(picMouseUp);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pc_Click(object sender, EventArgs e)
{
++c;
picturebox[c] = new PictureBox();
picturebox[c].Name = "picturebox" + c;
picturebox[c].Location = new Point(0 + (c - 1) * 100, 260);
picturebox[c].Size = new Size(70, 70);
picturebox[c].BorderStyle = BorderStyle.None;
picturebox[c].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[c]);
picturebox[c].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\pc.jpg");
picturebox[c].Refresh();
picturebox[c].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[c].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[c].MouseUp += new MouseEventHandler(picMouseUp);
}
private void server_Click(object sender, EventArgs e)
{
++d;
picturebox[d] = new PictureBox();
picturebox[d].Name = "picturebox" + d;
picturebox[d].Location = new Point(0 + (d - 1) * 100, 340);
picturebox[d].Size = new Size(70, 70);
picturebox[d].BorderStyle = BorderStyle.None;
picturebox[d].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[d]);
picturebox[d].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\server.png");
picturebox[d].Refresh();
picturebox[d].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[d].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[d].MouseUp += new MouseEventHandler(picMouseUp);
}
}
}
THank you for your help.
You need to invalidate the parent when you add a picturebox or when you move a picturebox:
(picMouseMove and 4 times in the click handlers, it would be better to use 1 function)
Invalidate();
This is an example OnPaint, drawing lines between the pictureboxes as they are located in the Controls collection: (your picturebox array seems very weird, you always add controls at index 1, always overwriting the previous entry?! i'd suggest using a List if you need to keep a reference to them)
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var pictureBoxes = Controls.OfType<PictureBox>().ToArray();
if (pictureBoxes.Length > 1)
{
for (int i = 1; i < pictureBoxes.Length; i++)
{
DrawLineBetween(e.Graphics, pictureBoxes[i - 1], pictureBoxes[i]);
}
}
}
This function can be used to draw a line between 2 of your boxes:
private void DrawLineBetween(Graphics g, PictureBox from, PictureBox to)
{
g.DrawLine(Pens.Black,
new Point(from.Left + from.Width / 2, from.Top + from.Height / 2),
new Point(to.Left + to.Width / 2, to.Top + to.Height / 2));
}
----- full sample below -----
I refactored your full example, and added the code above to start you off with a working example:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<PictureBox> pictureboxes = new List<PictureBox>();
public Form1()
{
InitializeComponent();
}
private void AddPictureBox(string imagePath)
{
var pb = new PictureBox();
pb.Name = "picturebox" + pictureboxes.Count;
pb.Location = new Point(pictureboxes.Count * 100, 100);
pb.Size = new Size(70, 70);
pb.BorderStyle = BorderStyle.None;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(pb);
pb.Image = Image.FromFile(imagePath);
pb.Refresh();
pb.MouseDown += new MouseEventHandler(picMouseDown);
pb.MouseMove += new MouseEventHandler(picMouseMove);
pb.MouseUp += new MouseEventHandler(picMouseUp);
pictureboxes.Add(pb);
Invalidate();
}
private void router_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\\router.jpg");
}
private void Form1_Load(object sender, EventArgs e)
{
}
int x = 0;
int y = 0;
bool drag = false;
private void picMouseDown(object sender, MouseEventArgs e)
{
// Get original position of cursor on mousedown
x = e.X;
y = e.Y;
drag = true;
}
private void picMouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
PictureBox pb = (PictureBox)sender;
// Get new position of picture
pb.Top += e.Y - y;
pb.Left += e.X - x;
pb.BringToFront();
Invalidate();
}
}
private void picMouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void switch1_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pc_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\pc.jpg");
}
private void server_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\server.png");
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (pictureboxes.Count > 1)
{
var arr = pictureboxes.ToArray();
for (int i = 1; i < arr.Length; i++)
{
DrawLineBetween(e.Graphics, arr[i - 1], arr[i]);
}
}
}
private void DrawLineBetween(Graphics g, PictureBox from, PictureBox to)
{
g.DrawLine(Pens.Black,
new Point(from.Left + from.Width / 2, from.Top + from.Height / 2),
new Point(to.Left + to.Width / 2, to.Top + to.Height / 2));
}
}
}
This is my code but its not working not drawing points at all on pictureBox2.
public partial class Form1 : Form
{
private int counter;
private int pb1mouse_x;
private int pb1mouse_y;
private int pbsize_x;
private int pbsize_y;
public Form1()
{
InitializeComponent();
pbsize_x = pictureBox2.Width / pictureBox1.Width;
pbsize_y = pictureBox2.Height / pictureBox1.Height;
label4.Visible = false;
label5.Visible = false;
label6.Visible = false;
counter = 0;
pictureBox1.Load(#"d:\radar000075.png");
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label4.Visible = true;
label4.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label5.Visible = true;
label5.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
counter += 1;
label6.Visible = true;
label6.Text = counter.ToString();
pb1mouse_x = e.X;
pb1mouse_y = e.Y;
pb1mouse_x = pb1mouse_x * pbsize_x;
pb1mouse_y = pb1mouse_y * pbsize_y;
pictureBox2.Invalidate();
}
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Color.Red);
var g = e.Graphics;
g.Clear(pictureBox1.BackColor);
g.DrawEllipse(p, new Rectangle(pb1mouse_x, pb1mouse_y, 10, 10));
}
}
You should be able to simply multiply by factors, i.e. smallBox.Width/largeBox.Width and smallBox.Height/largeBox.Height. Multiply the coordinates for the larger box by those factors and it will give you coordinates for the smaller box.
Edit:
This is what my code looks like:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace PBoxes
{
public partial class Form1 : Form
{
private float xFactor, yFactor;
List<PointF> points = new List<PointF>();
public Form1()
{
InitializeComponent();
xFactor = (float)pictureBox2.Width / pictureBox1.Width;
yFactor = (float)pictureBox2.Height / pictureBox1.Height;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
points.Add(new PointF(e.X * xFactor, e.Y * yFactor));
pictureBox2.Invalidate();
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
foreach (PointF pt in points)
{
e.Graphics.FillEllipse(Brushes.Red, pt.X, pt.Y, 3f, 3f);
}
}
private void pictureBox_SizeChanged(object sender, EventArgs e)
{
xFactor = (float)pictureBox2.Width / pictureBox1.Width;
yFactor = (float)pictureBox2.Height / pictureBox1.Height;
}
}
}