我有一個鼠標按下事件,我寫文字的RichTextBox:如何向richTextBox添加新行?
private void pictureBoxSnap_MouseDown(object sender, MouseEventArgs e)
{
richTextBox1.Text += "Rectangle Location: " + e.Location + Environment.NewLine;
Bitmap bmp;
Point pnt;
if (e.Button == MouseButtons.Right)
{
cm.Show(pictureBoxSnap, e.Location);
return;
}
if (e.Button == MouseButtons.Left)
{
if (checkError(listBox1.SelectedIndex, "pictureBoxSnap_MouseDown") == true) { return; } //if listBox1 empty
if (isCroppedList[listBox1.SelectedIndex] == true) { return; }
mouseDown = e.Location;
if (canDrawNormallyList[listBox1.SelectedIndex] == false)
{
bmp = new Bitmap(pictureBoxSnap.Width - 4, pictureBoxSnap.Height - 4);
pnt = PointToScreen(pictureBoxSnap.Location);
g = Graphics.FromImage(bmp);
g.CopyFromScreen(pnt.X + 2, pnt.Y + 2, 0, 0, new Size(bmp.Width, bmp.Height));
g.Dispose();
windowsBitmaps[listBox1.SelectedIndex] = bmp;
}
else
{
if (thumb != IntPtr.Zero)
{
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBoxIcon icon = MessageBoxIcon.Error;
MessageBox.Show("ERROR in pictureBoxSnap_MouseDown: thumb != IntPtr.Zero", "ERROR", buttons, icon);
this.Close();
}
}
}
}
在鼠標按下事件我寫了矩形的位置。
然後在DrawRectangle的方法,我想在實時richTextBox1顯示矩形的大小:
private void DrawRectangle(Point pnt)
{
g = Graphics.FromImage(img);
g.Clear(Color.FromKnownColor(KnownColor.Control));
if (pnt.X == mouseDown.X || pnt.Y == mouseDown.Y)
{
g.DrawLine(Pens.Firebrick, mouseDown.X, mouseDown.Y, pnt.X, pnt.Y);
}
else
{
g.DrawRectangle(Pens.Firebrick, Math.Min(mouseDown.X, pnt.X), Math.Min(mouseDown.Y, pnt.Y),
Math.Abs(mouseDown.X - pnt.X), Math.Abs(mouseDown.Y - pnt.Y));
richTextBox1.Text = "Rectangle Size: " + Math.Abs(mouseDown.X - pnt.X) +
Math.Abs(mouseDown.Y - pnt.Y);
}
g.Dispose();
g = frm.CreateGraphics();
g.DrawImage(img, 0, 0, img.Width, img.Height);
g.Dispose();
}
現在的問題是,當我讓鼠標按鈕按下鼠標左鍵單擊我看到位置,但隨後當我移動鼠標左右拖動它時,位置線被刪除,而我看到的是尺寸線。如果在DrawRectangle方法中,我也使+ =它只是添加尺寸線,很多時候填充整個richTextBox,當我移動/拖動鼠標aorund。
richTextBox1.Text + =「Rectangle Location:」+ e.Location + Environment.NewLine; 使用這種語法 – Partha 2014-09-02 23:07:30