为什么在MouseUp事件之后发生MouseMove事件?

时间:2019-07-11 18:11:33

标签: c# .net windows winforms visual-c++

WindowsForms中,我仅添加了如下的事件处理程序:

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        Debug.WriteLine($"=> Form1_MouseDown, Clicks: {e.Clicks}, Location: {e.Location}");
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        Debug.WriteLine($"=> Form1_MouseUp, Clicks: {e.Clicks}, Location: {e.Location}");
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        Debug.WriteLine($"=> Form1_MouseMove, Clicks: {e.Clicks}, Location: {e.Location}");
    }

输出为:

=> Form1_MouseMove, Clicks: 0, Location: {X=17,Y=21}
=> Form1_MouseDown, Clicks: 1, Location: {X=17,Y=21}
=> Form1_MouseUp,   Clicks: 1, Location: {X=17,Y=21}
=> Form1_MouseMove, Clicks: 0, Location: {X=17,Y=21}

您可以看到所有事件都发生在同一位置,所以我的问题是为什么在MouseMove事件之后还会有MouseUp事件?

我也在WPF中尝试了类似的代码,并且发生了MouseMove事件

然后我在C ++中尝试了类似的代码,并且发生了MouseMove事件

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
     ...

    case WM_MOUSEMOVE:
        OutputDebugString(L"WM_MOUSEMOVE\n");
        break;

    case WM_LBUTTONDOWN:
        OutputDebugString(L"WM_LBUTTONDOWN\n");
        break;

    case WM_LBUTTONUP:
        OutputDebugString(L"WM_LBUTTONUP\n");
        break;

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

3 个答案:

答案 0 :(得分:5)

如果您以前将鼠标放在一个单独的窗口上,则单击一个新窗口并移动鼠标焦点将生成一个鼠标移动事件(即使鼠标在您单击鼠标之前或之后没有立即移动)鼠标)。

这里是指向类似StackOverflow响应的链接 "Ghost" MouseMove Event

答案 1 :(得分:2)

这是因为(function() { const dummyDb = {}; dummyDb.webdb = {}; dummyDb.webdb.db = null; dummyDb.webdb.open = function() { let dbSize = 5 * 1024 * 1024; // 5MB dummyDb.webdb.db = openDatabase( "dummy Database", "1", "dummyDb Data Collection Database", dbSize ); console.log(`#1`); }; //##### Step 2. Creating a table ##### dummyDb.webdb.createTable = function() { let db = dummyDb.webdb.db; db.transaction(function(tx) { tx.executeSql( "CREATE TABLE IF NOT EXISTS " + "dummyDb_tbl(dummy_id INTEGER PRIMARY KEY, dummy_created_by TEXT, dummy_date_on DATETIME)", [] ); db.addDummy("Dummy text has been added bla bla bla"); }); console.log(`#2)`); }; //###### Step 3. Adding data to a table ###### dummyDb.webdb.addDummy = function(dummyText) { let db = dummyDb.webdb.db; db.transaction(function(tx) { let addedOn = new Date(); tx.executeSql( "INSERT INTO dummyDb_tbl(dummy_created_by, dummyd_date_on) VALUES (?,?)", [dummyText, addedOn] ); }); console.log(`#3`); }; dummyDb.webdb.open(); dummyDb.webdb.createTable(); })(); 释放了MouseDown捕获的鼠标。而这个额外的MouseUp可能是为了确保光标位置。作为解决方法,您可以这样做

MouseMove

答案 2 :(得分:1)

这是预期的行为,每当切换应用程序(例如Alt + Tab)时也会触发。

您应该按照@VishnuBabu的变通方法建议的变通方法进行操作。并且要忽略初始的mousemove触发器,您可以在加载窗口后获取光标的当前位置,而不是将LastLocation设置为Empty。