StickyNote开发笔记2

@adens 11/28/2016 8:06:21 AM

点击拖动便签和双击修改便签事件执行BUG

问题描述

在便签的页面中想要实现点击便签实现拖动效果,双击实现修改便签内容.在具体制作过程中,点击实现拖动成功实现,而双击修改便签事件没有执行.

加断点debug发现每次双击都先执行了单击事件.原因是鼠标的每次点击都会传递单击事件,并不会等双击之后传递事件.

问题代码

using System.Runtime.InteropServices;

public partial class NoteModel : Form
{

[DllImport("user32.dll")]//*********************拖动无窗体的控件
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
//以上为实现拖动窗口的API引用

private void NoteModel_MouseDown(object sender, MouseEventArgs e)
{
//鼠标点击实现拖动
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//*********************调用移动无窗体控件函数
UpdateNote();


}
private void NoteModel_MouseDoubleClick(object sender, MouseEventArgs e)
{
//将文本控件变为可编辑状态
TextBox t = sender as TextBox;
t.ReadOnly = false;

}

private void NoteModel_LostFocus(object sender, EventArgs e)
{

UpdateNote();//保存数据
TextBox t = sender as TextBox;
t.ReadOnly = true;//改为不可编辑状态

}

}

解决问题

拖动窗口的方法的鼠标事件是鼠标按下触发,鼠标移动的时候执行.松开鼠标的时候结束.双击鼠标事件是在第二次鼠标松开的时候执行

定义一个变量用以确定鼠标按下之后移动鼠标执行拖动窗口事件.而不是只要鼠标移动就执行

只有鼠标按下时移动鼠标才执行拖动窗口

鼠标抬起之后移动鼠标不执行拖动窗口

双击鼠标事件不受影响

解决后代码

private bool MouseDownFlag = false;//鼠标未按下
private void NoteModel_MouseDown(object sender, MouseEventArgs e)
{
MouseDownFlag = true;//鼠标按下
}
private void NoteModel_MouseMove(object sender, MouseEventArgs e)
{
//鼠标点击实现拖动
if(MouseDownFlag)
{
//鼠标按下时拖动窗口
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//*********************调用移动无窗体控件函数
UpdateNote();
}
}
private void NoteModel_MouseUp(object sender, MouseEventArgs e)
{
//鼠标松开时不可拖动
MouseDownFlag = false;
}
private void NoteModel_MouseLeave(object sender, EventArgs e)
{
//鼠标离开时不可拖动
MouseDownFlag = false;
}

private void NoteModel_MouseDoubleClick(object sender, MouseEventArgs e)
{
//双击修改属性
TextBox t = sender as TextBox;
t.ReadOnly = false;

}
private void NoteModel_LostFocus(object sender, EventArgs e)
{
//失去焦点后保存数据修改属性
UpdateNote();
TextBox t = sender as TextBox;
t.ReadOnly = true;

}

Last Modification : 11/28/2016 8:06:21 AM


In This Document