简体   繁体   中英

Awesomium WebView doesn't display page

I've made two simple classes to use for displaying UIs with Awesomium. However creating the Awesomium WebView and setting the parent window to my Win32 window causes the program to hang and the page is never displayed. The classes are simple and creating a window is simple so there isn't much I can think of that could be going wrong. Perhaps there is something else required than what I've done to display a WebView?

To clarify: Creating the Win32 window without creating the WebView works fine, the window functions including the drag code etc... The hang only happens when you call set_parent_window.

UI.h

#pragma once
#include <Windows.h>
#include <windowsx.h>
#include <Awesomium/WebCore.h>
#include <Awesomium/STLHelpers.h>

using namespace Awesomium;

LRESULT CALLBACK LoginUICallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

class UI
{
public:
    //Window Variables
    HINSTANCE instance;
    HWND window;
    BOOL drag_window = false;
    SHORT mouse_x, mouse_y, mouse_x_prev, mouse_y_prev;

    //Awesomium Variables
    WebCore* webcore = 0;
    WebView* webview;

    static HWND InitWindow(INT width, INT height, WNDPROC callback)
    {
        HWND hwnd;

        WNDCLASSEX wc;
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.style = 0;
        wc.lpfnWndProc = callback;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = GetModuleHandle(0);
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = "MyUI";
        wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

        if (!RegisterClassEx(&wc))
        {
            char msg[100];
            sprintf(msg, "System Error: %i", GetLastError());
            MessageBox(NULL, msg, "ERROR", MB_OK);
            return NULL;
        }

        hwnd = CreateWindow("MyUI",
            "",
            WS_POPUP,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            width,
            height,
            NULL,
            NULL,
            GetModuleHandle(0),
            NULL);

        if (!hwnd)
        {
            char msg[100];
            sprintf(msg, "System Error: %i", GetLastError());
            MessageBox(NULL, msg, "ERROR", MB_OK);
            return NULL;
        }


        ShowWindow(hwnd, SW_SHOWNORMAL);
        UpdateWindow(hwnd);
        SetTimer(hwnd, 0, 15, NULL);

        return hwnd;
    }
};

class LoginUI : public UI
{
public:
    INT width = 600;
    INT height = 600;

    INT RunUI()
    {
        this->window = UI::InitWindow(this->width, this->height, ::LoginUICallback);

        if (!this->window)
            return 0;

        WebConfig config;
        this->webcore = WebCore::Initialize(config);
        this->webview = this->webcore->instance()->CreateWebView(this->width, this->height, 0, kWebViewType_Window);
        this->webview->set_parent_window(this->window);
        this->webview->LoadURL(WebURL(WSLit("http://www.google.com")));

        MSG msg;
        while(GetMessage(&msg, this->window, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        WebCore::Shutdown();

        return 1;
    }
}login_ui;

LRESULT CALLBACK LoginUICallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_COMMAND:
            return DefWindowProc(hWnd, message, wParam, lParam);
            break;
        case WM_TIMER:
            break;
        case WM_MOUSEMOVE:
        {
            if (login_ui.drag_window && (wParam & MK_LBUTTON))
            {
                // code executed when the dialog window is moved around on the screen
                RECT win_rect;
                GetWindowRect(hWnd, &win_rect);
                int x_coord = GET_X_LPARAM(lParam);
                int y_coord = GET_Y_LPARAM(lParam);
                MoveWindow(hWnd,
                    win_rect.left + x_coord - login_ui.mouse_x_prev,
                    win_rect.top + y_coord - login_ui.mouse_y_prev,
                    win_rect.right - win_rect.left,
                    win_rect.bottom - win_rect.top,
                    false
                    );
            }
            break;
        }
        case WM_LBUTTONDOWN:
        {
            login_ui.mouse_x = GET_X_LPARAM(lParam);
            login_ui.mouse_y = GET_Y_LPARAM(lParam);
            if (login_ui.mouse_y < 41)
            {
                login_ui.mouse_x_prev = login_ui.mouse_x;
                login_ui.mouse_y_prev = login_ui.mouse_y;
                SetCapture(hWnd);
                login_ui.drag_window = true;
            }
            break;
        }
        case WM_LBUTTONUP:
        {
            if (login_ui.drag_window)
            {
                login_ui.drag_window = false;
                ReleaseCapture();
            }
            break;
        }
        case WM_SIZE:
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_QUIT:
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

Main.Cpp

#include "UI.h"

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, char*, int nCmdShow)
{
    login_ui.RunUI();
}

Have not used Awesomium but your GetMessage only pumps messages for the WebCore window. So instead you should pass NULL so your message pump dispatches messages for all windows created on that thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM