如果你只需要模擬鼠標點擊效果而不需要模擬在按下鼠標左鍵的同時移動鼠標效果就不需要導入User32.Lib
模擬鼠標點擊效果:
QPoint pos; pos.setX(88); pos.setY(58); QMouseEvent *mEvnPress; QMouseEvent *mEvnRelease; mEvnPress = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(QWidget::focusWidget(),mEvnPress); // mEvnRelease = new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(QWidget::focusWidget(),mEvnRelease);
函數原型:
QMouseEvent(Type type, const QPointF &localPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
說明:
這里的pos的位置是接受鼠標事件的widget的內部的一個局部位置。也就是說他的鼠標按鍵的產生點是:先通過
QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);//也就是說先是指明了傳遞給哪個widget
然后再根據mEventPress來進行具體的再改widget中的定位,以及具體的按鍵是什么。
下圖是第一個參數Type的一些值:
(這個MouseMove就是鼠標左鍵按下時移動?,但是我用的時候不符合我的要求,好像不是我要的那種左鍵按下時移動的效果,你們可以試一試)
bool MainWidget::eventFilter(QObject *target, QEvent *event) { if(event->type()==QEvent::FocusIn){//event->type()==QEvent::Enter ||
static_cast<QPushButton*>(target)->setStyleSheet("background-color: rgb(129, 129,129)"); QPalette pal; pal.setColor(QPalette::Active,QPalette::ButtonText,Qt::red); static_cast<QPushButton*>(target)->setPalette(pal); } else if(event->type()==QEvent::FocusOut){//event->type()==QEvent::Leave ||
static_cast<QPushButton*>(target)->setStyleSheet(""); QPalette pal; pal.setColor(QPalette::Active,QPalette::ButtonText,Qt::black); static_cast<QPushButton*>(target)->setPalette(pal); } else if(event->type()== QEvent::KeyPress){ QPoint pos; QWidget *now_button= QWidget::focusWidget(); QKeyEvent *k = (QKeyEvent *)event; QLayoutItem *next_button; switch (k->key()){ case Qt::Key_Up: next_button= ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)+8)%12); next_button->widget()->setFocus(); break; case Qt::Key_Down: next_button= ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)+4)%12); next_button->widget()->setFocus(); #ifdef COURSE QCursor::setPos(pos); #endif
break; case Qt::Key_Left: next_button= ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)-1+12)%12); next_button->widget()->setFocus(); #ifdef COURSE QCursor::setPos(pos); #endif
break; case Qt::Key_Right: next_button= ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)+1)%12); next_button->widget()->setFocus(); break; case Qt::Key_Period: pos = now_button->pos(); pos.setX( 20 + pos.x()+(now_button->width())/2 ); pos.setY( 20 + pos.y()+(now_button->height())/2 ); //printf("/n***%d1 %d***/n",pos.x(),pos.y());
#ifdef COURSE QCursor::setPos(pos); #endif pos.setX(88); pos.setY(58); QMouseEvent *mEvnPress; QMouseEvent *mEvnRelease; mEvnPress = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(QWidget::focusWidget(),mEvnPress); mEvnRelease = new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QApplication::sendEvent(QWidget::focusWidget(),mEvnRelease); next_button = ui->gridLayout->itemAt(ui->gridLayout->indexOf(QWidget::focusWidget())); break; default: return QWidget::eventFilter(target,event); } if(next_button){ pos.setX( 20 + next_button->geometry().x() + (next_button->geometry().width()) / 2 ); pos.setY( 20 + next_button->geometry().y() + (next_button->geometry().height()) / 2); #ifdef COURSE QCursor::setPos(pos); #endif } return true; } return QWidget::eventFilter(target,event); }
模擬鼠標左擊下移動(我寫了一個實例,可以看我另外一篇博客: Qt實現鼠標拖拉窗口,並實現模擬鼠標拖拉窗口代碼實例):
使用到了mouse_event函數,這個函數我們主要使用下面幾種類型參數:
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//原位置左鍵按下 //Sleep(33);
mouse_event(MOUSEEVENTF_MOVE,0,300,0,0);//向下移動300 //Sleep(33);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//在移動后的位置釋放左鍵
//函數原型
VOID mouse_event( DWORD dwFlags, // motion and click options
DWORD dx, // horizontal position or change
DWORD dy, // vertical position or change
DWORD dwData, // wheel movement
ULONG_PTR dwExtraInfo // application-defined information
); /*函數參數介紹 dwFlags:標志位集,指定點擊按鈕和鼠標動作的多種情況。 dx:指定鼠標沿x軸的絕對位置或者從上次鼠標事件產生以來移動的數量,依賴於MOUSEEVENTF_ABSOLUTE的設置。給出的絕對數據作為鼠標的實際X坐標;給出的相對數據作為移動的mickeys數。一個mickey表示鼠標移動的數量,表明鼠標已經移動。 dy:指定鼠標沿y軸的絕對位置或者從上次鼠標事件產生以來移動的數量,依賴於MOUSEEVENTF_ABSOLUTE的設置。給出的絕對數據作為鼠標的實際y坐標,給出的相對數據作為移動的mickeys數。 dwData:如果dwFlags為MOUSEEVENTF_WHEEL,則dwData指定鼠標輪移動的數量。正值表明鼠標輪向前轉動,即遠離用戶的方向;負值表明鼠標輪向后轉動,即朝向用戶。一個輪擊定義為WHEEL_DELTA,即120。如果dwFlagsS不是MOUSEEVENTF_WHEEL,則dWData應為零。 dwExtralnfo:指定與鼠標事件相關的附加32位值。應用程序調用函數GetMessageExtraInfo來獲得此附加信息。 */
常用鍵值表:
鍵盤鍵與虛擬鍵碼對照表 字母和數字鍵 數字小鍵盤的鍵 功能鍵 其它鍵 鍵 鍵碼 鍵 鍵碼 鍵 鍵碼 鍵 鍵碼 A 65 0 96 F1 112 Backspace 8 B 66 1 97 F2 113 Tab 9 C 67 2 98 F3 114 Clear 12 D 68 3 99 F4 115 Enter 13 E 69 4 100 F5 116 Shift 16 F 70 5 101 F6 117 Control 17 G 71 6 102 F7 118 Alt 18 H 72 7 103 F8 119 Caps Lock 20 I 73 8 104 F9 120 Esc 27 J 74 9 105 F10 121 Spacebar 32 K 75 * 106 F11 122 Page Up 33 L 76 + 107 F12 123 Page Down 34 M 77 Enter 108 -- -- End 35 N 78 - 109 -- -- Home 36 O 79 . 110 -- -- Left Arrow 37 P 80 / 111 -- -- Up Arrow 38 Q 81 -- -- -- -- Right Arrow 39 R 82 -- -- -- -- Down Arrow 40 S 83 -- -- -- -- Insert 45 T 84 -- -- -- -- Delete 46 U 85 -- -- -- -- Help 47 V 86 -- -- -- -- Num Lock 144 W 87 X 88 Y 89 Z 90
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
需要導入頭文件:
//注意導入這兩個頭文件順序不能錯
#include "Windows.h" #include "WinUser.h"
如果報錯出現“報錯C1189 #error: "No Target Architecture"”那就是頭文件導入出現了問題(更多解決辦法看:https://www.cnblogs.com/kevinWu7/p/10163436.html)
"No Target Architecture"原因:
是因為單獨包含了一些windows.h已經包含了的頭文件如"fileapi.h","WinUser.h",但是卻沒有包含windows.h
或者
先包含了如"fileapi.h","WinUser.h",后包含windows.h,順序不對先說解決方案:
在代碼中 加入include “windows.h” 即可,或者調整順序,把winows.h放在前面
然后導入User32.Lib這個庫,這個庫你可以在你的C盤搜索這個文件(我是這樣找到的)
然后導入到Qt的pro文件中就行了(導入vs中的方法自行百度吧,好多人講)
LIBS += F:\Qt\workspace\MouseTest\lib\User32.Lib
也可以配置相對路徑(對Qt Creator),是相對pro文件的,但是要注意在pro文件內的有些相對路徑不是相對於pro文件
原文見:https://blog.csdn.net/u010846653/article/details/73466872
1.相對於pro文件本身的相對路徑,例如:
INCLUDEPATH += \ ../AAA\ ../BBB \ ../CCC \ ../DDD \
//還有source ,form,headers 的文件亦如此。
2.相對於構建生成目錄的相對路徑。
win32:CONFIG(debug) { DESTDIR = ../output/debug/bin OBJECTS_DIR = ./debug/obj MOC_DIR = ./debug/moc LIBS += -L../output/debug/lib/ \ -laaa \ -lbbb \ -lccc \ -lddd \ } win32:CONFIG(release) { DESTDIR = ../output/release/bin OBJECTS_DIR = ./release/obj MOC_DIR = ./release/moc LIBS += -L../output/release/lib/ \ -laaa \ -lbbb \ -lccc \ -lddd \ }
這里面的相對路徑都是基於你項目下的構建目錄來的,如下圖