我在一个无边框窗口中处理 WM_NCHITTEST 来改变窗口大小,在加入一个 QAxWidget ("Microsoft Web Browser控件") 后, 处理 WM_NCHITTEST 失灵了, 调试后得知是加入 QAxWidget窗口后再也收不到 WM_NCHITTEST消息了。
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 设置无边框
this->setWindowFlags( Qt::FramelessWindowHint );
m_axWidget2 = new QAxWidget(ui->centralWidget);
//此处的{8856F961-340A-11D0-A96B-00C04FD705A2}就是Microsoft Web Browser控件的UUID
m_axWidget2->setControl(QString::fromUtf8("{8856F961-340A-11D0-A96B-00C04FD705A2}"));
m_axWidget2->dynamicCall("Navigate(const QString&)", "https://www.baidu.com"); //调用参
}
bool MainWindow::nativeEvent(const QByteArray & eventType, void * message, long * result) //主窗口处理 windows 消息
{
//qInfo() << eventType << endl;
if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG")
{
MSG * pMsg = reinterpret_cast<MSG *>(message);
if (pMsg->message == WM_NCHITTEST )
{
qInfo() << pMsg->message << "WM_NCHITTEST" <<endl;
int _border = 5; //鼠标检测范围, pixel
int m_nInflate = (-11); //窗口外框彭涨值
QRect rcTitle = QRect(_border, _border , this->geometry().width(), 32);
//获取到系统鼠标移动,可以做像qq一样的忙碌检测
QPoint mPoint = QPoint(INT16((int)pMsg->lParam & 0xFFFF), (int)pMsg->lParam >> 16 & 0xFFFF);
mPoint = this->mapFromGlobal(mPoint); //视口坐标转为窗口坐标
mPoint = QPoint(mPoint.x()+ m_nInflate,mPoint.y() + m_nInflate);
int xPos = GET_X_LPARAM(pMsg->lParam) - this->frameGeometry().x();//解析鼠标相对坐标
int yPos = GET_Y_LPARAM(pMsg->lParam) - this->frameGeometry().y();
if(rcTitle.contains(xPos, yPos))
{
*result= HTCAPTION; return true;
}
if (mPoint.x() <= _border)
{
if (mPoint.y() <= _border)
{
*result = HTTOPLEFT; return true;
}
else if (mPoint.y() >= (this->height() + 2 * m_nInflate) - _border)
{
*result = HTBOTTOMLEFT; return true;
}
else
{
*result = HTLEFT; return true;
}
}
else if (mPoint.x() >= (this->width() + 2 * m_nInflate) - _border)
{
if (mPoint.y() <= _border)
{
*result = HTTOPRIGHT; return true;
}
else if (mPoint.y() >= (this->height() + 2 * m_nInflate) - _border)
{
*result = HTBOTTOMRIGHT; return true;
}
else
{
*result = HTRIGHT; return true;
}
}
else if (mPoint.y() <= _border)
{
*result = HTTOP; return true;
}
else if (mPoint.y() >= (this->height() + 2 * m_nInflate) - _border)
{
*result = HTBOTTOM; return true;
}
}
}
return QMainWindow::nativeEvent(eventType, message, result);
}