0


AWTK-WIDGET-WEB-VIEW 实现笔记 (2) - Windows

在 Windows 平台上的实现,相对比较顺利,将一个窗口嵌入到另外一个窗口是比较容易的事情。

在这里插入图片描述

1. 创建窗口

这里有点需要注意:

  • 父窗口的大小变化时,子窗口也要跟着变化,否则 webview 显示不出来。
  • 创建时窗口的大小先设置为 0,后面再调整,否则 webview 也显示不出来。
  1. #include<windows.h>#include<SDL_syswm.h>
  2. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){switch(uMsg){case WM_SIZE:{
  3. HWND hwndSub =FindWindowEx(hwnd,NULL,NULL,NULL);if(hwndSub){
  4. RECT rcClient;int width =0;int height =0;GetClientRect(hwnd,&rcClient);
  5. width = rcClient.right - rcClient.left;
  6. height = rcClient.bottom - rcClient.top;MoveWindow(hwndSub,0,0, width, height, TRUE);}break;}case WM_PAINT:{
  7. PAINTSTRUCT ps;
  8. HDC hdc =BeginPaint(hwnd,&ps);FillRect(hdc,&ps.rcPaint,(HBRUSH)(COLOR_WINDOW +1));EndPaint(hwnd,&ps);break;}default:returnDefWindowProc(hwnd, uMsg, wParam, lParam);}return0;}staticconstchar CLASS_NAME[]="WebViewContainer";staticret_twebview_os_window_init(HINSTANCE hInstance){
  9. WNDCLASSEX wc;ZeroMemory(&wc,sizeof(WNDCLASSEX));
  10. wc.cbSize =sizeof(WNDCLASSEX);
  11. wc.hInstance = hInstance;
  12. wc.lpszClassName = CLASS_NAME;
  13. wc.lpfnWndProc = WindowProc;RegisterClassEx(&wc);return RET_OK;}webview_os_window_twebview_os_window_create(SDL_Window* parent,int x,int y,int w,int h){
  14. SDL_SysWMinfo wmInfo;SDL_VERSION(&wmInfo.version);SDL_GetWindowWMInfo(parent,&wmInfo);
  15. HWND hwndParent = wmInfo.info.win.window;
  16. HINSTANCE hInstance = wmInfo.info.win.hinstance;webview_os_window_init(hInstance);
  17. HWND hwndSub =CreateWindowEx(0, CLASS_NAME,"Container Window", WS_CHILD | WS_VISIBLE, x, y,0,0, hwndParent,NULL, hInstance,NULL);if(hwndSub ==NULL){return0;}ShowWindow(hwndSub, SW_SHOW);UpdateWindow(hwndSub);if(!hwndSub){printf("Failed to create subwindow: %lu\n",GetLastError());returnNULL;}return(webview_os_window_t)hwndSub;}

2. 调整窗口大小

resize 窗口时,需要调整子窗口的大小。要注意的是,窗口的大小是以像素为单位的,所以需要考虑缩放因子。

  1. voidwebview_os_window_move_resize(SDL_Window* parent,webview_os_window_t subwindow,int x,int y,int w,int h){
  2. HWND hwndSub =(HWND)subwindow;float scale =system_info()->device_pixel_ratio;MoveWindow(hwndSub, x, y, w * scale, h * scale, TRUE);}

3. 销毁窗口

销毁窗口时,需要销毁子窗口。

  1. voidwebview_os_window_destroy(webview_os_window_t subwindow){
  2. HWND hwndSub =(HWND)subwindow;DestroyWindow(hwndSub);}
标签: AWTK webview

本文转载自: https://blog.csdn.net/absurd/article/details/143834120
版权归原作者 李先静 所有, 如有侵权,请联系我们删除。

“AWTK-WIDGET-WEB-VIEW 实现笔记 (2) - Windows”的评论:

还没有评论