滑动解锁用到了3个鼠标事件,MouseDown按下鼠标时发生,MouseUp松开鼠标时发生,MouseMove按下又松开时发生
1、滑动解锁 2、解锁之后的页面 3、页面滑动切换
1、定义两个成员变量
//判断是否按下鼠标
private bool mousedown;
//记录按下时X坐标
private int CurX;
2、滑动解锁代码
//当按下鼠标时 private void button1_MouseDown(object sender, MouseEventArgs e) { //当鼠标按下时 mousedown 设置为 true mousedown=true; //记录一下当前X坐标 CurX = Cursor.Position.X; } //鼠标松开时 private void button1_MouseUp(object sender, MouseEventArgs e) { //当鼠标移动超过50时,解锁成功 if (Cursor.Position.X - CurX > 50) { panel3.Visible = false; } else { //没有超过50则返回原位置 button1.Location = new Point(30,180); } //松开鼠标时,mousedown设为false mousedown = false; } //鼠标按下又松开时 private void button1_MouseMove(object sender, MouseEventArgs e) { //记录一下当前鼠标滑动到什么位置 int curnowx = Cursor.Position.X; if (mousedown) { button1.Location = new Point(30+curnowx-CurX,180); } }
3、切换页面代码
//鼠标按下时 private void panel1_MouseDown(object sender, MouseEventArgs e) { //当鼠标按下时 mousedown 设置为 true mousedown = true; CurX =Cursor.Position.X; } //鼠标松开时 private void panel1_MouseUp(object sender, MouseEventArgs e) { //当鼠标移动超过30时 if (Cursor.Position.X - CurX < -30) { panel1.Location = new Point(-280, 0); panel2.Location = new Point(0, 0); } else { panel1.Location = new Point(0,0); panel2.Location = new Point(280,0); } mousedown = false; } //鼠标按下再松开时 private void panel1_MouseMove(object sender, MouseEventArgs e) { int curnowx=Cursor.Position.X; if (mousedown) { panel1.Location = new Point(0+curnowx-CurX,0); panel2.Location = new Point(280+curnowx-CurX,0); } } private void panel2_MouseDown(object sender, MouseEventArgs e) { mousedown = true; CurX = Cursor.Position.X; } private void panel2_MouseUp(object sender, MouseEventArgs e) { if (Cursor.Position.X - CurX > 30) { panel1.Location = new Point(0, 0); panel2.Location = new Point(280, 0); } else { panel1.Location = new Point(-280, 0); panel2.Location = new Point(0, 0); } mousedown = false; } private void panel2_MouseMove(object sender, MouseEventArgs e) { int curnowx=Cursor.Position.X; if (mousedown) { panel1.Location = new Point(-280+curnowx-CurX,0); panel2.Location = new Point(curnowx -CurX, 0); } }