Question

I'm working on window in the winapi that displays a cef browser. My code compiles without errors and doesn't run into any runtime errors, but while my window displays, my cef webpage does not (my window is entirely blank). I've spent about 6 hours going about this but still haven't got anything to work.

I have my window in a separate class from my main function, and I think that might be the cause of my problems, as my g_handler might not be passed correctly.

Thanks for your help!

I stripped all of my windows api code of my examples (as it has been working fine) to keep my code samples short.

Here is my code:

Winmain:

#include "trackboxWrapper.h"
#include <stdexcept>
#include <thread>
#include "include\cef_app.h"

CefRefPtr<trackboxCefHandler> g_handler;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    try
    {
        CefMainArgs main_args(hInstance);
        int exitCode = CefExecuteProcess(main_args, NULL);
        if (exitCode >= 0) {
            return exitCode;
        }

        CefSettings settings;
        CefRefPtr<CefApp> cefApplication;
        CefInitialize(main_args, settings, cefApplication);

        trackboxWrapper window(g_handler);

        CefRunMessageLoop();
    CefShutdown();
    }
    catch (std::exception& e)
    {
        MessageBoxA(0, (std::string("Trackox Internal Error \n\n") + e.what()).c_str(), "=(", 0);
    }
    }

trackboxWrapper(only cef relavent parts are shown): header(trackboxWrapper.h):

[code]#include <windows.h>

#include "include\cef_app.h"
#include "include\cef_base.h"
#include "include\cef_browser.h"
#include "include\cef_client.h"

class trackboxWrapper
{
public:
CefRefPtr<trackboxCefHandler> & g_handler;

trackboxWrapper(CefRefPtr<trackboxCefHandler> g_handler_pointer);
~trackboxWrapper();
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
}

cpp(trakcboxWrapper.cpp):

trackboxWrapper::trackboxWrapper(CefRefPtr<trackboxCefHandler> g_handler_pointer) : g_handler(g_handler_pointer) {}

LRESULT CALLBACK trackboxWrapper::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    trackboxWrapper *window = reinterpret_cast<trackboxWrapper*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
    if (!window) return DefWindowProc(hwnd, msg, wparam, lparam);

    switch (msg)
    {
    case WM_CREATE:
        {
            window->g_handler = new trackboxCefHandler();
            RECT trackboxCefRect;
            GetClientRect(hwnd, &trackboxCefRect);
            CefWindowInfo info;
            CefBrowserSettings settings;
            info.SetAsChild(hwnd, trackboxCefRect);
            CefBrowserHost::CreateBrowser(info, window->g_handler.get(), L"http://www.google.com", settings);

                  }
}

trackboxCefHandler.h:

#ifndef TRACKBOXCEFHANDLER_H_
#define TRACKBOXCEFHANDLER_H_

#include "include/cef_client.h"

class trackboxCefHandler : public CefClient {

public:
    trackboxCefHandler() {}
    ~trackboxCefHandler() {}

    IMPLEMENT_REFCOUNTING(trackboxCefHandler);
    IMPLEMENT_LOCKING(trackboxCefHandler);
};

#endif
Était-ce utile?

La solution

You create g_handler in three places, that might be your problem.

One is in the file with your Winmain (first code snippet where you declare this variable).

CefRefPtr<trackboxCefHandler> g_handler;

Another is in trackboxWrapper.h.

class trackboxWrapper
{
public:
CefRefPtr<trackboxCefHandler> g_handler;
....

And the third one is in trackboxWrapper.cpp:

case WM_CREATE:
    {
        CefRefPtr<trackboxCefHandler> g_handler = new trackboxCefHandler();

These are three different variables because they are fully declared in each of these places, so they shadow each other too (though the one in header file gets initialized with the argument in constructor, which leaves two).

You definitely don't need to create it in the third snippet, because you have already declared g_handler in your header:

case WM_CREATE:
    {
        window->g_handler = new trackboxCefHandler();

will be enough.

That way you should have the same object everywhere.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top