cefclient.dpr 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. program cefclient;
  2. uses
  3. Windows, Messages, ceflib;
  4. type
  5. TCustomClient = class(TCefClientOwn)
  6. private
  7. FLifeSpan: ICefLifeSpanHandler;
  8. protected
  9. function GetLifeSpanHandler: ICefLifeSpanHandler; override;
  10. public
  11. constructor Create; override;
  12. end;
  13. TCustomLifeSpan = class(TCefLifeSpanHandlerOwn)
  14. protected
  15. procedure OnAfterCreated(const browser: ICefBrowser); override;
  16. procedure OnBeforeClose(const browser: ICefBrowser); override;
  17. end;
  18. type
  19. TWindowProc = Pointer;
  20. WNDPROC = Pointer;
  21. var
  22. Window: HWND;
  23. handl: ICefClient = nil;
  24. brows: ICefBrowser = nil;
  25. browserId: Integer = 0;
  26. navigateto: ustring = 'http://www.google.com';
  27. setting: TCefBrowserSettings;
  28. function CefWndProc(Wnd: HWND; message: UINT; wParam: Integer; lParam: Integer): Integer; stdcall;
  29. var
  30. ps: PAINTSTRUCT;
  31. info: TCefWindowInfo;
  32. rect: TRect;
  33. hdwp: THandle;
  34. begin
  35. case message of
  36. WM_PAINT:
  37. begin
  38. BeginPaint(Wnd, ps);
  39. EndPaint(Wnd, ps);
  40. result := 0;
  41. end;
  42. WM_CREATE:
  43. begin
  44. handl := TCustomClient.Create;
  45. GetClientRect(Wnd, rect);
  46. FillChar(info, SizeOf(info), 0);
  47. info.Style := WS_CHILD or WS_VISIBLE or WS_CLIPCHILDREN or WS_CLIPSIBLINGS or WS_TABSTOP;
  48. info.parent_window := Wnd;
  49. info.x := rect.left;
  50. info.y := rect.top;
  51. info.Width := rect.right - rect.left;
  52. info.Height := rect.bottom - rect.top;
  53. FillChar(setting, sizeof(setting), 0);
  54. setting.size := SizeOf(setting);
  55. CefBrowserHostCreateSync(@info, handl, navigateto, @setting, nil);
  56. SetTimer(Wnd, 1, 100, nil);
  57. result := 0;
  58. end;
  59. WM_DESTROY:
  60. begin
  61. brows := nil;
  62. PostQuitMessage(0);
  63. result := DefWindowProc(Wnd, message, wParam, lParam);
  64. end;
  65. WM_SETFOCUS:
  66. begin
  67. if brows <> nil then
  68. PostMessage(brows.Host.WindowHandle, WM_SETFOCUS, wParam, 0);
  69. Result := 0;
  70. end;
  71. WM_SIZE:
  72. begin
  73. if(brows <> nil) then
  74. begin
  75. // Resize the browser window and address bar to match the new frame
  76. // window size
  77. GetClientRect(Wnd, rect);
  78. hdwp := BeginDeferWindowPos(1);
  79. hdwp := DeferWindowPos(hdwp, brows.Host.WindowHandle, 0, rect.left, rect.top,
  80. rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER);
  81. EndDeferWindowPos(hdwp);
  82. end;
  83. result := DefWindowProc(Wnd, message, wParam, lParam);
  84. end;
  85. WM_CLOSE:
  86. result := DefWindowProc(Wnd, message, wParam, lParam);
  87. else
  88. result := DefWindowProc(Wnd, message, wParam, lParam);
  89. end;
  90. end;
  91. { TCustomClient }
  92. constructor TCustomClient.Create;
  93. begin
  94. inherited;
  95. FLifeSpan := TCustomLifeSpan.Create;
  96. end;
  97. function TCustomClient.GetLifeSpanHandler: ICefLifeSpanHandler;
  98. begin
  99. Result := FLifeSpan;
  100. end;
  101. { TCustomLifeSpan }
  102. procedure TCustomLifeSpan.OnAfterCreated(const browser: ICefBrowser);
  103. begin
  104. if not browser.IsPopup then
  105. begin
  106. // get the first browser
  107. brows := browser;
  108. browserId := brows.Identifier;
  109. end;
  110. end;
  111. procedure TCustomLifeSpan.OnBeforeClose(const browser: ICefBrowser);
  112. begin
  113. if browser.Identifier = browserId then
  114. brows := nil;
  115. end;
  116. var
  117. wndClass : TWndClass;
  118. begin
  119. // multi process
  120. CefSingleProcess := False;
  121. if not CefLoadLibDefault then Exit;
  122. try
  123. wndClass.style := CS_HREDRAW or CS_VREDRAW;
  124. wndClass.lpfnWndProc := @CefWndProc;
  125. wndClass.cbClsExtra := 0;
  126. wndClass.cbWndExtra := 0;
  127. wndClass.hInstance := hInstance;
  128. wndClass.hIcon := LoadIcon(0, IDI_APPLICATION);
  129. wndClass.hCursor := LoadCursor(0, IDC_ARROW);
  130. wndClass.hbrBackground := 0;
  131. wndClass.lpszMenuName := nil;
  132. wndClass.lpszClassName := 'cefapp';
  133. RegisterClass(wndClass);
  134. Window := CreateWindow(
  135. 'cefapp',
  136. 'CEF 3 Application',
  137. WS_OVERLAPPEDWINDOW or WS_CLIPCHILDREN,
  138. Integer(CW_USEDEFAULT),
  139. Integer(CW_USEDEFAULT),
  140. Integer(CW_USEDEFAULT),
  141. Integer(CW_USEDEFAULT),
  142. 0,
  143. 0,
  144. HInstance,
  145. nil);
  146. ShowWindow(Window, SW_SHOW);
  147. UpdateWindow(Window);
  148. CefRunMessageLoop;
  149. finally
  150. handl := nil;
  151. end;
  152. end.