共用方式為


墨水縮放範例

此範例程式示範如何縮放和卷動筆跡。 特別是,它可讓使用者以遞增的方式放大和縮小筆跡。 它也會示範如何使用縮放矩形放大特定區域。 最後,此範例說明如何在不同的縮放比例下收集書寫筆跡,並如何在放大後的繪圖區域中設置滾動功能。

在範例中,轉譯器 對象的檢視和物件轉換可用來執行縮放和捲動。 檢視轉換會套用至點和畫筆寬度。 物件轉換只適用於點。 用戶可以藉由變更 [模式] 選單中的 [縮放筆畫寬度] 項目來控制使用的轉換。

注意

當訊息已傳送時,在某些介面方法上執行某些 COM 呼叫會出現問題(例如,InkRenderer.SetViewTransformInkRenderer.SetObjectTransform)。 當訊息已經發送時,它們必須被封送至 POST 消息佇列。 為了處理此情境,請呼叫 InSendMesssageEx 來測試您是否正在處理來自 POST 的訊息,若訊息已被 SENT,則將該訊息發布給自己。

 

此範例會使用下列功能:

初始化表單

首先,此範例會參考 Windows Vista 或 Windows XP 平板電腦版本軟體開發工具包 (SDK) 中提供的平板電腦自動化介面。

using Microsoft.Ink;

此範例會宣告 InkCollectormyInkCollector,以及一些私人成員,以協助調整。

// Declare the Ink Collector object
private InkCollector myInkCollector = null;
...
// The starting and ending points of the zoom rectangle
private Rectangle zoomRectangle = Rectangle.Empty;

// The current zoom factor (1 = 100% zoom level)
private float zoomFactor = 1;

// Declare constants for the width and height of the 
// drawing area (in ink space coordinates).
private const int InkSpaceWidth = 50000;
private const int InkSpaceHeight = 50000;
...
// Declare constant for the pen width used by this application
private const float MediumInkWidth = 100;

然後範例會在表單的 Load 事件處理程式中建立並啟用 InkCollector。 此外,會設定 InkCollector 物件 DefaultDrawingAttributes 屬性的 Width 属性。 最後,會定義滾動條範圍,並呼叫應用程式的 UpdateZoomAndScroll 方法。

private void InkZoom_Load(object sender, System.EventArgs e)
{
   // Create the pen used to draw the zoom rectangle
    blackPen = new Pen(Color.Black, 1);

    // Create the ink collector and associate it with the form
    myInkCollector = new InkCollector(pnlDrawingArea.Handle);

    // Set the pen width
    myInkCollector.DefaultDrawingAttributes.Width = MediumInkWidth;

    // Enable ink collection
    myInkCollector.Enabled = true;

    // Define ink space size - note that the scroll bars
    // map directly to ink space
    hScrollBar.Minimum = 0;
    hScrollBar.Maximum = InkSpaceWidth;
    vScrollBar.Minimum = 0;
    vScrollBar.Maximum = InkSpaceHeight;

    // Set the scroll bars to map to the current zoom level
    UpdateZoomAndScroll();
}

更新縮放和捲動值

筆跡收集器的繪圖區域受到許多事件的影響。 在 UpdateZoomAndScroll 方法中,轉換矩陣會用於縮放和轉譯視窗內的筆跡收集器。

註記

Renderer 物件的 SetViewTransform 方法會將轉換套用至筆劃和畫筆寬度,而 SetObjectTransform 方法只會將轉換套用至筆劃。

 

最後,會呼叫應用程式的 UpdateScrollBars 方法,並強制重新整理表單。

// Create a transformation matrix
Matrix m = new Matrix();

// Apply the current scale factor
m.Scale(zoomFactor,zoomFactor);

// Apply the current translation factor - note that since 
// the scroll bars map directly to ink space, their values
// can be used directly.
m.Translate(-hScrollBar.Value, -vScrollBar.Value);

// ...
if (miScalePenWidth.Checked)
{
    myInkCollector.Renderer.SetViewTransform(m);
}
else
{
    myInkCollector.Renderer.SetObjectTransform(m);
}

// Set the scroll bars to map to the current zoom level
UpdateScrollBars();

Refresh();

管理滾動條

UpdateScrollBars 方法會在 InkCollector中設定滾動條,使其能夠正確配合目前視窗大小、縮放設定和捲動位置運作。 此方法會計算垂直和水準滾動條的大型變更和小型變更值。 它也會計算滾動條的目前值,以及是否應該顯示它們。 Renderer 物件的 PixelToInkSpace 方法負責處理從像素到縮放後座標空間的轉換,並考慮透過視圖和物件變換所套用的任何縮放和捲動。

// Create a point representing the top left of the drawing area (in pixels)
Point ptUpperLeft = new Point(0, 0);

// Create a point representing the size of a small change
Point ptSmallChange = new Point(SmallChangeSize, SmallChangeSize);

// Create a point representing the lower right of the drawing area (in pixels)
Point ptLowerRight = new Point(hScrollBar.Width, vScrollBar.Height);

using (Graphics g = CreateGraphics())
{
    // Convert each of the points to ink space
    myInkCollector.Renderer.PixelToInkSpace(g, ref ptUpperLeft);
    myInkCollector.Renderer.PixelToInkSpace(g, ref ptLowerRight);
    myInkCollector.Renderer.PixelToInkSpace(g, ref ptSmallChange);
}

// Set the SmallChange values (in ink space)
// Note that it is necessary to subract the upper-left point
// value to account for scrolling.
hScrollBar.SmallChange = ptSmallChange.X - ptUpperLeft.X;
vScrollBar.SmallChange = ptSmallChange.Y - ptUpperLeft.Y;

// Set the LargeChange values to the drawing area width (in ink space)
// Note that it is necessary to subract the upper-left point
// value to account for scrolling.
hScrollBar.LargeChange = ptLowerRight.X - ptUpperLeft.X;
vScrollBar.LargeChange = ptLowerRight.Y - ptUpperLeft.Y;

// If the scroll bars are not needed, hide them
hScrollBar.Visible = hScrollBar.LargeChange < hScrollBar.Maximum;
vScrollBar.Visible = vScrollBar.LargeChange < vScrollBar.Maximum;

// If the horizontal scroll bar value would run off of the drawing area, 
// adjust it
if(hScrollBar.Visible && (hScrollBar.Value + hScrollBar.LargeChange > hScrollBar.Maximum)) 
{
    hScrollBar.Value = hScrollBar.Maximum - hScrollBar.LargeChange;
}

// If the vertical scroll bar value would run off of the drawing area, 
// adjust it
if(vScrollBar.Visible && (vScrollBar.Value + vScrollBar.LargeChange > vScrollBar.Maximum))
{
    vScrollBar.Value = vScrollBar.Maximum - vScrollBar.LargeChange;
}

縮放至矩形

pnlDrawingArea 面板事件處理程式負責將矩形繪製到視窗中。 如果在 [模式] 功能表上核取 Zoom To Rect 命令,則 MouseUp 事件處理程式會呼叫應用程式的 ZoomToRectangle 方法。 ZoomToRectangle 方法會計算矩形的寬度和高度、檢查界限條件、更新滾動條值和縮放比例,然後呼叫應用程式的 UpdateZoomAndScroll 方法來套用新的設定。

關閉表單

表單的 Dispose 方法會釋放 InkCollector 物件。