Example #1
0
void LootHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
  assert(CefCurrentlyOn(TID_UI));

#ifdef _WIN32
        // Set the title bar icon.
  HWND hWnd = browser->GetHost()->GetWindowHandle();
  HANDLE hIcon = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(MAINICON), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE);
  HANDLE hIconSm = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(MAINICON), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE);
  SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
  SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);

  // Set the window title.
  SetWindowText(hWnd, L"LOOT");
#endif

        // Set window size & position.
  if (lootState_.isWindowPositionStored()) {
#ifdef _WIN32
    RECT rc;
    rc.left = lootState_.getWindowPosition().left;
    rc.top = lootState_.getWindowPosition().top;
    rc.right = lootState_.getWindowPosition().right;
    rc.bottom = lootState_.getWindowPosition().bottom;

    // Fit the saved window size/position to the current monitor setup.

    // Get the nearest monitor to the saved size/pos.
    HMONITOR hMonitor;
    hMonitor = MonitorFromRect(&rc, MONITOR_DEFAULTTONEAREST);

    // Get the rect for the monitor's working area.
    MONITORINFO mi;
    mi.cbSize = sizeof(mi);
    GetMonitorInfo(hMonitor, &mi);

    // Clip the saved rect to fit inside the monitor rect.
    int width = rc.right - rc.left;
    int height = rc.bottom - rc.top;
    rc.left = max(mi.rcWork.left, min(mi.rcWork.right - width, rc.left));
    rc.top = max(mi.rcWork.top, min(mi.rcWork.bottom - height, rc.top));
    rc.right = rc.left + width;
    rc.bottom = rc.top + height;

    SetWindowPos(hWnd, HWND_TOP, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_SHOWWINDOW);
#endif
  } else {
#ifdef _WIN32
            // High DPI support doesn't seem to scale window content correctly
            // unless the window is resized, so if no size info is recorded,
            // just set its current size + 1.
    RECT rc;
    GetWindowRect(browser->GetHost()->GetWindowHandle(), &rc);
    SetWindowPos(browser->GetHost()->GetWindowHandle(), HWND_TOP, rc.left, rc.top, rc.right - rc.left + 1, rc.bottom - rc.top + 1, SWP_SHOWWINDOW);
#endif
  }

  // Add to the list of existing browsers.
  browser_list_.push_back(browser);

  // Create a message router.
  CefMessageRouterConfig config;
  browser_side_router_ = CefMessageRouterBrowserSide::Create(config);

  browser_side_router_->AddHandler(new QueryHandler(lootState_), false);
}