Programming in KOL (without MCK). Create a Form and start a Message Loop.
In order to start designing a "clean" KOL project (ie without MCK), it is enough to create a project as usual and remove the first module with a form from it. After that, in the project file, remove the reference to Forms and all other VCL units from uses (replacing them with a reference to KOL), and from the body of the project code, begin ... end. delete all lines (there are two of them, and they refer to Application to initialize and launch the application). Now you can add the first lines of code:
Applet: = NewForm (nil, 'form title'); Run (Applet);
This is a minimal KOL form application. You can try compiling it and running it. If something doesn't work out for you, take a look at the demo project called Empty. If you do nothing else, then the size of this application in Delphi5 is 23 Kilobytes. If, in the project options, add the path to the folder with the replacement of system modules to the list of search paths, and add SMALLEST_CODE and NOT_USE_RICHEDIT to the list of conditional compilation symbols, then the application size is reduced to 10.5 Kilobytes.
|
Please note: if you compile a project in Delphi version 6 or higher, then sometimes the compiler has an incomprehensible tendency to add additional functions to the code from the Variants.pas module (which appeared in this version for the first time). The size of the application grows dramatically by several kilobytes, even if you didn't use the options. Sometimes it is possible to get rid of this module by various manipulations (reopening the project in Delphi, restarting the environment, rebuilding the project). The most efficient way is to download the FakeVariants.zip archive, unpack it (the Variants.pas file, from which everything that is not required has been removed) into a directory and specify the path to it in the project options. Well, or just unzip it into your project folder. |
The global procedure Run, which is called in the above example, receives a window object as a parameter, calls for it to create a window, and then enters a loop of waiting and processing messages. This cycle continues until the application is terminated (i.e., until the global variable AppletTerminated is set to true). Then the global procedure TerminateExecution is called (if it is still needed), and this is where the application really ends.
The programmer always has the opportunity to write his own analog of the Run procedure, if necessary, and call it exactly. For example, in one of my applications, I changed this procedure in order to process messages from the mouse and keyboard first, before other window messages (otherwise, when multithreaded work with an increased priority, problems with reaction to the keyboard and mouse began to arise).
When programming in KOL without MCK, you should pay attention to how to assign event handlers. The easiest way is to create a custom object (derived from TObj), define a method for it that matches the handler type description, and specify that method as the event handler.
|
But you can use ordinary procedures instead of methods, turning them into a method using the MakeMethod (data, proc) function and not forgetting to convert the resulting method to the required handler type, for example:
Button1.OnClick: = TOnEvent (MakeMethod (nil, @ Button1ClickProc)); It should be remembered that in the declaration of the handler procedure, you must add an additional parameter of the pointer type as the first. This pointer corresponds to a pointer to an object instance, that is, the same pointer that you assigned in the first parameter in the call to the MakeMethod function will be passed here. For example, to correctly handle the above button click event, the titlethe handler should look like this:
procedure Button1ClickProc (Dummy, Sender: PControl); |
In the case of a different type of handler with more parameters, they also follow in the usual order, but the same dummy parameter must be added first.