C#鼠标控制控件移动的示例
生活随笔
收集整理的這篇文章主要介紹了
C#鼠标控制控件移动的示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//鼠標點擊控件按住不放,控件跟隨移動。施放則不移動
//這里的控件可以更換為其他控件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Test
{public partial class Form1 : Form{private int ax, ay; // 定義兩個變量存儲鼠標的橫縱坐標public Form1(){InitializeComponent();}/// <summary>/// 鼠標首次單擊控件,記錄鼠標坐標/// </summary>/// <param ax="int">鼠標橫坐標</param>/// <param ay="int">鼠標縱坐標</param>private void button1_MouseDown(object sender, MouseEventArgs e){ax = e.X;ay = e.Y;}/// <summary>/// 鼠標移動時,控件跟隨鼠標移動。利用相對坐標計算/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_MouseMove(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){button1.Left = button1.Left + (e.X - ax);button1.Top = button1.Top + (e.Y - ay);}}}
}
?
轉載于:https://www.cnblogs.com/frankwu2014/p/4374128.html
總結
以上是生活随笔為你收集整理的C#鼠标控制控件移动的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hibernate中启用日志
- 下一篇: MD5加密字符串并转化为base64(C