我制作了一个WFA,我为用户显示了一个子表单。
在这种形式中,有一个图片框,里面有位图图像,还有一些标签和一个按钮。
我做了一个小小的迷你项目来设置和尝试绘制一个工作正常的表单,我转移了代码并重新调整它(即更改var名称)并将鼠标事件和绘制应用到图片框而不是形成。
当我来绘制图片框时它不会让我,但是如果我改变了.Paint(...)应用于图片框到表格(这个)我可以在表格上绘制?
我在这里错过了什么/做错了吗?
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 Valo.CustomDraw
{
public delegate void PassValueHandler(Point [] pointAct);
public partial class bitmap_Square : Form
{
public event PassValueHandler PassValue;
public Point p1 = new Point(100, 100);
public Point p2;
Bitmap bmp;
Point[] actualPoints;
Graphics gr;
public bitmap_Square(Bitmap b)
{
InitializeComponent();
DoubleBuffered = true;
this.bmp = b;
pb_bitmapImage.Image = bmp;
pb_bitmapImage.SizeMode = PictureBoxSizeMode.StretchImage;
pb_bitmapImage.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
pb_bitmapImage.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
pb_bitmapImage.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
pb_bitmapImage.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
p1 = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
p2 = e.Location;
this.Invalidate();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
p2 = e.Location;
this.Invalidate();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (p1.X > 0 && p1.Y > 0 && p2.X > 0 && p2.Y > 0)
g.DrawRectangle(Pens.Blue, new Rectangle(p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y));
}
private void btn_rectApply_Click(object sender, EventArgs e)
{
if (PassValue != null)
{
actualPoints[0] = p1;
actualPoints[1] = p2;
PassValue(actualPoints);
}
this.Close();
}
}
}
谢谢(忽略结束括号的格式,以及它是如何复制的)
约旦
答案 0 :(得分:1)
除了拨打this.Invalidate();
而不是pb_bitmapImage.Invalidate()
外,您的代码没有问题。否则这很好。
this.Invalidate(true);
也应该有效,但这会引发所有孩子不必要的重画。所以只需使用pb_bitmapImage.Invalidate()
。
实际上当绘图框重新绘制时会绘制矩形,你需要耐心看看:)