今回は表示サイズを変更できるようにします。長くなるので2回にわけます。
まずはメニューをつくります。
次に表示サイズを設定するためのプロパティをつくります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public partial class UserControlImage : UserControl { float displayMagnification = 1.0F; public float DisplayMagnification { get { return displayMagnification; } set { displayMagnification = value; pictureBox1.Invalidate(); } } } |
メニューを選択したらUserControlImage.DisplayMagnificationメソッドを変更できるようにします。またどれが選択されているかわかるようにチェックマークをつけます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
public partial class Form1 : Form { private void Show100MenuItem_Click(object sender, EventArgs e) { userControlImage1.DisplayMagnification = 1.0F; } private void Show75MenuItem_Click(object sender, EventArgs e) { userControlImage1.DisplayMagnification = 0.75F; } private void Show50MenuItem_Click(object sender, EventArgs e) { userControlImage1.DisplayMagnification = 0.5F; } private void Show150MenuItem_Click(object sender, EventArgs e) { userControlImage1.DisplayMagnification = 1.5F; } private void Show300MenuItem_Click(object sender, EventArgs e) { userControlImage1.DisplayMagnification = 3.0F; } private void Show200MenuItem_Click(object sender, EventArgs e) { userControlImage1.DisplayMagnification = 2.0F; } private void DisplayMagnificationMenuItem_DropDownOpening(object sender, EventArgs e) { Show100MenuItem.Checked = false; Show75MenuItem.Checked = false; Show50MenuItem.Checked = false; Show150MenuItem.Checked = false; Show200MenuItem.Checked = false; Show300MenuItem.Checked = false; float f = userControlImage1.DisplayMagnification; if(f == 1.0F) Show100MenuItem.Checked = true; if(f == 0.75F) Show75MenuItem.Checked = true; if(f == 0.5F) Show50MenuItem.Checked = true; if(f == 1.5F) Show150MenuItem.Checked = true; if(f == 2.0F) Show200MenuItem.Checked = true; if(f == 3.0F) Show300MenuItem.Checked = true; } } |
表示倍率が変更された場合、スクロールバーを変更する必要があります。そこでOptimizationScrloolBarメソッドを変更します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public partial class UserControlImage : UserControl { void OptimizationScrloolBar(Bitmap bitmap) { // 横スクロールバーの初期位置、最小値、最大値を設定 hScrollBar1.Minimum = 0; int pictureBoxWidth = (int)(pictureBox1.Width / DisplayMagnification); if(bitmap.Width - pictureBoxWidth >= 0) { int largeChange = pictureBox1.Width * pictureBoxWidth / bitmap.Width; hScrollBar1.LargeChange = largeChange; hScrollBar1.Maximum = bitmap.Width - pictureBoxWidth + largeChange; } else hScrollBar1.Maximum = 0; // 縦スクロールバーと横スクロールバーの初期位置、最小値、最大値を設定する vScrollBar1.Minimum = 0; int pictureBoxHeight = (int)(pictureBox1.Height / DisplayMagnification); if(bitmap.Height - pictureBoxHeight >= 0) { int largeChange = pictureBox1.Height * pictureBoxHeight / bitmap.Height; vScrollBar1.LargeChange = largeChange; vScrollBar1.Maximum = bitmap.Height - pictureBoxHeight + largeChange; } else vScrollBar1.Maximum = 0; } } |
表示倍率が変更された場合、ピクチャーボックスの上に描画する画像もそれにあわせて変更しなければなりません。そこでGetBitmapDrawPictureBoxメソッドも変更します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public partial class UserControlImage : UserControl { Bitmap GetBitmapDrawPictureBox(Bitmap bitmap) { Bitmap newBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height); RectangleF rectSrc = new RectangleF( hScrollBar1.Value, vScrollBar1.Value, pictureBox1.Width / DisplayMagnification, pictureBox1.Height / DisplayMagnification); Graphics g = Graphics.FromImage(newBitmap); Rectangle destRect = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height); g.DrawImage(bitmap, destRect, rectSrc, GraphicsUnit.Pixel); g.Dispose(); return newBitmap; } } |
表示倍率が変更されているので自作メソッドのOn~にわたす引数を変更する必要があります。わたされるのは元の大きさのビットマップ上における座標です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
public partial class UserControlImage : UserControl { private void PictureBox1_MouseDown(object sender, MouseEventArgs e) { if(Bitmap == null) return; int x = (int)(e.X / DisplayMagnification); int y = (int)(e.Y / DisplayMagnification); // 範囲外におけるクリック if(x + ScrollBarPosX >= Bitmap.Width || y + ScrollBarPosY >= Bitmap.Height) { OnMouseDownOutOfBitmap(); Console.WriteLine("OnMouseDownOutOfBitmap()"); return; } // クリックされた場所の色を通知する if(PictureBoxMouseDown != null) { Color color = Bitmap.GetPixel(x + ScrollBarPosX, y + ScrollBarPosY); PictureBoxMouseDown(this, new PictureBox1MouseDownArgs(x + ScrollBarPosX, y + ScrollBarPosY, color)); } if(BitmapRectangle == null) { if(EditMode == EditMode.Selection) OnMouseDownForSelectRectangle(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(EditMode == EditMode.DrawRectangle || EditMode == EditMode.FillRectangle || EditMode == EditMode.DrawEllipse || EditMode == EditMode.FillEllipse) OnMouseDownForDrawRectangle(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(EditMode == EditMode.DrawLine) OnMouseDownForDrawLine(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(EditMode == EditMode.FreeLine) OnMouseDownForDrawFreeLine(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(EditMode == EditMode.SelectionFree) OnMouseDownForSelectFreeArea(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); } else { if(IsPointInRectangle(new Point(x + ScrollBarPosX, y + ScrollBarPosY), BitmapRectangle.Rectangle)) OnMouseDownInBitmapRectangle(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); else OnMouseDownOutOfSelection(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); } } private void PictureBox1_MouseMove(object sender, MouseEventArgs e) { if(Bitmap == null) return; int x = (int)(e.X / DisplayMagnification); int y = (int)(e.Y / DisplayMagnification); if(isMouseDown) { if(EditMode == EditMode.Selection) OnMouseMoveForSelectRectangle(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(EditMode == EditMode.DrawRectangle || EditMode == EditMode.FillRectangle || EditMode == EditMode.DrawEllipse || EditMode == EditMode.FillEllipse) OnMouseMoveForDrawRectangle(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(EditMode == EditMode.DrawLine) OnMouseMoveForDrawLine(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(EditMode == EditMode.FreeLine) OnMouseMoveForDrawFreeLine(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(EditMode == EditMode.SelectionFree) OnMouseMoveForSelectFreeArea(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); } else ChangeCursorWhenMouseMoveOnSelection(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(isMouseDownForMove) { if(EditMode == EditMode.SelectionFree) OnMouseMoveForMoveFreeLineSelection(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); else if(EditMode == EditMode.Selection) OnMouseMoveForMoveSelection(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); else if(EditMode == EditMode.DrawRectangle || EditMode == EditMode.FillRectangle || EditMode == EditMode.DrawEllipse || EditMode == EditMode.FillEllipse) OnMouseMoveForMoveSelection(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); } if(isMouseDownForSizeChange) OnMouseMoveForSizeChangeSelection(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(isMouseDownForFieldSizeChange) OnMouseMoveForSizeChangeField(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); } private void PictureBox1_MouseUp(object sender, MouseEventArgs e) { if(Bitmap == null) return; int x = (int)(e.X / DisplayMagnification); int y = (int)(e.Y / DisplayMagnification); if(isMouseDown) { if(EditMode == EditMode.Selection) OnMouseUpForSelectRectangle(); if(EditMode == EditMode.DrawRectangle || EditMode == EditMode.FillRectangle || EditMode == EditMode.DrawEllipse || EditMode == EditMode.FillEllipse) OnMouseUpForDrawRectangle(); if(EditMode == EditMode.DrawLine) OnMouseUpForDrawLine(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(EditMode == EditMode.FreeLine) OnMouseUpForDrawFreeLine(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(EditMode == EditMode.SelectionFree) OnMouseUpForSelectFreeArea(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); return; } if(isMouseDownForMove) { if(EditMode == EditMode.SelectionFree) OnMouseUpForMoveFreeLineSelection(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); else OnMouseUpForMoveSelection(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); } if(isMouseDownForSizeChange) OnMouseUpForSizeChangeSelection(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); if(isMouseDownForFieldSizeChange) OnMouseUpForSizeChangeField(new Point(x + ScrollBarPosX, y + ScrollBarPosY)); return; } } |
Paintイベント関連で修正が必要な箇所が多々あります。後半に続きます。