2015-12-29 77 views
-3

編輯:所以我通過倒票猜測,人們有問題再現問題?這對我來說並不難,請複製我包含的代碼,閱讀原始的預期結果和實際結果,運行程序,點擊幾下鼠標並拖動。我可以每次重現這一點。這並不重要,因爲我解決了這個問題,但必須等待考慮它的答案。因此,如果進行此舉行的管理員可以刪除該帖子或將我的答案標記爲很棒的答案。我提供了指南中概述的所有內容,並提供了「最小代碼」的可能意見。它具有理想的和實際的結果,重現的步驟,並且我不知道如何在沒有進行屏幕錄製的情況下使其更加清晰。如果你懇求不同,你是如何要求澄清某些事情的,以便我能看到這個錯誤。c#if bool不工作

原創:

我在這裏有一個有趣的問題。我試圖在C#中做一個簡單的橡皮筋選擇,如果我不做選擇,除了一個奇怪的錯誤之外,我已經可以工作了。

預期結果:鼠標移動鼠標後,沒有移動鼠標和沒有選擇的MouseDown/MouseUp。

實際結果:MouseDown/MouseUp沒有移動鼠標,那麼當我鼠標移動鼠標時,選擇就像我移動鼠標一樣繪製。如果我點擊,選擇消失。然後,如果我再次單擊並鼠標移動鼠標,則不斷重新繪製新選擇。

我在表格中添加了一個標籤來檢查布爾狀態。當我這樣做時,一切都按預期工作。但是,一旦我註釋掉label1.Text系列,它不再按預期工作。我試着添加多個布爾來解決這個問題,但無濟於事。

那麼爲什麼布爾沒有被識別,除非我設置標籤文本?附:我是C#的新手,所以我歡迎任何批評。我把這個代碼http://csharphelper.com/blog/2014/08/use-a-rubber-band-box-to-let-the-user-select-an-area-in-a-picture-in-c/

這裏是我當前的代碼:

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 RubberBand 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private Bitmap m_Orig = null; 
     private int X0, X1, Y0, Y1; 
     private bool sArea, isDown, hasMoved = false; 
     private Bitmap sImg = null; 
     private Graphics sGraph = null; 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      m_Orig = new Bitmap(pictureBox1.Image); 
      this.KeyPreview = true; 
     } 

     private void Form1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if (e.KeyChar == 27) 
      { 
       if (!sArea) return; 
       sArea = false; 
       isDown = false; 
       hasMoved = false; 

       sImg = null; 
       sGraph = null; 
       pictureBox1.Image = m_Orig; 
       pictureBox1.Refresh(); 
      } 
     } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      sArea = true; 
      isDown = true; 
      //label1.Text = isDown.ToString(); 
      X0 = e.X; 
      Y0 = e.Y; 

      sImg = new Bitmap(m_Orig); 
      sGraph = Graphics.FromImage(sImg); 
      pictureBox1.Image = sImg; 
     } 

     private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (!sArea) return; 
      hasMoved = true; 
      //label1.Text = isDown.ToString(); 

      if (isDown) 
      { 
       X1 = e.X; 
       Y1 = e.Y; 

       sGraph.DrawImage(m_Orig, 0, 0); 

       using (Pen select_pen = new Pen(Color.Red)) 
       { 
        select_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; 
        Rectangle rect = MakeRectangle(X0, Y0, X1, Y1); 
        sGraph.DrawRectangle(select_pen, rect); 
       } 

       pictureBox1.Refresh(); 
      } 
     } 

     private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      if (!sArea) return; 
      if (!hasMoved) return; 

      sArea = false; 
      hasMoved = false; 
      isDown = false; 
      //label1.Text = isDown.ToString(); 
      sImg = null; 
      sGraph = null; 
      pictureBox1.Image = m_Orig; 
      pictureBox1.Refresh(); 

      Rectangle rect = MakeRectangle(X0, Y0, X1, Y1); 
      if ((rect.Width > 0) && (rect.Height > 0)) 
      { 
       MessageBox.Show(rect.ToString()); 
      } 
     } 

     private Rectangle MakeRectangle(int x0, int y0, int x1, int y1) 
     { 
      return new Rectangle(
       Math.Min(x0, x1), 
       Math.Min(y0, y1), 
       Math.Abs(x0 - x1), 
       Math.Abs(y0 - y1)); 
     } 
    } 
} 
+0

我第一次的猜測之後,你說,它設置一個看似無關的標籤時正常工作將是設置標籤導致表單被重新繪製或者其他東西。如果您只是將標籤文本設置爲字符串會發生什麼? – Cyral

+0

我真的很困惑什麼是錯誤的,運行我的機器上的代碼似乎功能與原始教程相同,無論標籤代碼是否存在。 – Cyral

+0

@Cyral我會盡快測試並通知你。我正在運行Windows 8.1並使用VS 2015社區版。 你什麼時候要我標籤爲字符串? – WesGann

回答

0

好了,更多的研究後,這似乎是一個已知的問題。我的問題謝謝,我已經解決了這個頁面:https://social.msdn.microsoft.com/Forums/vstudio/en-US/8e85eb78-77f2-485f-9bd3-3eaa44233a8a/mousedown-and-mousemove-events

這裏是更新和工作代碼:

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 RubberBand 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private Bitmap m_Orig = null; 
     private int X0, X1, Y0, Y1; 
     private bool sArea, hasMoved = false; 
     private Bitmap sImg = null; 
     private Graphics sGraph = null; 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      m_Orig = new Bitmap(pictureBox1.Image); 
      this.KeyPreview = true; 
     } 

     private void Form1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if (e.KeyChar == 27) 
      { 
       if (!sArea) return; 
       sArea = false; 
       hasMoved = false; 

       sImg = null; 
       sGraph = null; 
       pictureBox1.Image = m_Orig; 
       pictureBox1.Refresh(); 
      } 
     } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      sArea = true; 

      X0 = e.X; 
      Y0 = e.Y; 

      sImg = new Bitmap(m_Orig); 
      sGraph = Graphics.FromImage(sImg); 
      pictureBox1.Image = sImg; 
     } 

     private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       if (!sArea) return; 
       hasMoved = true; 

       X1 = e.X; 
       Y1 = e.Y; 

       sGraph.DrawImage(m_Orig, 0, 0); 

       using (Pen select_pen = new Pen(Color.Red)) 
       { 
        select_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; 
        Rectangle rect = MakeRectangle(X0, Y0, X1, Y1); 
        sGraph.DrawRectangle(select_pen, rect); 
       } 

       pictureBox1.Refresh(); 
      } 
     } 

     private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      if (!sArea) return; 
      if (!hasMoved) return; 

      sArea = false; 
      hasMoved = false; 

      sImg = null; 
      sGraph = null; 
      pictureBox1.Image = m_Orig; 
      pictureBox1.Refresh(); 

      Rectangle rect = MakeRectangle(X0, Y0, X1, Y1); 
      if ((rect.Width > 0) && (rect.Height > 0)) 
      { 
       MessageBox.Show(rect.ToString()); 
      } 
     } 

     private Rectangle MakeRectangle(int x0, int y0, int x1, int y1) 
     { 
      return new Rectangle(
       Math.Min(x0, x1), 
       Math.Min(y0, y1), 
       Math.Abs(x0 - x1), 
       Math.Abs(y0 - y1)); 
     } 
    } 
}