code.club
標題:
用程式碼直接創設3x3圖像以及可移動的屬性
[打印本頁]
作者:
csharp
時間:
2021-6-25 18:11
標題:
用程式碼直接創設3x3圖像以及可移動的屬性
本帖最後由 csharp 於 2021-6-25 19:15 編輯
public partial class Form1 : Form
{
int oldX;
int oldY;
bool isdown;
public Form1()
{
InitializeComponent();
PictureBox[] pb = new PictureBox[9];
for (int i = 0; i < 9; i++) pb[i] = new PictureBox();
for (int i = 0; i < 9; i++)
{
pb[i].Location = new System.Drawing.Point(50 + i %3* 99, i/3* 99);
pb[i].Size = new Size(99, 99);
pb[i].Image = Image.FromFile("d:\\img\\" + (i + 1) + ".jpg");
Controls.Add(pb[i]);
}
for (int i = 0; i < 9; i++)
{
pb[i ].MouseDown += new MouseEventHandler(pb_mousedown);
pb[i ].MouseMove += new MouseEventHandler(pb_mousemove);
pb[i].MouseUp += new MouseEventHandler(pb_mouseup);
}
}
void pb_mousedown(object sender, MouseEventArgs e)
{
PictureBox pb = (PictureBox)sender;
isdown = true;
oldX = e.X;
oldY = e.Y;
}
void pb_mousemove(object sender, MouseEventArgs e)
{
PictureBox pb = (PictureBox)sender;
if (isdown)
{
pb.Top += e.Y - oldY;
pb.Left += e.X - oldX;
}
}
void pb_mouseup(object sender, MouseEventArgs e)
{
isdown = false;
}
}
複製代碼
歡迎光臨 code.club (https://code.club/)
Powered by Discuz! X3.2