让我解释一下我的问题:
我有一个图片框,它应该用来绘制列表框的几个元素。我给了画盒一块画布。通过左击和右击,你可以创建一个矩形,它应该预览你想要在图片框的画布上绘制的元素。
现在我的问题是:
这个矩形应该只是一个预览,每次我点击图片框的另一个位置并创建一个新的矩形时,它都应该被擦除。当我点击输入按钮时,它应该只是绘制在画布上。
那么我怎么能预览选定的元素在矩形由左击和右击?
我希望我的问题足够清楚:/
这是我目前所做的:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Interaktive_systeme_sta
{
public partial class Bildeditor : Form
{
void drawRect()
{
pbxBild.Refresh();
using (Graphics g = this.pbxBild.CreateGraphics())
{
Pen blackPen = new Pen(Color.Black, 1);
blackPen.DashPattern = new float[] {2,2};
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawRectangle(blackPen, X, Y, B, H);
g.Dispose();
}
}
public Bildeditor()
{
InitializeComponent();
this.MinimumSize = new Size(630,420);
this.MaximumSize = new Size(630, 420);
int canvasWidth = pbxBild.Width;
int canvasHeight = pbxBild.Height;
nupBreite.Maximum = canvasWidth;
nupHoehe.Maximum = canvasHeight;
nupBreite.Minimum = 0;
nupHoehe.Minimum = 0;
nupBreite.Value = canvasWidth;
nupHoehe.Value = canvasHeight;
nupX.Maximum = canvasWidth;
nupY.Maximum = canvasHeight;
nupX.Minimum = 0;
nupY.Minimum = 0;
nupX.Value = 0;
nupY.Value = 0;
drawRect();
}
private void btnZuruecksetzten_Click(object sender, EventArgs e)
{
int canvasWidth = pbxBild.Width;
int canvasHeight = pbxBild.Height;
nupX.Value = 0;
nupY.Value = 0;
nupBreite.Value = canvasWidth-1;
nupHoehe.Value = canvasHeight-1;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void pbxBild_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point mouseDownLocation = new Point(e.X, e.Y);
switch (e.Button)
{
case MouseButtons.Left:
if (e.X < 0)
{
nupX.Value = 0;
}
else
{
nupX.Value = e.X;
}
if (e.Y < 0)
{
nupY.Value = 0;
}
else
{
nupY.Value = e.Y;
}
break;
case MouseButtons.Right:
if (e.X - nupX.Value < 1)
{
nupBreite.Value = 1;
}
else
{
nupBreite.Value = e.X - nupX.Value;
}
if (e.Y - nupY.Value < 1)
{
nupHoehe.Value = 1;
}
else
{
nupHoehe.Value = e.Y - nupY.Value;
}
break;
}
drawRect();
}
private void nupY_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void nupBreite_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void nupHoehe_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void btnEinfuegen_Click(object sender, EventArgs e)
{
if (lbBildelemente.SelectedIndex == 0)
{
zeichneBild();
}
else if (lbBildelemente.SelectedIndex == 1)
{
zeichneLine();
}
else if (lbBildelemente.SelectedIndex == 2)
{
zeichneRect();
}
else if (lbBildelemente.SelectedIndex == 3)
{
zeichneText();
}
else if (lbBildelemente.SelectedIndex == 4)
{
zeichneKreis();
}
}
private void lbBildelemente_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbBildelemente.SelectedIndex == 0)
{
tbFarbe.Enabled = false;
btnFarbe.Enabled = false;
nupPen.Enabled = false;
tbBild.Enabled = true;
btnBild.Enabled = true;
tbText.Enabled = false;
tbSchrift.Enabled = false;
btnSchrift.Enabled = false;
}
else if (lbBildelemente.SelectedIndex == 1 || lbBildelemente.SelectedIndex == 2 || lbBildelemente.SelectedIndex == 4)
{
tbFarbe.Enabled = true;
btnFarbe.Enabled = true;
nupPen.Enabled = true;
tbBild.Enabled = false;
btnBild.Enabled = false;
tbText.Enabled = false;
tbSchrift.Enabled = false;
btnSchrift.Enabled = false;
}
else if (lbBildelemente.SelectedIndex == 3)
{
tbFarbe.Enabled = true;
btnFarbe.Enabled = true;
nupPen.Enabled = true;
tbBild.Enabled = false;
btnBild.Enabled = false;
tbText.Enabled = true;
tbSchrift.Enabled = true;
btnSchrift.Enabled = true;
}
}
void zeichneBild()
{
Graphics g = pbxBild.CreateGraphics();
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
Bitmap bitmap = new Bitmap(tbBild.Text);
g.DrawImage(bitmap, X, Y, B, H);
}
// Als zusatzfunktion kann man die Pinseldicke auswählen
void zeichneRect()
{
Graphics g = pbxBild.CreateGraphics();
Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawRectangle(Pen, X, Y, B, H);
}
//zusatzfunktion
void zeichneKreis()
{
Graphics g = pbxBild.CreateGraphics();
Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawEllipse(Pen, X, Y, B, H);
}
void zeichneLine()
{
Graphics g = pbxBild.CreateGraphics();
Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawLine(Pen, X, Y, X+B, Y+H);
}
void zeichneText()
{
Graphics g = pbxBild.CreateGraphics();
Brush brush = new SolidBrush(tbFarbe.BackColor);
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
g.DrawString(tbText.Text, fontDialog1.Font, brush, X, Y);
}
private void btnFarbe_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
tbFarbe.BackColor = colorDialog1.Color;
}
}
private void btnSchrift_Click(object sender, EventArgs e)
{
// Show the dialog.
DialogResult result = fontDialog1.ShowDialog();
// See if OK was pressed.
if (result == DialogResult.OK)
{
// Get Font.
Font font = fontDialog1.Font;
// Set TextBox properties.
this.tbSchrift.Text = string.Format(font.Name +"; "+ font.Size +"pt");
}
}
private void btnBild_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Joint Photographic Experts Group (*.jpg)|*.jpg|"
+ "EMF Enhanced Metafile Format (*.emf)|*.emf|"
+ "Graphics Interchange Format (*.gif)|*.gif|"
+ "Bitmap (*.bmp)|*.bmp|"
+ "W3C Portable Network Graphics (*.png)|*.png";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
tbBild.Text = openFileDialog.FileName;
}
}
private void bildbereichLöschenToolStripMenuItem_Click(object sender, EventArgs e)
{
pbxBild.Refresh();
}
private void bildLadenToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Joint Photographic Experts Group (*.jpg)|*.jpg|"
+ "EMF Enhanced Metafile Format (*.emf)|*.emf|"
+ "Graphics Interchange Format (*.gif)|*.gif|"
+ "Bitmap (*.bmp)|*.bmp|"
+ "W3C Portable Network Graphics (*.png)|*.png";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
Graphics g = pbxBild.CreateGraphics();
int X = 0;
int Y = 0;
int B = Convert.ToInt32(pbxBild.Width);
int H = Convert.ToInt32(pbxBild.Height);
Bitmap bitmap = new Bitmap(openFileDialog.FileName);
g.DrawImage(bitmap, X, Y, B, H);
}
}
}
}
我想这就是你需要的
private void Form1_Load(object sender, EventArgs e)
{
listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem == "Circle")
{
pictureBox1.Image = circle;
}
}