拖 或
焦點 矩形是按住滑鼠左鍵,使用滑鼠指標會追蹤的矩形。這種技巧通常用來分隔使用者滑鼠指標輸入的回應中的選取範圍。圖形裝置介面 (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 網站]:
請注意此解決方案只適用於螢幕上的輸出。若要在圖形物件上繪製可反轉的線,您需要與 GDI 交互操作,或呼叫
Bitmap::LockBits() 並直接操作影像位元。