下記サイト様のコードを参考にpictureBoxをD&Dで縦横比を固定したまま拡大・縮小できるクラスを考えておりますが、うまくいきません。
(https://anis774.net/codevault/danddsizechanger.html)
縦の変化量を常に横の4/3倍としているように書き換えたのですが希望する動きになっていない状態です。
問題点をご指摘頂けると助かります。
宜しくお願いします。
C#
1uclass DAndDSizeChanger 2 { 3 Control mouseListner; 4 Control sizeChangeCtrl; 5 DAndDArea sizeChangeArea; 6 Size lastMouseDownSize; 7 Point lastMouseDownPoint; 8 DAndDArea status; 9 int sizeChangeAreaWidth; 10 Cursor defaultCursor; 11 12 const double fixedRate = (double)4 / 3; 13 14 /// <param name="mouseListner">マウス入力を受け取るコントロール</param> 15 /// <param name="sizeChangeCtrl">マウス入力によってサイズが変更されるコントロール</param> 16 /// <param name="sizeChangeArea">上下左右のサイズ変更が有効になる範囲を指定</param> 17 /// <param name="sizeChangeAreaWidth">サイズ変更が有効になる範囲の幅を指定</param> 18 public DAndDSizeChanger(Control mouseListner, Control sizeChangeCtrl, DAndDArea sizeChangeArea, int sizeChangeAreaWidth) 19 { 20 this.mouseListner = mouseListner; 21 this.sizeChangeCtrl = sizeChangeCtrl; 22 this.sizeChangeAreaWidth = sizeChangeAreaWidth; 23 this.sizeChangeArea = sizeChangeArea; 24 defaultCursor = mouseListner.Cursor; 25 26 mouseListner.MouseDown += new MouseEventHandler(mouseListner_MouseDown); 27 mouseListner.MouseMove += new MouseEventHandler(mouseListner_MouseMove); 28 mouseListner.MouseUp += new MouseEventHandler(mouseListner_MouseUp); 29 } 30 31 void mouseListner_MouseDown(object sender, MouseEventArgs e) 32 { 33 lastMouseDownPoint = e.Location; 34 lastMouseDownSize = sizeChangeCtrl.Size; 35 36 //動作を決定 37 status = DAndDArea.None; 38 if (getTop().Contains(e.Location)) 39 { 40 status |= DAndDArea.Top; 41 } 42 if (getLeft().Contains(e.Location)) 43 { 44 status |= DAndDArea.Left; 45 } 46 if (getBottom().Contains(e.Location)) 47 { 48 status |= DAndDArea.Bottom; 49 } 50 if (getRight().Contains(e.Location)) 51 { 52 status |= DAndDArea.Right; 53 } 54 55 if (status != DAndDArea.None) 56 { 57 mouseListner.Capture = true; 58 } 59 } 60 61 void mouseListner_MouseMove(object sender, MouseEventArgs e) 62 { 63 //カーソルを変更 64 if ((getTop().Contains(e.Location) && 65 getLeft().Contains(e.Location)) || 66 (getBottom().Contains(e.Location) && 67 getRight().Contains(e.Location))) 68 { 69 70 mouseListner.Cursor = Cursors.SizeNWSE; 71 } 72 else if ((getTop().Contains(e.Location) && 73 getRight().Contains(e.Location)) || 74 (getBottom().Contains(e.Location) && 75 getLeft().Contains(e.Location))) 76 { 77 mouseListner.Cursor = Cursors.SizeNESW; 78 } 79 80 else 81 { 82 mouseListner.Cursor = defaultCursor; 83 } 84 85 86 87 if (e.Button == MouseButtons.Left) 88 { 89 int diffX = e.X - lastMouseDownPoint.X; 90 int diffY = e.Y - lastMouseDownPoint.Y; 91 92 93 94 if ((status & DAndDArea.Left) == DAndDArea.Left && (status & DAndDArea.Top) == DAndDArea.Top)//左上 95 { 96 97 98 int w = sizeChangeCtrl.Width; 99 sizeChangeCtrl.Width -= diffX; 100 sizeChangeCtrl.Left += w - sizeChangeCtrl.Width; 101 102 int h = sizeChangeCtrl.Height; 103 sizeChangeCtrl.Height -= diffY; 104 //sizeChangeCtrl.Top += (int)(fixedRate * (h - sizeChangeCtrl.Height) + 0.5); 105 sizeChangeCtrl.Top += h - sizeChangeCtrl.Height; 106 } 107 if ((status & DAndDArea.Right) == DAndDArea.Right && (status & DAndDArea.Top) == DAndDArea.Top)//右上サイド 108 { 109 110 111 sizeChangeCtrl.Width = lastMouseDownSize.Width + diffX; 112 113 int h = sizeChangeCtrl.Height; 114 sizeChangeCtrl.Height -= diffY; 115 //sizeChangeCtrl.Top += (int)(fixedRate * (h - sizeChangeCtrl.Height) + 0.5); 116 sizeChangeCtrl.Top += h - sizeChangeCtrl.Height; 117 } 118 119 120 if ((status & DAndDArea.Left) == DAndDArea.Left && (status & DAndDArea.Bottom) == DAndDArea.Bottom)//左下 121 { 122 123 int w = sizeChangeCtrl.Width; 124 sizeChangeCtrl.Width -= diffX; 125 sizeChangeCtrl.Left += w - sizeChangeCtrl.Width; 126 127 //sizeChangeCtrl.Height = (int)(fixedRate * (lastMouseDownSize.Height + diffY) + 0.5); 128 sizeChangeCtrl.Height = lastMouseDownSize.Height + diffY; 129 } 130 if ((status & DAndDArea.Right) == DAndDArea.Right && (status & DAndDArea.Bottom) == DAndDArea.Bottom) //右下 131 { 132 133 134 sizeChangeCtrl.Width = lastMouseDownSize.Width + diffX; 135 //sizeChangeCtrl.Height = (int)(fixedRate * (lastMouseDownSize.Height + diffY) + 0.5); 136 sizeChangeCtrl.Height = lastMouseDownSize.Height + diffY; 137 } 138 139 140 } 141 } 142 143 void mouseListner_MouseUp(object sender, MouseEventArgs e) 144 { 145 mouseListner.Capture = false; 146 } 147 148 /// <summary> 149 /// ポイントがD&Dするとサイズが変更されるエリア内にあるかどうかを判定します。 150 /// </summary> 151 public bool ContainsSizeChangeArea(Point p) 152 { 153 return getTop().Contains(p) || 154 getBottom().Contains(p) || 155 getLeft().Contains(p) || 156 getRight().Contains(p); 157 } 158 159 private Rectangle getTop() 160 { 161 if ((sizeChangeArea & DAndDArea.Top) == DAndDArea.Top) 162 { 163 return new Rectangle(0, 0, mouseListner.Width, sizeChangeAreaWidth); 164 } 165 else 166 { 167 return new Rectangle(); 168 } 169 } 170 171 private Rectangle getBottom() 172 { 173 if ((sizeChangeArea & DAndDArea.Bottom) == DAndDArea.Bottom) 174 { 175 return new Rectangle(0, mouseListner.Height - sizeChangeAreaWidth, 176 mouseListner.Width, sizeChangeAreaWidth); 177 } 178 else 179 { 180 return new Rectangle(); 181 } 182 } 183 184 private Rectangle getLeft() 185 { 186 if ((sizeChangeArea & DAndDArea.Left) == DAndDArea.Left) 187 { 188 return new Rectangle(0, 0, 189 sizeChangeAreaWidth, mouseListner.Height); 190 } 191 else 192 { 193 return new Rectangle(); 194 } 195 } 196 197 private Rectangle getRight() 198 { 199 if ((sizeChangeArea & DAndDArea.Right) == DAndDArea.Right) 200 { 201 return new Rectangle(mouseListner.Width - sizeChangeAreaWidth, 0, 202 sizeChangeAreaWidth, mouseListner.Height); 203 } 204 else 205 { 206 return new Rectangle(); 207 } 208 } 209 } 210 211 public enum DAndDArea 212 { 213 None = 0, 214 Top = 1, 215 Bottom = 2, 216 Left = 4, 217 Right = 8, 218 All = 15 219 220 } 221}
回答1件
あなたの回答
tips
プレビュー