I need that "x" button on any form would not close the form but instead open another 3 random forms on delphi, i have no idea how to do that, please help
Just use the form's OnCloseQuery event to detect the user's trying to close your form (by clicking the close button in the top-right corner, by double-clicking the form's title bar icon, by selecting the Close system menu item, by pressing Alt+F4, etc.).
Then set CanClose to False and instead open your three new forms:
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := False;
Form2.Show;
Form3.Show;
Form4.Show;
end;
As suggested by #AndreasRejbrand's answer, you could use the Form's OnCloseQuery event. But, the problem with that approach is that the event is also triggered during system reboot/shutdown, and you don't want to block that. If OnCloseQuery returns CanClose=False during a system shutdown, the shutdown is canceled.
Another option is to use the Form's OnClose event instead, setting its Action parameter to caNone, eg:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone;
Form2.Show;
Form3.Show;
Form4.Show;
end;
However, the best option would to be to handle only user-initiated closures (the X button, ALT-F4, etc) by having the Form handle the WM_SYSCOMMAND message looking for SC_CLOSE notifications, eg:
procedure TForm1.WndProc(var Message: TMessage);
begin
if (Message.Msg = WM_SYSCOMMAND) and (Message.WParam and $FFF0 = SC_CLOSE) then
begin
Message.Result := 0;
Form2.Show;
Form3.Show;
Form4.Show;
end
else
inherited;
end;
This way, system-initiated closures are unhindered.
Related
I have a DLL application that is loaded in my main application.
The DLL contains a form that is created at runtime.
The functionality is:
In main application I have a menu which whenever pressed calls a procedure from within the DLL. This procedure dynamically creates the form.
procedure doCreateForm;
var
myForm: TForm1;
begin
myForm := TForm1.Create(nil)
try
...
except
myForm.Free;
end;
end;
The closing procedures:
procedure CloseWindow(ASender: TForm1);
begin
FreeAndNil(ASender);
Application.ProcessMessages;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
CloseWindow(Self);
end;
The problem (access violation) occurs only on the second attempt to create the form. Not first time, not 3rd, 4th, 5th and so on.
So I click on the menu, the form is created (dynamically) and closed (if condition is not satisfied during form create event). I click again on the menu, and when myForm.Create(nil) is called, AV raises. I click again on the menu, and all is ok. I click again and again and again and all is ok. Only when pressed the 2nd time, AV raises.
Is there something wrong with dynamic creation of visual form in DLL?
A more detailed explanation:
The chain is:
I create MyForm (myForm := TForm1.Create(nil))
Prior to showing the form I do some conditioning tests.
If all is ok, myForm.Show - this is working fine and I can also close myForm properly
If something is wrong:
a). I create a message form myMessageForm := TMyMessageForm.Create(nil) that contains a closing timer (the form closes after 10s). This form has action:=caFree in onClose event
b). I call myForm.Close. This form has also action:=caFree in onClose event - this form closes before myMessageForm closes (due to the timer present in myMessageForm)
Both forms are created with nil owner, but they are connected in some way (I don't know why). and the destruction of forms is not performed correctly. The next time myForm.Create(nil) or myMessageForm.Create(nil) is called, access violation occurs.
The myMessageForm should be created independently from myForm and it's destruction should not condition myForm destruction in any way.
unit1;
procedure doCreateForm;
var
myForm: TForm1;
begin
myForm := TForm1.Create(nil)
try
with myForm do
begin
if <test condition true> then Show
else
begin
ShowErrMessage('Error', 'Error message text', errType);
Close;
end;
end;
except
myForm.Free;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
unit2;
procedure ShowErrMessage(title, text: string; err: mtErrType);
var
myMessageForm: TMyMessageForm;
begin
myMessageForm := TMyMessageForm.Create(nil)
try
with myMessageForm do
begin
StepDownCounter := 10;
CloseTimer.Enable := True;
end;
except
myMessageForm.Free;
end;
end;
procedure TMyMessageForm.CloseTimerTimer(Sender: TObject);
begin
StepDownCounter := StepDownCounter - 1;
if (StepDownCounter < 1) then
begin
CloseTimer.Enabled := False;
LabelStepDownText.Visible := False;
Close;
end
else
begin
LabelStepDownText.Caption := 'Window will close in ' + IntToStr(StepDownCounter) + 's';
LabelStepDownText.Visible := True;
end;
end;
procedure TMyMessageForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
You are destroying the object that is executing the current method. All code in that object that is executed after the object is destroyed is invalid.
You should set the close action to caFree, or use Release, as has been explained. These work by posting a message to the queue to allow the object to be destroyed after the current method returns. You are subverting that with the call to ProcessMessages which pumps the queue. Remove the call to ProcessMessages.
I am trying to handle some events when my application is idle so i created this code
procedure TForm1.ApplicationEventIdle(Sender: TObject; var Done: Boolean);
begin
Done := false;
ShowMessage('Hello');
Done := true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnIdle := ApplicationEventIdle;
end;
The problem is the message box appears infinite times how can i display it only once ?
This behaviour happens precisely because you show the dialog. In order to close the dialog you have to click the OK button. This places an input message on the queue. After that has been processed the OnIdle event fires. And you show the message box again. And so on and so on.
You need to make sure that you don't do anything that requires user input in your OnIdle handler. In your case try outputting to a log rather than showing a dialog, e.g. OutputDebugString.
Create a private boolean field in TForm1 to indicate when the dialog has been shown, so you don't show it again.
procedure TForm1.ApplicationEventIdle(Sender: TObject; var Done: Boolean);
begin
if not FDialogShown then
begin
FDialogShown := True;
ShowMessage('Hello');
end;
end;
As I typed this question I realize that it probably should.
Docking a form to a TPageControl calls FormShow when form.Create() is called and when form.ManualDock(pagecontrol,pagecontrol.alClient) is called.
Un-docking the form also calls show and I assume this is because the form is actually 'reset' when you dock/undock?
If this is as designed I'll just refactor the code I dont want to fire there to onCreate (unless that is bad design).
If should or not is more philosophical than technical question. The TForm.OnShow event is fired by performing control message CM_DOCKCLIENT which is used also by the ManualDock function. Internally this message calls the CM_SHOWINGCHANGED what fires the event itself.
In the following example I will use two forms, Form1 (with a page control and a button) and Form2 (empty and dockable). I presume that both are auto created.
The following code is a proof that the OnShow event is fired by the CM_DOCKCLIENT control message. Clicking on the button, the CM_DOCKCLIENT message is performed and Form2's OnShow event is fired.
Code for Form1
procedure TForm1.FormShow(Sender: TObject);
begin
Form2.Show;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
DockObject: TDragDockObject;
begin
DockObject := TDragDockObject.Create(Form2);
try
// sending the CM_DOCKCLIENT message internally performs also the
// CM_SHOWINGCHANGED message which triggers the TForm.OnShow event
PageControl1.Perform(CM_DOCKCLIENT, WPARAM(DockObject), LPARAM(SmallPoint(0, 0)));
finally
DockObject.Free;
end;
end;
And Form2 has only the OnShow event handler
procedure TForm2.FormShow(Sender: TObject);
begin
ShowMessage('FormShow');
end;
An easy workaround is not to dock the Form2 manually by its own (in the OnShow event) but dock it by the creator or let's say by the form which displays it. In my previous example I've displayed the Form2 in the Form1.OnShow event, so I can easily dock it manually there.
procedure TForm1.FormShow(Sender: TObject);
begin
Form2.Show;
Form2.ManualDock(PageControl1);
end;
procedure TForm2.FormShow(Sender: TObject);
begin
// no manual docking of this form by itself
end;
I have added hints to components on my form. When the components receive the focus, I'd like to set the caption of a label component to display the hint.
I have added a TApplicationEvents object and set the OnShowHint event to
procedure TImportFrm.ApplicationEvents1ShowHint(var HintStr: string;
var CanShow: Boolean; var HintInfo: THintInfo);
begin
HelpLbl.Caption := HintStr;
end;
However it seems that the ShowHint event only fires with mouse movements. Is there a way to fire the hint code when components receive focus, without having to implement the OnEnter event for every single component on the form?
Add a handler for TScreen.OnActiveControlChange in your main form's creation, and handle the hints in that event:
type
TForm2=class(TForm)
...
private
procedure ScreenFocusControlChange(Sender: TObject);
end;
implementation
procedure TForm2.FormCreate(Sender: TObject);
begin
Screen.OnActiveControlChange := ScreenFocusControlChange;
end;
procedure TForm2.ScreenFocusControlChange(Sender: TObject);
begin
Label1.Caption := ActiveControl.Hint;
Label1.Update;
end;
Note that Sender won't do you much good; it's always Screen. You can filter (for instance, to only change the Label.Caption for edit controls) by testing the ActiveControl:
if (ActiveControl is TEdit) then
// Update caption of label with ActiveControl.Hint
Note that if you'll need to reassign the event when you show child forms (to an event on that child form), or you'll always be updating the original form's label with the hints. The easiest way to do the reassignment is to give every form an OnActiveControlChange handler, and assign it in the form's OnActivate event and unassign it in the OnDeactivate event:
procedure TForm1.FormActivate(Sender: TObject);
begin
Screen.OnActiveControlChange := Self.ScreenActiveControlChange;
end;
procedure TForm1.FormDeactivate(Sender: TObject);
begin
Screen.OnActiveControlChange := nil;
end;
This will allow you to update controls other than Label1 on each form, and only use the hint changes on forms you want to do so.
A simple solution is to use OnIdle event:
procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
begin
if Assigned(ActiveControl) then
Label1.Caption:= ActiveControl.Hint;
end;
A more advanced solution is to override protected ActiveChanged method of TForm:
type
TForm1 = class(TForm)
...
protected
procedure ActiveChanged; override;
end;
...
procedure TForm1.ActiveChanged;
begin
inherited;
if Assigned(ActiveControl) then
Label1.Caption:= ActiveControl.Hint;
end;
Receiving focus and OnShowHint are quite different events; OnShowHint can be triggered for non-focused control as well.
Why would you need to implement the OnEnter event for every single component? You can create one generic method / event handler like:
procedure TForm1.AnyControlEnter(Sender: TObject);
begin
lbl1.Caption := TControl(Sender).Hint;
end;
and assign it to every component you placed on the form.
You said:
it seems that the ShowHint event only fires with mouse movements
This is a normal behaviour. The problem you have ( it's a guess) is that hints are not fired directly. Don't try to make a workaround, what you try to do with MouseEnter is exactly what is already happening...the only difference is that you'be forget something...
Keep the event ApplicationEvents1ShowHint() as you've initially done but add this in the form constructor event:
Application.HintPause := 1;
And then hints will be displayed (almost) without delay.
I need to somehow implement this in Delphi 2009:
The user clicks on button 2. If the user's very last action was clicking on button 1, then I want to do one thing, but if the user's very last action was anything else, I want to do another thing.
Obviously, I set up a boolean variable: UserClickedOnButton1 and set it to true when button 1 is clicked on, and I test that variable in the OnButtonClick event for Button 2.
My question is how do I set that to false whenever anything else is done by the user before clicking on button 2. (e.g. Mouse press, key press, arrow keys, switch to another program, or anything else).
... or is there a simpler way to do this that I am overlooking.
The code below seems to work (D7), but please check this for your specific situation.
type
TButton = class(StdCtrls.TButton)
private
FClickedLast: Boolean;
FNextButton: TButton;
protected
procedure WndProc(var Message: TMessage); override;
public
procedure Click; override;
property ClickedLast: Boolean read FClickedLast write FClickedLast;
property NextButton: TButton write FNextButton;
end;
TForm1 = class(TForm)
...
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.NextButton := Button2;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Button1.ClickedLast then
Caption := Caption + ' +'
else
Caption := Caption + ' -';
Button1.ClickedLast := False;
end;
{ TButton }
procedure TButton.Click;
begin
inherited Click;
if (FNextButton <> nil) and Focused then
FClickedLast := True;
end;
procedure TButton.WndProc(var Message: TMessage);
begin
if (FNextButton <> nil) and not (csDestroying in ComponentState) then
case Message.Msg of
CM_CANCELMODE,
WM_KEYFIRST..WM_KEYLAST:
FClickedLast := False;
WM_KILLFOCUS:
if TWMKillFocus(Message).FocusedWnd <> FNextButton.Handle then
FClickedLast := False;
end;
inherited WndProc(Message);
end;
Explanation:
CM_CANCELMODE handles mouse clicks anywhere not resulting in changing focus,
WM_KEY* handles all key events, but also switching to another application (there is a WM_SYSKEYDOWN, otherwise WM_KILLFOCUS takes care),
WM_KILLFOCUS handles everything else.
From what I think; It's not really possible unless you're willing to go and track all (or at least all possibly unwanted) of events with logic.
A key-press (Tab?) can still be valid to move on to the next button and click it; a mouse-down event, obviously is good if it's on the second button, otherwise it's not. You'd probably want to check if the 'first button is clicked' before executing a whole bunch of logic to slow down every keypress/mousedown/lostfocus event in your application.
An idea could be to use a timer, but this doesn't prevent the user from 'quickly' doing something else.
Edt1: If all other actions that are 'illegal' are actually doing something, perhaps a lostfocus event on the first button could be a start?