2

I have a CHtmlView derived class. My menu has a Print Preview menu item. That event handler creates a special print preview derived document, and then it gets displayed:

void CChristianLifeMinistryHtmlView::DoPrintPreview()
{
    HWND      HWND_PP, HWND_FG ;
    TCHAR     szClassName[256] ;
    ULONGLONG t1, t2 ;
    RECT      workArea;

    ExecWB(OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_PROMPTUSER, nullptr, nullptr);

    HWND_PP = nullptr ;
    t1 = ::GetTickCount64();
    t2 = ::GetTickCount64(); // Extra line required for 'while' rearrangement.
    while (HWND_PP == nullptr && t2 - t1 <= 6000) // if found or Timeout reached...
    {
        HWND_FG = ::GetForegroundWindow(); // Get the ForeGroundWindow
        if (HWND_FG != nullptr)
        {
            ::GetClassName(HWND_FG, szClassName, 256 );
            if (lstrcmp(szClassName, IE_PPREVIEWCLASS) == 0) // Check for Print Preview Dialog
                HWND_PP = HWND_FG ;
        }

        theApp.PumpMessage();
        t2 = ::GetTickCount64();
    }

    if (HWND_PP != nullptr)
    {
        // AJT V3.01 BUG Fix - showing maximized crops bottom of preview dialog
        ::SystemParametersInfo( SPI_GETWORKAREA, 0, &workArea, 0 );
        ::MoveWindow(HWND_PP, workArea.left, workArea.top,
            workArea.right - workArea.left, workArea.bottom - workArea.top, TRUE);

    }

}

This works fine. I have zero problems with it. The problem is if the user right-clicks the actual view and chooses print from there:

Context Menu

I don't know how to intercept this menu entry. Ideally it needs to redirect the action back to my main dialog so it can do the print preview correctly.

I don't want to disable the context menu because it has other choices there that are useful, like, view source. But I would like to add my own behaviour for the context menu print and print preview actions.

Is this possible?

Just wondered if anyone else has had success with this?


This question relates to:

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    The best answer to this is the following [article](https://ehsanakhgari.org/article/visual-c/2008-06-22/custom-webbrowser-context-menus) – xMRi Oct 20 '17 at 05:20
  • @xMRi Thanks for this excellent article. I have to read it again to digest properly. However, it looks like I have a decision to make, unless I am missing something. There are three scenarios: Scenario 1:  Adding a new menu item to all applications using the WebBrowser control; Scenario 2:  Showing a whole customized WebBrowser context menu inside your application; Scenario 3:  Customizing the standard WebBrowser context menu. It looks like I need scenario 3. I think. – Andrew Truckle Oct 20 '17 at 08:50
  • @xMRi I tried compiling the third scenario in Visual Studio 2017 but I hot some build errors in both Debug and Release modes. I notice the source code in the ZIP is dated 2002 and refers to MFC 6 and MFC 7 in the article. Am I right in saying we are now on MFC 10? Does it match the SDK? Anyway, I can't get the samples to compile in themselves. – Andrew Truckle Oct 21 '17 at 14:13
  • I just tried to get the szenario 3 to run. I just turned /Gy compiler settings to use the inherited settings. The problem was just to set some interface definitions form protected to public.Change just each `protected:` clause before `BEGIN_INTERFACE_PART` into `public:`. The VC6 compiler was bad about C++ standards. – xMRi Oct 23 '17 at 06:50
  • @xMRi I followed your instructions. Thanks. It compiles. I have one warning `_WIN32_WINNT not defined. Defaulting to _WIN32_WINNT_MAXVER (see WinSDKVer.h)`. But using scenario 3 I find that right-click does nothing now. I even get a script error several times. – Andrew Truckle Oct 24 '17 at 13:28
  • Unfortunately the original link referred to in the comments also 404s now too. – Andrew Truckle Mar 03 '20 at 04:42
  • 1
    archive.org: https://web.archive.org/web/20180125183350/https://ehsanakhgari.org/article/visual-c/2008-06-22/custom-webbrowser-context-menus – xMRi Mar 03 '20 at 09:53
  • I have created a spin off question to this. https://stackoverflow.com/questions/60505381/is-it-not-possible-to-add-our-own-menu-items-on-the-chtmlview-context-menu At the moment I have replaced the context menu and removed the Print Preview features because I can't supersede their behaviour. – Andrew Truckle Mar 03 '20 at 11:45

0 Answers0