程序分两个部分,一部分实现可拖动的准星,一部分实现在准星拖动时刷新准星所在窗口的句柄信息。
一个主窗口类:MainWindow,负责主窗口显示以及在收到开始信号后,定时刷新当前鼠标所在窗口信息并显示,收到停止信号后停止刷新。
一个可拖动的准星类:准星拖动以一个QWidget的派生类实现,在此类中,鼠标左键按下发出开始信号,左键释放发出停止信号。按下并拖动过程中,将准星QWidget移动到鼠标所在位置。
这是我静态编译好的可执行程序,单文件执行。
FormDragWidget类负责准星操作,主要是重载鼠标按键按下事件、鼠标移动事件、鼠标按键释放事件:
#ifndef FORMDRAGWIDGET_H
#define FORMDRAGWIDGET_H
#include <QWidget>
namespace Ui {
class FormDragWidget;
}
QT_BEGIN_NAMESPACE
QT_END_NAMESPACE
class FormDragWidget : public QWidget
{
Q_OBJECT
public:
explicit FormDragWidget(QWidget *parent = 0);
~FormDragWidget();
signals:
void sg_drag(bool flg);
private:
Ui::FormDragWidget *ui;
QPoint m_startPos;
private:
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseMoveEvent(QMouseEvent *event);
virtual void mouseReleaseEvent(QMouseEvent *event);
virtual void paintEvent(QPaintEvent *event);
};
#endif // FORMDRAGWIDGET_H
void FormDragWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_startPos = event->pos();
emit sg_drag(true);
}
QWidget::mousePressEvent(event);
}
void FormDragWidget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
move(event->globalPos() - QPoint(this->width()/2, this->height()/2));
}
QWidget::mouseMoveEvent(event);
}
void FormDragWidget::mouseReleaseEvent(QMouseEvent *event)
{
emit sg_drag(false);
QWidget::mouseReleaseEvent(event);
}
void FormDragWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
主窗口类负责根据接收的信号来检查准星所在窗口的句柄信息:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
QT_BEGIN_NAMESPACE
class FormDragWidget;
class QTimer;
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void sl_timeOut();
void sl_drag(bool flg);
void sl_init();
private:
Ui::MainWindow *ui;
FormDragWidget *mp_formDragWidget;
QTimer *mp_timer;
private:
void __GetWindowsInfo();
void __BackPos();
private:
virtual void resizeEvent(QResizeEvent *event);
virtual void moveEvent(QMoveEvent *event);
};
#endif // MAINWINDOW_H
void MainWindow::__GetWindowsInfo()
{
POINT ptCursor;
GetCursorPos(&ptCursor);
POINT pt2;
pt2.x = ptCursor.x-10;
pt2.y = ptCursor.y-10;
//获取窗口句柄
HWND hWnd = WindowFromPoint(pt2);
if (hWnd != 0) {
if ((HWND)this->winId() == hWnd) { //----- 本程序窗口
ui->lineEdit_windowHwnd->setText("");
ui->lineEdit_windowTitle->setText("");
ui->lineEdit_windowClass->setText("");
ui->lineEdit_windowStyle->setText("");
ui->lineEdit_windowRect->setText("");
return;
}
if ((HWND)mp_formDragWidget->winId() == hWnd) { //----- 准星窗口
return;
}
//----- 获取窗口标题
TCHAR szTitle[MAX_PATH];
GetWindowText(hWnd, szTitle, MAX_PATH);
//----- 获取窗口类名
TCHAR szClass[MAX_PATH];
GetClassName(hWnd, szClass, MAX_PATH);
//----- 获取窗口样式
LONG style = GetWindowLong(hWnd, GWL_STYLE);
//----- 获取窗口矩形
RECT rect;
GetWindowRect(hWnd, &rect);
QString windowsHwnd = QString("%1").arg((quintptr)hWnd, 8, 16, QLatin1Char('0')).toUpper();
QString windowsTitle = QString::fromWCharArray(szTitle);
QString windowsClass = QString::fromWCharArray(szClass);
QString windowsStyle = QString("%1").arg((quint32)style, 8, 16, QChar('0')).toUpper();
QString windowsRect = QString("(%1,%2)-(%3,%4) %5×%6")
.arg(rect.left)
.arg(rect.top)
.arg(rect.right)
.arg(rect.bottom)
.arg(rect.right - rect.left)
.arg(rect.bottom - rect.top);
ui->lineEdit_windowHwnd->setText(windowsHwnd);
ui->lineEdit_windowTitle->setText(windowsTitle);
ui->lineEdit_windowClass->setText(windowsClass);
ui->lineEdit_windowStyle->setText(windowsStyle);
ui->lineEdit_windowRect->setText(windowsRect);
}
}