我的WinForm只是一个40x40的按钮,用户可以点击呼叫寻求帮助。一切正常,除非您单击并拖动以将按钮移动到屏幕的其他部分,按钮在释放鼠标左键后激活(完成“单击”)。有没有办法在<img src={this.props.user.img} alt="logo" />
事件发生时检查窗口是否已移动,并在这种情况下停止mousedown
事件?
我目前正在做什么“点击并拖动”:
mouseclick
答案 0 :(得分:1)
你可以使用一个标志来处理:
private bool ignoreClick = false;
private void btn_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
ignoreClick = true;
btn.Left = e.X + btn.Left - mouseDownLocation.X;
btn.Top = e.Y + btn.Top - mouseDownLocation.Y;
}
}
private void btn_MouseUp(object sender, MouseEventArgs e) {
ignoreClick = false;
}
private void btn_Click(object sender, EventArgs e) {
if (!ignoreClick) {
// do your click code...
}
}
答案 1 :(得分:1)
您也可以通过这样的按钮移动表单:
public const int HT_CAPTION = 0x2;
public const int WM_NCLBUTTONDOWN = 0xA1;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello!");
}
答案 2 :(得分:0)
如果您可以使用子类按钮,则可以防止在光标移动太远时触发click事件(我认为这应该是默认行为)。
这样,实际的事件处理程序可以保持一点清洁,而不必处理这个问题。
class SpecialButton: Button
{
private Point ClickOrigin;
private const int ClickMaxDistance = 5;
protected override void OnMouseDown(MouseEventArgs e)
{
ClickOrigin = System.Windows.Forms.Cursor.Position;
base.OnMouseDown(e);
}
protected override void OnClick(EventArgs e)
{
Point p = System.Windows.Forms.Cursor.Position;
if(Math.Abs(p.X - ClickOrigin.X) < ClickMaxDistance &&
Math.Abs(p.Y - ClickOrigin.Y) < ClickMaxDistance)
base.OnClick(e);
}
}