初始效果图(左), 转动指针效果图(中),转动拨盘效果图(右)
代码如下:
int centerX = 124; int centerY = 124; private void Form1_Load(object sender, EventArgs e) { var bmp = GetPointerImg(Resources.valsalvaThresholdPointer, 90f, pictureBox1.Width, pictureBox1.Height, centerX, centerY); pictureBox1.Image = bmp; lb_currentScale.Text = currentScale.ToString(); } bool mouseMoved = false; int currentScale = 5;//当前刻度,取值范围[0,5] int moveStartX = int.MinValue; int moveStartY = int.MinValue; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { moveStartX = e.X; moveStartY = e.Y; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { mouseMoved = true; } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (!mouseMoved || (moveStartX == int.MinValue && moveStartY == int.MinValue)) return;//拖动才绘制 mouseMoved = false; Console.WriteLine($"X:{e.X}-{moveStartX},Y:{e.Y}-{moveStartY}"); //计算刻度步进 int addition = 1; //以变化大的为准 if (Math.Abs(e.Y - moveStartY) > Math.Abs(e.X - moveStartX)) addition = e.X.CompareTo(centerX) * moveStartY.CompareTo(e.Y);//中心点右方,↑为正,左方↓为正 else addition = e.Y.CompareTo(centerY) * e.X.CompareTo(moveStartX);//中心点上方,←为正,下方→为正 float currentAngle = (5 - currentScale) * 18;//当前角度,每个刻度 90/5 度 Console.WriteLine($"currentAngle:{currentAngle},addition:{addition}"); currentScale += addition; if (currentScale > 5 || currentScale < 0) { currentScale -= addition; } else if (addition != 0) { //1.5秒内过渡到下一个刻度 Task.Run(() => { //帧率24,1.5秒共36帧,每帧 1000/24 毫秒 for (int i = 0; i < 36; i++) { currentAngle -= addition * 0.5f;//角度变化和刻度相反,每帧 18/36 度 var bmp = GetPieImg(Resources.valsalvaThresholdPie, currentAngle, centerX, centerY); pictureBox1.BackgroundImage = bmp; Thread.Sleep(42); } }); lb_currentScale.Text = currentScale.ToString(); } moveStartX = int.MinValue; moveStartY = int.MinValue; } private Bitmap GetPieImg(Bitmap pieImg, float angle, int centerX, int centerY) { // 创建新的位图作为旋转后的图片 Bitmap rotatedImage = new Bitmap(pieImg.Width, pieImg.Height); // 创建一个Graphics对象,用于绘制旋转后的图片 using (Graphics g = Graphics.FromImage(rotatedImage)) { // 设置旋转中心点为图片中心 g.TranslateTransform(centerX, centerY); // 执行旋转 g.RotateTransform(angle); // 恢复旋转中心点到原位置 g.TranslateTransform(-centerX, -centerY); g.DrawImage(pieImg, Point.Empty); } return rotatedImage; } private void pictureBox1_Click(object sender, EventArgs e) { if (moveStartX != int.MinValue) return; var ea = (MouseEventArgs)e; double distance = Math.Sqrt(Math.Pow(ea.X - centerX, 2) + Math.Pow(ea.Y - centerY, 2));//点击处到圆心的距离 double angle = Math.Asin((centerY - ea.Y) / distance) * 180 / Math.PI; if (centerX < ea.X && centerY > ea.Y) angle = 180- angle;//第一象限 else if (centerX > ea.X && centerY < ea.Y) angle = 0;//第三象限 else if (centerX < ea.X && centerY < ea.Y) angle = 180;//第四象限 text_Angle.Text = angle.ToString("f3"); var bmp = GetPointerImg(Resources.valsalvaThresholdPointer, (float)angle, pictureBox1.Width, pictureBox1.Height, centerX, centerY); pictureBox1.Image = bmp; } private void btn_DrawPointer_Click(object sender, EventArgs e) { var angle = Convert.ToDouble(text_Angle.Text); var bmp = GetPointerImg(Resources.valsalvaThresholdPointer, (float)angle, pictureBox1.Width, pictureBox1.Height, centerX, centerY); pictureBox1.Image = bmp; } private Bitmap GetPointerImg(Bitmap pointerImg, float angle, int w, int h, int centerX, int centerY) { // 创建新的位图作为旋转后的图片 Bitmap rotatedImage = new Bitmap(w, h); // 创建一个Graphics对象,用于绘制旋转后的图片 using (Graphics g = Graphics.FromImage(rotatedImage)) { g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(centerX - 5, centerY - 5, 10, 10)); // 设置旋转中心点为图片中心 g.TranslateTransform(centerX, centerY); // 执行旋转 g.RotateTransform(angle); // 恢复旋转中心点到原位置 g.TranslateTransform(-centerX, -centerY); g.DrawImage(pointerImg, 0, centerY - pointerImg.Height / 2); } return rotatedImage; }
标签:拨盘,angle,int,currentScale,转动,pictureBox1,centerX,centerY,指针 From: https://www.cnblogs.com/chixiner/p/17693822.html