1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2025-01-13 10:22:04 +02:00

- add Open Jpeg functionality

This commit is contained in:
Greg 2019-11-02 20:51:21 +01:00
parent 8958b09b68
commit 5e6c32b0ee
5 changed files with 204 additions and 58 deletions

View File

@ -31,7 +31,7 @@
display: flex;
flex-direction: column;
height: calc(100vh - 2em);
width: 100vw;
width: 100vw;
}
#appWinIcons {
@ -94,20 +94,20 @@
border: 2px solid #aaa;
color: #aaa;
border-radius: 5px;
width:100px;
margin:20px;
width: 100px;
margin: 20px;
outline: none;
}
#openJpegButton:hover {
border: 2px solid #fff;
}
#openJpegButton:hover {
border: 2px solid #fff;
}
#canvas {
width: 720px;
height: 405px;
background: black;
margin:20px;
margin: 20px;
}
</style>
</head>
@ -120,17 +120,32 @@
<div id="winMin" class="winIco"></div>
<div id="winMax" class="winIco"></div>
<div id="winCls" class="winIco">&times;</div>
</div>
</div>
</div>
<div class="form">
<button id="openJpegButton">Open JPEG</button>
<canvas id="canvas"></canvas>
</div>
<script>
let img = new Image();
let canvas = document.getElementById("canvas");
canvas.width = 720;
canvas.height = 405;
let ctx = canvas.getContext('2d');
img.onload = () => {
ctx.drawImage(img, 0, 0, 720, 405);
}
function setAppCaption(caption) {
document.getElementById("title").innerHTML = caption;
}
document.getElementById('openJpegButton').addEventListener('mousedown', (e) => {
if (e.button == 0) myextension.openjpeg();
})
document.getElementById('winMin').addEventListener('mousedown', (e) => {
if (e.button == 0) myextension.minimize();
})

View File

@ -230,6 +230,16 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="UWP_DelphiLogo150">
<Platform Name="Win64">
<RemoteDir>Assets</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Win32">
<RemoteDir>Assets</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_LauncherIcon96">
<Platform Name="Android">
<RemoteDir>res\drawable-xhdpi</RemoteDir>
@ -260,16 +270,6 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="UWP_DelphiLogo150">
<Platform Name="Win64">
<RemoteDir>Assets</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Win32">
<RemoteDir>Assets</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="DebugSymbols">
<Platform Name="OSX32">
<RemoteDir>Contents\MacOS</RemoteDir>
@ -328,13 +328,13 @@
</Platform>
</DeployClass>
<DeployClass Name="ProjectiOSInfoPList">
<Platform Name="iOSDevice32">
<Platform Name="iOSDevice64">
<Operation>1</Operation>
</Platform>
<Platform Name="iOSSimulator">
<Operation>1</Operation>
</Platform>
<Platform Name="iOSDevice64">
<Platform Name="iOSDevice32">
<Operation>1</Operation>
</Platform>
</DeployClass>
@ -567,7 +567,7 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="OSX32" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Android" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSDevice" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Win32" Name="$(PROJECTNAME)"/>
@ -576,7 +576,7 @@
<ProjectRoot Platform="Linux64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSSimulator" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="OSX32" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/>
</Deployment>
<Platforms>
<Platform value="Win32">True</Platform>

View File

@ -2,7 +2,7 @@ object CTBForm: TCTBForm
Left = 0
Top = 0
BorderStyle = bsNone
Caption = 'CTBForm'
Caption = 'CEF4 Custom Title Bar'
ClientHeight = 628
ClientWidth = 994
Color = clBtnFace
@ -18,25 +18,13 @@ object CTBForm: TCTBForm
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object StatusBar1: TStatusBar
Left = 0
Top = 609
Width = 994
Height = 19
Panels = <
item
Width = 1000
end>
ExplicitTop = 570
ExplicitWidth = 978
end
object CEFWindowParent1: TCEFWindowParent
Left = 0
Top = 0
Width = 994
Height = 609
Height = 628
Align = alClient
TabOrder = 1
TabOrder = 0
ExplicitTop = 30
ExplicitWidth = 978
ExplicitHeight = 540
@ -66,6 +54,7 @@ object CTBForm: TCTBForm
Top = 352
end
object Timer2: TTimer
Enabled = False
Interval = 1
OnTimer = Timer2Timer
Left = 624

View File

@ -57,18 +57,21 @@ uses
const
MINIBROWSER_SHOWTEXTVIEWER = WM_APP + $100;
MINIBROWSER_JSBINPARAM = WM_APP + $103;
MINIBROWSER_CONTEXTMENU_SETJSEVENT = MENU_ID_USER_FIRST + 1;
MINIBROWSER_CONTEXTMENU_JSVISITDOM = MENU_ID_USER_FIRST + 2;
MINIBROWSER_CONTEXTMENU_MUTATIONOBSERVER = MENU_ID_USER_FIRST + 3;
MINIBROWSER_CONTEXTMENU_SHOWDEVTOOLS = MENU_ID_USER_FIRST + 4;
MOUSEOVER_MESSAGE_NAME = 'mouseover';
CUSTOMNAME_MESSAGE_NAME = 'customname';
MOUSEOVER_MESSAGE_NAME = 'mousestate';
WINDOW_MINIMIZE_MESSAGE = 'minimize';
WINDOW_MAXIMIZE_MESSAGE = 'maximize';
WINDOW_CLOSE_MESSAGE = 'close';
BINARY_PARAM_JS = 'JSBinaryParameter';
type
TCTBForm = class(TForm)
StatusBar1: TStatusBar;
CEFWindowParent1: TCEFWindowParent;
Chromium1: TChromium;
Timer1: TTimer;
@ -124,6 +127,8 @@ type
procedure BrowserDestroyMsg(var aMessage: TMessage); message CEF_DESTROY;
procedure ShowTextViewerMsg(var aMessage: TMessage);
message MINIBROWSER_SHOWTEXTVIEWER;
procedure EvalJSBinParamMsg(var aMessage: TMessage);
message MINIBROWSER_JSBINPARAM;
procedure WMMove(var aMessage: TWMMove); message WM_MOVE;
procedure WMMoving(var aMessage: TMessage); message WM_MOVING;
public
@ -140,7 +145,8 @@ implementation
{$R *.dfm}
uses
uCEFv8Handler, uTestExtension, uCEFMiscFunctions;
uCefBinaryValue, uCefProcessMessage, uCEFv8Handler, uTestExtension,
uCEFMiscFunctions;
// Please, read the code comments in the JSExtension demo (uJSExtension.pas) before using this demo!
@ -210,7 +216,7 @@ procedure TCTBForm.Chromium1AfterCreated(Sender: TObject;
const browser: ICefBrowser);
begin
PostMessage(Handle, CEF_AFTERCREATED, 0, 0);
CTBForm.executeJS(Chromium1.Browser.MainFrame);
CTBForm.executeJS(Chromium1.browser.MainFrame);
end;
procedure TCTBForm.Chromium1BeforeContextMenu(Sender: TObject;
@ -245,11 +251,10 @@ begin
if (frame <> nil) and frame.IsValid then
begin
TempJSCode := 'document.body.addEventListener("mousedown", function(evt){' +
'myextension.mousestate(getComputedStyle(evt.target).webkitAppRegion)'
+ '});'+chr(13);
'myextension.mousestate(getComputedStyle(evt.target).webkitAppRegion)' +
'});' + chr(13);
TempJSCode := TempJSCode +
' setAppCaption("'+CTBForm.caption +'");' ;
TempJSCode := TempJSCode + ' setAppCaption("' + CTBForm.caption + '");';
frame.ExecuteJavaScript(TempJSCode, 'about:blank', 0);
end;
@ -283,10 +288,63 @@ begin
end;
end;
procedure TCTBForm.EvalJSBinParamMsg(var aMessage: TMessage);
var
TempMsg: ICefProcessMessage;
TempOpenDialog: TOpenDialog;
TempStream: TFileStream;
TempBinValue: ICefBinaryValue;
TempBuffer: TBytes;
TempSize: NativeUInt;
TempPointer: pointer;
begin
TempOpenDialog := nil;
TempStream := nil;
try
try
TempOpenDialog := TOpenDialog.Create(nil);
TempOpenDialog.Filter := 'JPEG files (*.jpg)|*.JPG';
if TempOpenDialog.Execute then
begin
TempStream := TFileStream.Create(TempOpenDialog.FileName, fmOpenRead);
TempSize := TempStream.Size;
if (TempSize > 0) then
begin
SetLength(TempBuffer, TempSize);
TempSize := TempStream.Read(TempBuffer, TempSize);
if (TempSize > 0) then
begin
TempPointer := @TempBuffer[0];
TempBinValue := TCefBinaryValueRef.New(TempPointer, TempSize);
TempMsg := TCefProcessMessageRef.New(BINARY_PARAM_JS);
if TempMsg.ArgumentList.SetBinary(0, TempBinValue) then
Chromium1.SendProcessMessage(PID_RENDERER, TempMsg);
end;
end;
end;
except
on e: exception do
if CustomExceptionHandler('TCTBForm.EvalJSBinParamMsg', e) then
raise;
end;
finally
if (TempOpenDialog <> nil) then
FreeAndNil(TempOpenDialog);
if (TempStream <> nil) then
FreeAndNil(TempStream);
SetLength(TempBuffer, 0);
end;
end;
procedure TCTBForm.Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser;
const frame: ICefFrame; httpStatusCode: Integer);
begin
CTBForm.executeJS(Chromium1.Browser.MainFrame);
CTBForm.executeJS(Chromium1.browser.MainFrame);
end;
procedure TCTBForm.Chromium1ProcessMessageReceived(Sender: TObject;
@ -310,7 +368,6 @@ begin
if (message.Name = MOUSEOVER_MESSAGE_NAME) then
begin
StatusBar1.Panels[0].Text := message.ArgumentList.GetString(0);
tp := Mouse.CursorPos;
tp := CTBForm.ScreenToClient(tp);
@ -318,16 +375,36 @@ begin
if message.ArgumentList.GetString(0) = 'drag' then
begin
mouseDrag := True;
Timer2.Enabled := True;
end;
Result := True;
end
end;
if (message.Name = WINDOW_MINIMIZE_MESSAGE) then
begin
CTBForm.WindowState := wsMinimized;
end;
if (message.Name = WINDOW_MAXIMIZE_MESSAGE) then
begin
if CTBForm.WindowState = wsNormal then
begin
CTBForm.WindowState := wsMaximized;
end
else if CTBForm.WindowState = wsMaximized then
CTBForm.WindowState := wsNormal;
end;
if (message.Name = WINDOW_CLOSE_MESSAGE) then
begin
CTBForm.close;
end;
end;
procedure TCTBForm.FormShow(Sender: TObject);
begin
StatusBar1.Panels[0].Text := 'Initializing browser. Please wait...';
Chromium1.DefaultURL := 'file:///app_view.html';
@ -356,9 +433,7 @@ end;
procedure TCTBForm.ShowTextViewerMsg(var aMessage: TMessage);
begin
// This form will show the HTML received from JavaScript
// SimpleTextViewerFrm.Memo1.Lines.Text := FText;
// SimpleTextViewerFrm.ShowModal;
end;
procedure TCTBForm.Timer1Timer(Sender: TObject);
@ -366,14 +441,14 @@ begin
Timer1.Enabled := False;
if not(Chromium1.CreateBrowser(CEFWindowParent1, '')) and
not(Chromium1.Initialized) then
begin
begin
Timer1.Enabled := True;
end;
end;
end;
procedure TCTBForm.BrowserCreatedMsg(var aMessage: TMessage);
begin
StatusBar1.Panels[0].Text := '';
CEFWindowParent1.UpdateSize;
end;
@ -430,7 +505,10 @@ begin
MouseLBtnDown := (GetKeyState(VK_LBUTTON) < 0);
if not MouseLBtnDown then
begin
mouseDrag := False;
Timer2.Enabled := False;
end;
if mouseDrag then
begin
@ -438,8 +516,6 @@ begin
CTBForm.top := tpc.y - tp.y;
end;
// StatusBar1.Panels[0].Text := IntToStr(tp.x) + ' ' + IntToStr(tp.y) + ' ' +
// BoolToStr(MouseLBtnDown) + ' ' + BoolToStr(mouseDrag);
end;
end.

View File

@ -53,6 +53,9 @@ uses
type
TTestExtension = class
class procedure mousestate(const data: string);
class procedure minimize();
class procedure maximize();
class procedure close();
end;
implementation
@ -81,5 +84,68 @@ begin
end;
end;
class procedure TTestExtension.minimize();
var
TempMessage : ICefProcessMessage;
TempFrame : ICefFrame;
begin
try
TempMessage := TCefProcessMessageRef.New(WINDOW_MINIMIZE_MESSAGE);
// Sending a message back to the browser. It'll be received in the TChromium.OnProcessMessageReceived event.
// TCefv8ContextRef.Current returns the v8 context for the frame that is currently executing Javascript.
TempFrame := TCefv8ContextRef.Current.Browser.MainFrame;
if (TempFrame <> nil) and TempFrame.IsValid then
TempFrame.SendProcessMessage(PID_BROWSER, TempMessage);
finally
TempMessage := nil;
end;
end;
class procedure TTestExtension.maximize();
var
TempMessage : ICefProcessMessage;
TempFrame : ICefFrame;
begin
try
TempMessage := TCefProcessMessageRef.New(WINDOW_MAXIMIZE_MESSAGE);
// Sending a message back to the browser. It'll be received in the TChromium.OnProcessMessageReceived event.
// TCefv8ContextRef.Current returns the v8 context for the frame that is currently executing Javascript.
TempFrame := TCefv8ContextRef.Current.Browser.MainFrame;
if (TempFrame <> nil) and TempFrame.IsValid then
TempFrame.SendProcessMessage(PID_BROWSER, TempMessage);
finally
TempMessage := nil;
end;
end;
class procedure TTestExtension.close();
var
TempMessage : ICefProcessMessage;
TempFrame : ICefFrame;
begin
try
TempMessage := TCefProcessMessageRef.New(WINDOW_CLOSE_MESSAGE);
// Sending a message back to the browser. It'll be received in the TChromium.OnProcessMessageReceived event.
// TCefv8ContextRef.Current returns the v8 context for the frame that is currently executing Javascript.
TempFrame := TCefv8ContextRef.Current.Browser.MainFrame;
if (TempFrame <> nil) and TempFrame.IsValid then
TempFrame.SendProcessMessage(PID_BROWSER, TempMessage);
finally
TempMessage := nil;
end;
end;
end.