文章編號: 314945 - 上次校閱: 2006年12月11日 - 版次: 3.4

如何繪製拖放矩形或焦點矩形在 Visual C# 中

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

焦點 矩形是按住滑鼠左鍵,使用滑鼠指標會追蹤的矩形。這種技巧通常用來分隔使用者滑鼠指標輸入的回應中的選取範圍。圖形裝置介面 (GDI) 中中, 通常會使用光柵操作 (ROPs) 來實作這些矩形。不過,System.Drawing 方法根據 GDI + (至 GDI 後續),具有 ROPs 的支援。本文將告訴您,在.NET Framework 中實作焦點矩形的另一種方法。

「 GDI 中藉由使用 ROP 代碼通常繪製焦點矩形。在特別經常使用 ROP2 代碼 R2_XORPEN 和 R2_NOT。使用任一種這些 ROP2 代碼時您可以藉由在相同的位置再次繪製線條來清除前一行。這有時稱為獨占 OR (XOR) 效果。

範例程式碼

因為在 GDI + 和 System.Drawing 沒有 ROPs 另一種方法就會所以需要的繪製這些工具可還原的線條。比方說您可以使用平台叫用服務 (PInvoke) [GDI 交互操作。不過,用途僅 Managed 程式碼的方案是可透過靜態成員 ControlPaint::DrawReversibleFrame() 的使用。下列範例程式碼,所撰寫的 C# 和準備?貼上的表單類別,在預設 Microsoft Visual C# 應用程式中,示範這種方法:
Boolean bHaveMouse;
Point	ptOriginal = new Point();
Point	ptLast = new Point();

// Called when the left mouse button is pressed. 
public void MyMouseDown( Object sender, MouseEventArgs e )
{
	// Make a note that we "have the mouse".
	bHaveMouse = true;
	// Store the "starting point" for this rubber-band rectangle.
	ptOriginal.X = e.X;
	ptOriginal.Y = e.Y;
	// Special value lets us know that no previous
	// rectangle needs to be erased.
	ptLast.X = -1;
	ptLast.Y = -1;
}
// Convert and normalize the points and draw the reversible frame.
private void MyDrawReversibleRectangle( Point p1, Point p2 )
{
	Rectangle rc = new Rectangle();

	// Convert the points to screen coordinates.
	p1 = PointToScreen( p1 );
	p2 = PointToScreen( p2 );
	// Normalize the rectangle.
	if( p1.X < p2.X )
	{
		rc.X = p1.X;
		rc.Width = p2.X - p1.X;
	}
	else
	{
		rc.X = p2.X;
		rc.Width = p1.X - p2.X;
	}
	if( p1.Y < p2.Y )
	{
		rc.Y = p1.Y;
		rc.Height = p2.Y - p1.Y;
	}
	else
	{
		rc.Y = p2.Y;
		rc.Height = p1.Y - p2.Y;
	}
	// Draw the reversible frame.
	ControlPaint.DrawReversibleFrame( rc, 
					Color.Red, FrameStyle.Dashed );
}
// Called when the left mouse button is released.
public void MyMouseUp( Object sender, MouseEventArgs e )
{
	// Set internal flag to know we no longer "have the mouse".
	bHaveMouse = false;
	// If we have drawn previously, draw again in that spot
	// to remove the lines.
	if( ptLast.X != -1 )
	{
		Point ptCurrent = new Point( e.X, e.Y );
		MyDrawReversibleRectangle( ptOriginal, ptLast );
	}
	// Set flags to know that there is no "previous" line to reverse.
	ptLast.X = -1;
	ptLast.Y = -1;
	ptOriginal.X = -1;
	ptOriginal.Y = -1;
}
// Called when the mouse is moved.
public void MyMouseMove( Object sender, MouseEventArgs e )
{
	Point ptCurrent = new Point( e.X, e.Y );
	// If we "have the mouse", then we draw our lines.
	if( bHaveMouse )
	{
		// If we have drawn previously, draw again in
		// that spot to remove the lines.
		if( ptLast.X != -1 )
		{
			MyDrawReversibleRectangle( ptOriginal, ptLast );
		}
		// Update last point.
		ptLast = ptCurrent;
		// Draw new lines.
		MyDrawReversibleRectangle( ptOriginal, ptCurrent );
	}
}
// Set up delegates for mouse events.
protected override void OnLoad(System.EventArgs e)
{
	MouseDown += new MouseEventHandler( MyMouseDown );
	MouseUp += new MouseEventHandler( MyMouseUp );
	MouseMove += new MouseEventHandler( MyMouseMove );
	bHaveMouse = false;
}
				
筆記 的程式碼應該在 Visual Studio 2005 中進行變更。當您建立 Windows Form 專案時,Visual C# 將一表單加入專案預設。此表單名為 Form1。代表表單的兩個檔案被命名 Form1.cs 和 Form1.designer.cs。您可以撰寫程式碼中 Form1.cs。為.designer.cs 檔案是其中 Windows Form 設計工具寫入程式碼會實作所有動作您執行藉由拖放控制項從 [工具箱]。 如需有關 Windows Form 設計工具在 Visual C# 2005年中的詳細資訊,請造訪下列 Microsoft 網站]:
http://msdn2.microsoft.com/en-us/library/ms173077.aspx (http://msdn2.microsoft.com/en-us/library/ms173077.aspx)
請注意此解決方案只適用於螢幕上的輸出。若要在圖形物件上繪製可反轉的線,您需要與 GDI 交互操作,或呼叫 Bitmap::LockBits() 並直接操作影像位元。


這篇文章中的資訊適用於:
  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition
關鍵字:?
kbmt kbdswgdi2003swept kbgdi kbhowtomaster KB314945 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:314945? (http://support.microsoft.com/kb/314945/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。