기본 콘텐츠로 건너뛰기

C++ - Form(Delphi, C++ Builder) Birth and Death


Birth : OnCreate -> OnShow -> OnActivate -> OnPaint -> OnResize -> OnPaint ... 
Death :  OnCloseQuery -> OnClose -> OnDeactivate -> OnHide -> OnDestroy

In Windows, most elements of the user interface are windows. In Delphi, every project has at least one window - program's main window. All windows of a Delphi application are based on TForm object.


FORM

Form objects are the basic building blocks of a Delphi application, the actual windows with which a user interacts when they run the application. Forms have their own properties, events, and methods with which you can control their appearance and behavior. 
A form is actually a Delphi component, but unlike other components, a form doesn't appear on the component palette.
We normally create a form object by starting a new application (File | New Application). This newly created form will be, by default, the application's main form - the first form created at runtime.
Note: To add an additional form to Delphi project, we select File|New Form. There are, of course, other ways to add a "new" form to a Delphi project.

BIRTH

OnCreate
The OnCreate event is fired when a TForm is first created, that is, only once. The statement responsible for creating the form is in the project's source (if the form is set to be automatically created by the project). When a form is being created and its Visible property is True, the following events occur in the order listed: OnCreate, OnShow, OnActivate, OnPaint.
You should use the OnCreate event handler to do, for example, initialization chores like allocating string lists.
Any objects created in the OnCreate event should be freed by the OnDestroy event.
 OnCreate -> OnShow -> OnActivate -> OnPaint -> OnResize -> OnPaint ... 
OnShow
This event indicates that the form is being displayed. OnShow is called just before a form becomes visible. Besides main forms, this event happens when we set forms Visible property to True, or call the Show or ShowModal method.
OnActivate
This event is called when the program activates the form - that is, when the form receives the input focus. Use this event to change which control actually gets focus if it is not the one desired.
OnPaint, OnResize
Events like OnPaint and OnResize are always called after the form is initially created, but are also called repeatedly. OnPaint occurs before any controls on the form are painted (use it for special painting on the form).

LIFE

As we have seen the birth of a form is not so interesting as the life and death can be. When your form is created and all the controls are waiting for events to handle, the program is running until someone tries to close the form!

DEATH

An event-driven application stops running when all its forms are closed and no code is executing. If a hidden form still exists when the last visible form is closed, your application will appear to have ended (because no forms are visible), but will in fact continue to run until all the hidden forms are closed. Just think of a situation where the main form gets hidden early and all other forms are closed.
 ... OnCloseQuery -> OnClose -> OnDeactivate -> OnHide -> OnDestroy 
OnCloseQuery
When we try to close the form using the Close method or by other means (Alt+F4), the OnCloseQuery event is called.
Thus, event handler for this event is the place to intercept a form's closing and prevent it. We use the OnCloseQuery to ask the users if they are sure that they realy want the form to close.
 procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean) ;
 begin
   if MessageDlg('Really close this window?', mtConfirmation, [mbOk, mbCancel], 0) = mrCancel then CanClose := False;
 end; 
An OnCloseQuery event handler contains a CanClose variable that determines whether a form is allowed to close. The OnCloseQuery event handler may set the value of CloseQuery to False (via the CanClose parameter), thus aborting the Close method.
OnClose
If OnCloseQuery indicates that the form should be closed, the OnClose event is called.
The OnClose event gives us one last chance to prevent the form from closing.
The OnClose event handler has an Action parameter, with the following four possible values:
  • caNone. The form is not allowed to close. Just as if we have set the CanClose to False in the OnCloseQuery.
  • caHide. Instead of closing the form you hide it.
  • caFree. The form is closed, so it's allocated memory is freed by Delphi.
  • caMinimize. The form is minimized, rather than closed. This is the default action for MDI child forms. Note: When a user shuts down Windows, the OnCloseQuery event is activated, not the OnClose. If you want to prevent Windows from shuting down, put your code in the OnCloseQuery event handler, of course CanClose=False will not do the trick.
OnDestroy
After the OnClose method has been processed and the form is to be closed, the OnDestroy event is called. Use this event for operations opposite to those in the OnCreate event. OnDestroy is therefore used to deallocate objects related to the form and free the corresponding memory.
Of course, when the main form for a project closes, the application terminates.

댓글

이 블로그의 인기 게시물

C# - Serial Port ASCII/HEX Format

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; namespace SerialTest1 {     public partial class Form1 : Form     {         delegate void MyDelegate();      //델리게이트 선언(크로스 쓰레드 해결하기 위한 용도)         bool SendForamt = true;          // true : ASCII   false : HEX         bool ReceiveFormat = true;       // true : ASCII   false : HEX         public Form1()         {             InitializeComponent();          ...

C# - Windows Form 에 있는 control 찾기

// 아래 코드는 form 의 최상위 control만 찾을 수 있음. // panle, groubbox ... 내부에 있는 control은 찾지 못함. Control GetControlByName(string Name) {     foreach (Control c in this.Controls)         if (c.Name == Name)             return c;     return null; } // form 의 모든 control을 찾을 수 있음. string name = "btnBit" + (i + 1).ToString("D2"); var tmpBtn = this.Controls.Find(name, true).FirstOrDefault(); if (tmpBtn != null) {     if (value == 1) tmpBtn.BackColor = Color.Lime;     else tmpBtn.BackColor = Color.Gray; }

C# - ARGB 색상

속성 A 이  Color  구조체의 알파 구성 요소 값을 가져옵니다. Alice Blue ARGB 값이  #FFF0F8FF 인 시스템 정의 색을 가져옵니다. Antique White ARGB 값이  #FFFAEBD7 인 시스템 정의 색을 가져옵니다. Aqua ARGB 값이  #FF00FFFF 인 시스템 정의 색을 가져옵니다. Aquamarine ARGB 값이  #FF7FFFD4 인 시스템 정의 색을 가져옵니다. Azure ARGB 값이  #FFF0FFFF 인 시스템 정의 색을 가져옵니다. B 이  Color  구조체의 파랑 구성 요소 값을 가져옵니다. Beige ARGB 값이  #FFF5F5DC 인 시스템 정의 색을 가져옵니다. Bisque ARGB 값이  #FFFFE4C4 인 시스템 정의 색을 가져옵니다. Black ARGB 값이  #FF000000 인 시스템 정의 색을 가져옵니다. Blanched Almond ARGB 값이  #FFFFEBCD 인 시스템 정의 색을 가져옵니다. Blue ARGB 값이  #FF0000FF 인 시스템 정의 색을 가져옵니다. Blue Violet ARGB 값이  #FF8A2BE2 인 시스템 정의 색을 가져옵니다. Brown ARGB 값이  #FFA52A2A 인 시스템 정의 색을 가져옵니다. Burly Wood ARGB 값이  #FFDEB887 인 시스템 정의 색을 가져옵니다. Cadet Blue ARGB 값이  #FF5F9EA0 인 시스템 정의 색을 가져옵니다. Chartreuse ARGB 값이  #FF7FFF00 인 시스템 정의 색을 가져옵니다. Chocolate ARGB 값이  #FFD2691E 인 시스템 정의 색을 가져옵니다. Coral ARGB ...