Get Response Text from TEdgeBrowser - delphi

How To Get Response Text from TEdgeBrowser?
I call TEdgeBrowser on new form
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
Brws: TEdgeBrowser;
begin
Form := TForm.Create(Self);
Form.Width := 700;
Form.Height := 700;
Form.BorderStyle := bsDialog;
Form.Position := poScreenCenter;
Form.Caption := 'Google';
Form.OnClose := BrowserFormClosed;
Brws := TEdgeBrowser.Create(Form);
TWinControl(Brws).Parent := Form;
Brws.Align := alClient;
Form.Show;
Brws.Navigate('http://google.com');
end;
thi code browser from closed
procedure TForm1.BrowserFormClosed(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
end;

Related

Create Form and WebBrowser at runtime

I'm using Delphi 7 and trying to create a WebBrowser inside a Form, both at run time, but can't make it work. Here is the code:
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
Brws: TWebBrowser;
begin
Form := TForm.Create(nil);
try
Form.Width := 500;
Form.Height := 500;
Form.BorderStyle := bsDialog;
Form.Position := poScreenCenter;
Form.Caption := 'Select the Option';
Brws := TWebBrowser.Create(Form);
Brws.ParentWindow := Form.Handle;
TWinControl(Brws).Parent := Form;
Brws.Align := alClient;
Brws.AddressBar := False;
Brws.MenuBar := False;
Brws.StatusBar := False;
Application.ProcessMessages;
if Form.ShowModal = mrOk then
Brws.Navigate('https://www.google.com');
finally
Form.Free;
end;
end;
The result is like WebBrowser is not responding. I got a white screen and no error messages.
Please, what am I missing? Thanks!
You are displaying the Form using its ShowModal() method, which is a synchronous (aka blocking) function that does not exit until the Form is closed. So, you are never reaching the call to Navigate() while the Form is open.
You have two options:
Use Show() instead of ShowModal(). Show() signals the Form to display itself, and then exits immediately, allowing subsequent code to run while the Form is open. As such, you will have to get rid of the try...finally and instead use the Form's OnClose event to free the Form when it is closed, eg:
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
Brws: TWebBrowser;
begin
Form := TForm.Create(Self);
Form.Width := 500;
Form.Height := 500;
Form.BorderStyle := bsDialog;
Form.Position := poScreenCenter;
Form.Caption := 'Select the Option';
Form.OnClose := BrowserFormClosed;
Brws := TWebBrowser.Create(Form);
TWinControl(Brws).Parent := Form;
Brws.Align := alClient;
Brws.AddressBar := False;
Brws.MenuBar := False;
Brws.StatusBar := False;
Form.Show;
Brws.Navigate('https://www.google.com');
end;
procedure TForm1.BrowserFormClosed(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
end;
Otherwise, if you want to keep using ShowModal() then move the call to Navigate() into the Form's OnShow or OnActivate event instead, eg:
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
Brws: TWebBrowser;
begin
Form := TForm.Create(nil);
try
Form.Width := 500;
Form.Height := 500;
Form.BorderStyle := bsDialog;
Form.Position := poScreenCenter;
Form.Caption := 'Select the Option';
Form.OnShow := BrowserFormShown;
Brws := TWebBrowser.Create(Form);
TWinControl(Brws).Parent := Form;
Brws.Align := alClient;
Brws.AddressBar := False;
Brws.MenuBar := False;
Brws.StatusBar := False;
Form.ShowModal;
finally
Form.Free;
end;
end;
procedure TForm1.BrowserFormShown(Sender: TObject);
var
Form: TForm;
Brws: TWebBrowser;
begin
Form := TForm(Sender);
Brws := TWebBrowser(Form.Components[0]);
Brws.Navigate('https://www.google.com');
end;

Delphi: How do you control multiple alike objects?

Say that I have five TRectangle objects, and a function is going to pass a parameter in to make one of them blink.
I know how to control one object like the following code:
procedure TForm1.TimerTimer(Sender: TObject);
begin
if rect1.Visible then
rect1.Visible := false
else
rect1.Visible := true;
end;
procedure TForm1.Blink_Square;
begin
Timer := TTimer.Create(nil);
Timer.OnTimer := TimerTimer;
rect1.Fill.Color := TAlphacolors.Red;
rect1.fill.Kind := TBrushKind.bkSolid;
rect1.Stroke.Thickness := 1;
rect1.Stroke.Color := Talphacolors.Darkgray;
Timer.Interval := 500;
Timer.Enabled := True;
end;
But I really wonder if there is a way that I can use the blink square repeatedly like having a procedure as procedure TForm1.Blink_Square(rec_number: integer); And we can call Blink_Square(5); to make rect5 blink.
Thanks in Advance
You can store your objects in an array or list, then use your procedure parameter to index into it.
var
Blinks: array[1..5] of record
Rectangle: TRectangle;
Timer: TTimer;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Blinks[1].Rectangle := Rect1;
Blinks[1].Timer := nil;
Blinks[2].Rectangle := Rect2;
Blinks[2].Timer := nil;
Blinks[3].Rectangle := Rect3;
Blinks[3].Timer := nil;
Blinks[4].Rectangle := Rect4;
Blinks[4].Timer := nil;
Blinks[5].Rectangle := Rect5;
Blinks[5].Timer := nil;
end;
procedure TForm1.TimerTimer(Sender: TObject);
var
Timer: TTimer;
begin
Timer := TTimer(Sender);
Blinks[Timer.Tag].Visible := not Blinks[Timer.Tag].Visible;
end;
procedure TForm1.Blink_Square(Number: Integer);
begin
Blinks[Number].Rectangle.Fill.Color := TAlphacolors.Red;
Blinks[Number].Rectangle.fill.Kind := TBrushKind.bkSolid;
Blinks[Number].Rectangle.Stroke.Thickness := 1;
Blinks[Number].Rectangle.Stroke.Color := Talphacolors.Darkgray;
if Blinks[Number].Timer = nil then
begin
Blinks[Number].Timer := TTimer.Create(Self);
Blinks[Number].Timer.OnTimer := TimerTimer;
Blinks[Number].Timer.Interval := 500;
Blinks[Number].Timer.Tag := Number;
Blinks[Number].Timer.Enabled := True;
end;
end;
Alternatively:
var
Rects: array[1..5] of TRectangle;
procedure TForm1.FormCreate(Sender: TObject);
begin
Rects[1] := Rect1;
Rects[2] := Rect2;
Rects[3] := Rect3;
Rects[4] := Rect4;
Rects[5] := Rect5;
end;
procedure TForm1.TimerTimer(Sender: TObject);
begin
TRectangle(Sender).Visible := not TRectangle(Sender).Visible;
end;
procedure TForm1.Blink_Square(Number: Integer);
var
Rec: TRectangle;
Timer: TTimer;
M: TNotifyEvent;
begin
Rec := Rects[Number];
Rec.Fill.Color := TAlphacolors.Red;
Rec.fill.Kind := TBrushKind.bkSolid;
Rec.Stroke.Thickness := 1;
Rec.Stroke.Color := Talphacolors.Darkgray;
if Rec.Tag = 0 then
begin
M := TimerTimer;
TMethod(M).Data := Rec;
Timer := TTimer.Create(Rec);
Timer.OnTimer := M;
Timer.Interval := 500;
Timer.Enabled := True;
Rec.Tag := NativeInt(Timer);
end;
end;

How to show dialog box with two buttons ( Continue / Close ) in Delphi

I want to create a warning dialog box which asks the users if the information typed during signup was correct, and asks him wether he want to continue or close that dialog and correct his information.
var
td: TTaskDialog;
tb: TTaskDialogBaseButtonItem;
begin
td := TTaskDialog.Create(nil);
try
td.Caption := 'Warning';
td.Text := 'Continue or Close?';
td.MainIcon := tdiWarning;
td.CommonButtons := [];
tb := td.Buttons.Add;
tb.Caption := 'Continue';
tb.ModalResult := 100;
tb := td.Buttons.Add;
tb.Caption := 'Close';
tb.ModalResult := 101;
td.Execute;
if td.ModalResult = 100 then
ShowMessage('Continue')
else if td.ModalResult = 101 then
ShowMessage('Close');
finally
td.Free;
end;
end;
Note: This will only work on Windows Vista or later.
if delphi then
if mrYes=MessageDlg('Continue?',mtwarning,[mbYes, mbNo],0) then
begin
//do somthing
end
else
exit; //go out
var
AMsgDialog: TForm;
abutton: TButton;
bbutton: TButton;
begin
AMsgDialog := CreateMessageDialog('This is a test message.', mtWarning,[]);
abutton := TButton.Create(AMsgDialog);
bbutton := TButton.Create(AMsgDialog);
with AMsgDialog do
try
Caption := 'Dialog Title' ;
Height := 140;
AMsgDialog.Width := 260 ;
with abutton do
begin
Parent := AMsgDialog;
Caption := 'Continue';
Top := 67;
Left := 60;
// OnClick :tnotyfievent ;
end;
with bbutton do
begin
Parent := AMsgDialog;
Caption := 'Close';
Top := 67;
Left := 140;
//OnClick :tnotyfievent ;
end;
ShowModal ;
finally
abutton.Free;
bbutton.Free;
Free;
end;
Based on this:
procedure HookResourceString(rs: PResStringRec; newStr: PChar);
var
oldprotect: DWORD;
begin
VirtualProtect(rs, SizeOf(rs^), PAGE_EXECUTE_READWRITE, #oldProtect);
rs^.Identifier := Integer(newStr);
VirtualProtect(rs, SizeOf(rs^), oldProtect, #oldProtect);
end;
const
SContinue = 'Continue';
SClose = 'Close';
procedure TForm1.Button1Click(Sender: TObject);
begin
HookResourceString(#SMsgDlgOK, SContinue);
HookResourceString(#SMsgDlgCancel, SClose);
if MessageDlg('My Message', mtConfirmation, [mbOK, mbCancel], 0) = mrOK then
begin
// OK...
end;
end;

Is it possible to dynamically create form without having *.dfm and *.pas files?

is it possible to create and show TForm without having source files for it ?
I want to create my forms at runtime and having the empty *.dfm and *.pas files seems to me useless.
Thank you
Do you mean like this?
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
Lbl: TLabel;
Btn: TButton;
begin
Form := TForm.Create(nil);
try
Form.BorderStyle := bsDialog;
Form.Caption := 'My Dynamic Form!';
Form.Position := poScreenCenter;
Form.ClientWidth := 400;
Form.ClientHeight := 200;
Lbl := TLabel.Create(Form);
Lbl.Parent := Form;
Lbl.Caption := 'Hello World!';
Lbl.Top := 10;
Lbl.Left := 10;
Lbl.Font.Size := 24;
Btn := TButton.Create(Form);
Btn.Parent := Form;
Btn.Caption := 'Close';
Btn.ModalResult := mrClose;
Btn.Left := Form.ClientWidth - Btn.Width - 16;
Btn.Top := Form.ClientHeight - Btn.Height - 16;
Form.ShowModal;
finally
Form.Free;
end;
end;
Yes, it is possible:
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
begin
Form:= TForm.Create(Self);
try
Form.ShowModal;
finally
Form.Free;
end;
end;

How do I conceal the password text in an edit box?

I have an inputbox and would like the user to enter a password, but at the same time hide it.
Is this possible?
This is my code so far:
var password : string;
begin
password := InputBox('Password: ', 'Please enter your password: ', password)
end;
You 'cannot' use InputBox for this, because, well... clearly this function doesn't hide the text.
The standard Windows edit control has a 'password mode', though. To test this, simply add a TEdit to a form and set its PasswordChar to *.
If you want to use such an edit in an input box, you have to write this dialog yourself, like my 'super input dialog':
type
TMultiInputBox = class
strict private
class var
frm: TForm;
lbl: TLabel;
edt: TEdit;
btnOK,
btnCancel: TButton;
shp: TShape;
FMin, FMax: integer;
FTitle, FText: string;
class procedure SetupDialog;
class procedure ValidateInput(Sender: TObject);
public
class function TextInputBox(AOwner: TCustomForm; const ATitle,
AText: string; var Value: string): boolean;
class function NumInputBox(AOwner: TCustomForm; const ATitle,
AText: string; AMin, AMax: integer; var Value: integer): boolean;
class function PasswordInputBox(AOwner: TCustomForm; const ATitle,
AText: string; var Value: string): boolean;
end;
class procedure TMultiInputBox.SetupDialog;
begin
frm.Caption := FTitle;
frm.Width := 512;
frm.Position := poOwnerFormCenter;
frm.BorderStyle := bsDialog;
lbl := TLabel.Create(frm);
lbl.Parent := frm;
lbl.Left := 8;
lbl.Top := 8;
lbl.Width := frm.ClientWidth - 16;
lbl.Caption := FText;
edt := TEdit.Create(frm);
edt.Parent := frm;
edt.Top := lbl.Top + lbl.Height + 8;
edt.Left := 8;
edt.Width := frm.ClientWidth - 16;
btnOK := TButton.Create(frm);
btnOK.Parent := frm;
btnOK.Default := true;
btnOK.Caption := 'OK';
btnOK.ModalResult := mrOk;
btnCancel := TButton.Create(frm);
btnCancel.Parent := frm;
btnCancel.Cancel := true;
btnCancel.Caption := 'Cancel';
btnCancel.ModalResult := mrCancel;
btnCancel.Top := edt.Top + edt.Height + 16;
btnCancel.Left := frm.ClientWidth - btnCancel.Width - 8;
btnOK.Top := btnCancel.Top;
btnOK.Left := btnCancel.Left - btnOK.Width - 4;
frm.ClientHeight := btnOK.Top + btnOK.Height + 8;
shp := TShape.Create(frm);
shp.Parent := frm;
shp.Brush.Color := clWhite;
shp.Pen.Style := psClear;
shp.Shape := stRectangle;
shp.Align := alTop;
shp.Height := btnOK.Top - 8;
shp.SendToBack;
end;
class function TMultiInputBox.TextInputBox(AOwner: TCustomForm; const ATitle,
AText: string; var Value: string): boolean;
begin
FTitle := ATitle;
FText := AText;
frm := TForm.Create(AOwner);
try
SetupDialog;
edt.NumbersOnly := false;
edt.PasswordChar := #0;
edt.Text := Value;
edt.OnChange := nil;
result := frm.ShowModal = mrOK;
if result then Value := edt.Text;
finally
frm.Free;
end;
end;
class function TMultiInputBox.PasswordInputBox(AOwner: TCustomForm;
const ATitle, AText: string; var Value: string): boolean;
begin
FTitle := ATitle;
FText := AText;
frm := TForm.Create(AOwner);
try
SetupDialog;
edt.NumbersOnly := false;
edt.PasswordChar := '*';
edt.Text := Value;
edt.OnChange := nil;
result := frm.ShowModal = mrOK;
if result then Value := edt.Text;
finally
frm.Free;
end;
end;
class procedure TMultiInputBox.ValidateInput(Sender: TObject);
var
n: integer;
begin
btnOK.Enabled := TryStrToInt(edt.Text, n) and InRange(n, FMin, FMax);
end;
class function TMultiInputBox.NumInputBox(AOwner: TCustomForm; const ATitle,
AText: string; AMin, AMax: integer; var Value: integer): boolean;
begin
FMin := AMin;
FMax := AMax;
FTitle := ATitle;
FText := AText;
frm := TForm.Create(AOwner);
try
SetupDialog;
edt.NumbersOnly := true;
edt.PasswordChar := #0;
edt.Text := IntToStr(value);
edt.OnChange := ValidateInput;
result := frm.ShowModal = mrOK;
if result then Value := StrToInt(edt.Text);
finally
frm.Free;
end;
end;
Try it:
procedure TForm1.Button1Click(Sender: TObject);
var
str: string;
begin
str := '';
if TMultiInputBox.PasswordInputBox(Self, 'Password',
'Please enter your password:', str) then
ShowMessageFmt('You entered %s.', [str]);
end;
This looks like it was answered here:
Delphi InputBox for password entry?
Don't use an InputBox. Create a dialog yourself and make sure to set TEdit.PasswordChar to something other than #0.
It may also be possible to get a handle to the InputBox's Edit control and set the PasswordChar via a Windows message, but I don't know how to do that off the top of my head (especially since the InputBox is a blocking call).
Delphi XE also has a Password Dialog form available to use when creating a new form. Older versions probably do too, XE just happens to be what I have running right now. (Edit Delphi 2007 also has it. 2007 & XE are the only versions of Delphi I have installed right now though, so I can't verify any other versions.)
const
InputBoxMessage = WM_USER + 200;
type
TForm1 = class(TForm)
...
procedure InputBoxSetPasswordChar(var Msg: TMessage); message InputBoxMessage;
function GetPassword: String;
...
end;
...
procedure TForm1.InputBoxSetPasswordChar(var Msg: TMessage);
var
hInputForm, hEdit: HWND;
begin
hInputForm := Screen.Forms[0].Handle;
if (hInputForm <> 0) then
begin
hEdit := FindWindowEx(hInputForm, 0, 'TEdit', nil);
SendMessage(hEdit, EM_SETPASSWORDCHAR, Ord('*'), 0);
end;
end;
function TForm1.GetPassword: String;
begin
PostMessage(Handle, InputBoxMessage, 0, 0);
Result := InputBox('Title', 'Password:', '');
end;
I think you also need to set:
Echomode := eemPassword
At least for TdlcxLabeledDBTextEdit.
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if checkbox1.checked = true then
edit1.passwordchar := '*'
else
edit1.PasswordChar := #0;
end;
end;

Resources