0

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
4

1 回答 1

1

您在三个地方创建 g_handler,这可能是您的问题。

一个在您的 Winmain 文件中(您声明此变量的第一个代码片段)。

CefRefPtr<trackboxCefHandler> g_handler;

另一个在 trackboxWrapper.h 中。

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

第三个在 trackboxWrapper.cpp 中:

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

这是三个不同的变量,因为它们在每个地方都被完全声明,所以它们也相互影响(尽管头文件中的一个被构造函数中的参数初始化,剩下两个)。

您绝对不需要在第三个片段中创建它,因为您已经在标题中声明了 g_handler:

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

就足够了。

这样你应该到处都有相同的对象。

于 2013-10-28T00:36:03.513 回答