在listBox的左侧我有一些项目。 我想这样做,当我点击一个项目绘制矩形点击按钮ConfirmRectangle它会记住,在这个项目中我绘制了一个矩形。 因此,如果我移动到一个我没有绘制矩形的项目,它将显示该项目,因为它没有任何绘制的矩形。但是,如果我在显示我绘制的矩形之前单击任何已经绘制矩形的项目,并且还启用了False按钮ConfirmRectangle。未启用矩形的项目为true,启用了绘制矩形的按钮项目为false。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace MinimizeCapture
{
public partial class Form1 : Form
{
Rectangle RectClone;
bool btn = false;
Point RectStartPoint = Point.Empty;
Point RectEndPoint = Point.Empty;
private Brush selectionBrush = new SolidBrush(Color.Red);
private Pen pen;
private string selectedIndex;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
btn = false;
pen = new Pen(selectionBrush);
}
private void buttonSnap_Click(object sender, EventArgs e)
{
this.listBoxSnap.Items.Clear();
this.pictureBoxSnap.Image = null;
backgroundWorker1.RunWorkerAsync();
}
private void listBoxSnap_SelectedIndexChanged(object sender, EventArgs e)
{
WindowSnap snap = this.listBoxSnap.SelectedItem as WindowSnap;
selectedIndex = this.listBoxSnap.SelectedIndex.ToString();
this.pictureBoxSnap.Image = snap.Image;
}
private void checkBoxForceMDI_CheckedChanged(object sender, EventArgs e)
{
WindowSnap.ForceMDICapturing = (sender as CheckBox).Checked;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
listBoxSnap.Invoke(new MethodInvoker(delegate { this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray()); }));
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
private void pictureBoxSnap_Paint(object sender, PaintEventArgs e)
{
if (pictureBoxSnap.Image != null)
{
{
Rectangle Rect = getRect(RectStartPoint, RectEndPoint);
RectClone = Rect;
if (Rect != Rectangle.Empty)
{
e.Graphics.DrawRectangle(Pens.Firebrick, Rect);
}
}
}
}
private void pictureBoxSnap_MouseMove(object sender, MouseEventArgs e)
{
if (btn == true)
{
RectEndPoint = e.Location;
pictureBoxSnap.Invalidate();
}
}
private void pictureBoxSnap_MouseDown(object sender, MouseEventArgs e)
{
RectStartPoint = e.Location;
btn = true;
}
private void pictureBoxSnap_MouseUp(object sender, MouseEventArgs e)
{
btn = false;
RectEndPoint = e.Location;
pictureBoxSnap.Invalidate();
}
Rectangle getRect(Point p1, Point p2)
{
Point p = new Point(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y));
Size s = new Size(Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
return new Rectangle(p, s);
}
private void ConfirmRectangle_Click(object sender, EventArgs e)
{
ConfirmRectangle.ForeColor = Color.Red;
ConfirmRectangle.Enabled = false;
StreamWriter w = new StreamWriter(@"c:\temp\Settings.txt", true);
w.WriteLine("Rectangle Location: " + RectClone.Location + " Rectangle Size: " + RectClone.Size + " Selected Index: " + selectedIndex);
textBoxIndex.Text = selectedIndex.ToString();
w.Close();
listBoxSnap.Invalidate();
}
}
}
答案 0 :(得分:0)
以下是一个可用于填充ListBox
的课程,而不是原来的WindowSnap
课程:
class SnapWrapper
{
public WindowSnap windowSnap { get; set; }
public Rectangle rect { get; set; }
public Bitmap bitmap { get; set; }
public SnapWrapper(WindowSnap windowSnap_) { windowSnap = windowSnap_; }
public override string ToString() { return windowSnap.ToString(); }
public bool IsSet() { return rect != Rectangle.Empty; }
}
您可以使用WindowSnap
..
SnapWrapper.windowSnap
引用
现在您可以在SelectedItem
中设置矩形,也可以在OKButton_Click
事件中设置:
if (listBox1.SelectedItem != null)
(listBox1.SelectedItem as SnapWrapper).rect = getRect(RectStartPoint, RectEndPoint);
使用上一个问题中的getRect函数;或者再次删除它:
if (listBox1.SelectedItem != null)
(listBox1.SelectedItem as SnapWrapper).rect = Rectangle.Empty;
并检查:
if (listBox1.SelectedItem != null)
if ((listBox1.SelectedItem as SnapWrapper).IsSet() ) //do stuff...
如何从图像中复制部分很容易用谷歌搜索; Graphics.DrawImage(image, destRect, srceRect, unit)
格式将完成这项工作..