Qt使用IE控件和屏蔽页面右键菜单

来源:互联网 发布:编程技术流 编辑:程序博客网 时间:2024/05/29 12:28

要在Qt中使用IE控件,需要用到 QAxWidget,在工程文件中需要添加axcontainer

QT       += core gui axcontainer

在设计器中添加QAxWidget


右键点击控件,设置ActiveX控件为Microsoft Web Browser



通过页面加载后,查找到Internet Explorer_Server窗口后,直接屏蔽鼠标右键点击消息

#include "mainwindow.h"#include "ui_mainwindow.h"#include <QDebug>#include <Windows.h>MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);    navigate("http://www.baidu.com");}MainWindow::~MainWindow(){    delete ui;}void MainWindow::navigate(const QString &url){    ui->axWidget->dynamicCall("Navigate(cosnt QString &)", url);}void MainWindow::showHwndName(HWND hwnd){    if (hwnd == 0)        return;    TCHAR className[MAX_PATH];    GetClassName(hwnd, className, MAX_PATH);    qDebug() << QString::fromWCharArray(className);}static WNDPROC preWndProc;LONG shieldMenuWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){    if(msg==WM_RBUTTONDOWN) //屏蔽鼠标右键        return 0;    return CallWindowProc((WNDPROC)preWndProc,hwnd,msg,wParam,lParam);}void MainWindow::shieldIEContextMenu(QAxWidget *widget){    HWND widgetHwnd = (HWND)widget->winId();    if (widgetHwnd > 0)    {        HWND ieServerHwnd = GetTopWindow(widgetHwnd); //QAxHostWidgetWindow        ieServerHwnd = GetWindow(ieServerHwnd, GW_CHILD); //Shell Embedding        ieServerHwnd = GetWindow(ieServerHwnd, GW_CHILD); //Shell DocObject View        ieServerHwnd = GetWindow(ieServerHwnd, GW_CHILD); //Internet Explorer_Server        if (ieServerHwnd > 0)        {            if (GetWindowLong(ieServerHwnd, GWL_WNDPROC) != (LONG)&shieldMenuWndProc)            {                qDebug() << "shield Menu";                preWndProc=(WNDPROC)::SetWindowLong(ieServerHwnd,GWL_WNDPROC,(LONG)shieldMenuWndProc);            }            else            {                qDebug() << "ignore";            }        }    }}void MainWindow::on_axWidget_NavigateComplete(const QString &URL){    shieldIEContextMenu(ui->axWidget);}


0 0
原创粉丝点击