C#单击窗体A按钮在窗体B上画一条直线

2025-01-07 05:01:45
推荐回答(2个)
回答1:

Form1代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int startx = int.Parse(StartX.Text.Trim());
int starty = int.Parse(StartY.Text.Trim());
int endx = int.Parse(EndX.Text.Trim());
int endy = int.Parse(EndY.Text.Trim());
Form2 f2 = new Form2();
f2.Get(startx, starty, endx, endy);
f2.Show();
}

}
}
Form2代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
int sx; int sy; int ex; int ey;
public void Get(int startx, int starty, int endx, int endy)
{//获取FORM1的数据,
sx = startx;
sy = starty;
ex = endx;
ey = endy;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{// 鼠标放在PictureBox上就显示图画
Graphics g;
Pen p = new Pen(Color.Blue, 2);
g = pictureBox1.CreateGraphics();
g.DrawLine(p, sx, sy, ex, ey);
}
}
}

回答2:

不太明白