在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;
}
答案 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。