Using interfaces in delphi. Features of working with interfaces in Delphi. Standard interface elements

The article was written based on the results of analyzes of programs written by young developers of our group.

We correctly arrange the sequence of switching components

Many users, especially those who previously worked in DOS, have the habit of switching between input fields not with the mouse, but using the keyboard with the Tab key. Plus, it's much faster than selecting each field with the mouse. Therefore, the order of switching components must be set correctly. This applies both to the components inside all container components (panels, GroupBoxes and the like), and to the container components themselves, if there are several of them on the form.

The order of switching components inside the container is specified by the TabOrder property. The first component to become active is the one with TabOrder equal to 0, the second with 1, etc., until all components are enumerated. In addition, the component has a TabStop property, which indicates whether the component will receive focus when toggled with the Tab key. If you need to prohibit switching to any component, set its TabStop = false. In this case, you can only switch to this component using the mouse.

There are cases when users who are accustomed to switching with a certain key in one program, out of habit, continue to use it in others. This often happens with 1C users, where the Enter key can be used to navigate through input fields. Well, we will give them such an opportunity in our programs if they ask for it. Set the form's KeyPreview property to true and write an OnKeyPress event handler:

Procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
if ord(key)=vk_Return then
Form1.SelectNext(PriemForm.ActiveControl, true, true);
end;

This handler provides navigation through form elements when the Enter key is pressed. It should be noted that this method will not work with buttons, because... pressing Enter on a button causes it to be clicked, while pressing Tab gives input focus to the next component in the toggle sequence.

Default buttons

All the same users quickly get used to the fact that in application dialog boxes, as a rule, you can confirm your choice with the Enter key, and cancel with the Esc key. Let's not disappoint them in our programs, especially since it's very easy to do. For a button that responds to Enter, set the Default property to true. For the button that responds to Esc, set the Cancel property to true. That's all.

Yes or no

All dialog boxes, requesting user actions, must have at least two buttons: confirmation of the action and refusal of the action (Yes/No, Save/Cancel, etc.). The action can be canceled by closing the window with the [X] button in the window title. It is unacceptable if there is only one button to confirm the action, and to refuse it is supposed to close the window with the [X] button in the title, or there is no possibility of refusal at all. This confuses the user, raising a logical question: how to refuse?

Also, do not forget what was said above in the “Default buttons” paragraph.

All dialog boxes should open in the center of the screen

Centered, not where they were created in design mode. Firstly, it is more visual, and secondly, it automatically eliminates the problem of different screen resolutions for different users.

An exception is made if the dialog box is not modal, and as a result of the user's work in this window, changes immediately occur in the main window (for example, filtering a data set, redrawing graphs, etc.).

Window sizes should not exceed the size of the screen

In no case. It's a disgrace when part of the window extends beyond the screen. This requirement does not depend on the user’s screen resolution, i.e. excuses like “Let them set a higher resolution” do not work.

Correct resizing of window elements

Window elements should resize or move correctly when the window is resized, when the window is maximized, and when the window is restored after maximization.

Everything is always visible

Reducing the window size should not lead to the disappearance of window elements and, preferably, should not lead to the appearance of scroll bars (scrollers) of the window itself. You can limit the minimum window sizes so that all elements are visible and accessible. If it is not possible to place components so that they are all visible in the window, you can use bookmarks (such as PageControl) to divide components into groups. We also don’t ignore excuses about screen resolution.

Hints everywhere, hints always

For buttons, especially on toolbars (such as ToolBar), hints should be specified so that it is always clear why this or that button is needed.

Color spectrum

You should not paint the components on the form in all the colors of the rainbow. This tires the eyes and distracts the user's attention. It doesn't look "cool". Highlighting is used when it is necessary to draw the user's attention to a specific element or a specific part of the window. For example, color records that contain errors with light red color or, conversely, record records that were successfully checked with light green color.

Conclusion

There is a very good method that allows you to find flaws in the program in general and the interface in particular. It's simple: imagine yourself in the user's place and for half an hour try to work the way he works. It's even better if your user is within range (for example, works in the same organization). In this case, sit next to him, or better yet, instead of him, and try to do his work. Enter data, change it, display reports, etc. If you don't know how to do it right, ask your user. Do not just one or two operations of the same type, as in debugging mode, but 20-30, or even more different operations, in different orders. Forget to enter something or enter it incorrectly and see how the program reacts to it. You will quickly see the weaknesses of your program.

The author of the article automated the work of the admissions committee at the university, and in the first year of introducing the program, he spent 3-4 hours a day in the admissions committee, registering applicants, filling out their personal data and giving them exam reports. And for the rest work time corrected errors and shortcomings. Believe me, next year there will be practically no problems left. The same thing happened when introducing the personnel module.

Thus, keep the user experience in mind. Make it easy and pleasant for them to work with your programs.

One and the most strengths environment Delphi programming is its open architecture, thanks to which Delphi allows a kind of metaprogramming, allowing you to “program the programming environment.” This approach translates Delphi into high-quality new level application development systems and allows you to embed additional tools into this product that support almost all stages of creation application systems. Such a wide range of possibilities opens up thanks to the concept of so-called open interfaces implemented in Delphi, which are the connecting link between the IDE (Integrated Development Environment) and external tools.

This article is dedicated to Delphi's open interfaces and provides an overview of the capabilities they provide. Delphi defines six open interfaces: Tool Interface, Design Interface, Expert Interface, File Interface, Edit Interface, and Version Control Interface. It is unlikely that within the framework of this article we would be able to cover and illustrate in detail the capabilities of each of them. The Delphi source texts will help you more thoroughly understand the issues under consideration; fortunately, the developers have provided them with detailed comments. Declarations of classes representing public interfaces are contained in the corresponding modules in the directory...\Delphi\Source\ToolsAPI. The Design Interface (module DsgnIntf.pas) provides tools for creating property editors and component editors.

Property and component editors are a topic worthy of a separate discussion, so let us just recall that the property editor controls the behavior of the Object Inspector when trying to change the value of the corresponding property, and the component editor is activated when double click left mouse button on the image of the component placed on the form. Version Control Interface (VCSIntf.pas module) is intended for creating version control systems. Since version 2.0, Delphi supports the integrated version control system Intersolv PVCS, so in most cases there is no need to develop your own system. For this reason, we will also omit consideration of the Version Control Interface. File Interface (FileIntf.pas module) allows you to override the IDE's working file system, which makes it possible to choose your own method of storing files (in Memo fields on the database server, for example). Edit Interface (module EditIntf.pas) provides access to the source text buffer, which allows you to analyze and generate code, determine and change the cursor position in the code editor window, and also control the syntactic highlighting of the source text.

Special classes provide interfaces to components placed on the form (defining the type of component, obtaining references to parent and child components, accessing properties, transferring focus, deleting, etc.), to the form itself, and to the project resource file. Edit Interface also allows you to identify so-called modular notifiers that determine the reaction to events such as changing the source text of a module, modifying a form, renaming a component, saving, renaming or deleting a module, changing a project resource file, etc. Tool Interface (ToolIntf module. pas) provides a means for developers to obtain general information about the state of the IDE and performing such actions as opening, saving and closing projects and individual files, creating a module, obtaining information about the current project (number of modules and forms, their names, etc.), registration file system, organization of interfaces to individual modules, etc. In addition to modular notifiers, Tool Interface defines add-in notifiers that notify about events such as opening/closing files and projects, loading and saving a desktop project file, adding/excluding project modules, installing/uninstalling packages, compiling a project, and in Unlike modular notifiers, add-in notifiers allow you to cancel the execution of some events.

In addition, the Tool Interface provides a means of accessing the main menu of the Delphi IDE, allowing you to embed additional items into it. Expert Interface (ExtIntf.pas module) is the basis for creating experts - software modules built into the IDE to expand its functionality. An example of an expert is the Delphi Database Form Wizard, which generates a form for viewing and changing the contents of a database table. Having defined the expert class, we need to make sure that Delphi “learns” about our expert. To do this, you need to register it by calling the RegisterLibraryExpert procedure, passing it an instance of the expert class as a parameter. As an illustration, let's create a simple expert in the esStandard style, which, when you select the corresponding Delphi menu item, displays a message stating that it is running. As you can see from the table above, the esStandard style requires overriding six methods:

In order for the expert to be “put into action”, you must select the Component/Install Component... menu item, select the module containing the expert (in our case exmpl_01.pas) in the Browse dialog, click OK, and after compiling the dclusr30.dpk package In the Delphi main menu, in the Help section, the Simple Expert 1 item should appear, when selected, the information message “Standard Expert started!” appears. Why Delphi places the expert menu item in the Help section remains a mystery. If you don’t like the fact that the menu item appears wherever Delphi wants, and not where you want, the following option is possible: create an expert in the add-in style, which eliminates the automatic creation of the menu item, and add the menu item “manually” using the Tool Interface. This will allow you to set the location of the new item in the main menu in an arbitrary manner.

To add a menu item, use the TIToolServices class - the basis of the Tool Interface - and the TIMainMenuIntf, TIMenuItemIntf classes, which implement interfaces to the IDE main menu and its items. An instance of the ToolServices class TIToolServices is created by the IDE itself when it is initialized. Please note that the responsibility for releasing interfaces to the Delphi main menu and its items rests entirely with the developer. Along the way, let’s complicate the expert’s functional load a little: when you activate its menu item, it will issue a certificate about the name of the project opened in this moment in the environment: In this example, the central place is occupied by the AddIDEMenuItem function, which adds a menu item to the main menu of the Delphi IDE. As parameters, it receives the text of the new menu item, its identifier, the identifier of the item before which the new item is inserted, the symbolic representation of the key, which, together with the Ctrl key, can be used to quick access to a new item, and an event handler corresponding to the selection of a new item. We have added a new menu item in the View section before the Watches item.

Now let's get acquainted with notifiers. Let's define an add-in notifier that tracks the closing/opening of projects and adjusts the field storing the name of the active project accordingly (we will omit the implementation of methods that have not undergone changes compared to the previous example for brevity): To implement the notifier, we have defined the TAddInNotifier class, which is a descendant TIAddInNotifier, and overridden the FileNotification method. The IDE will call this method every time an event occurs that the add-in notifier can respond to (each such event is indicated by a corresponding constant of type TFileNotification). The Expert field in the TAddInNotifier class is used to feedback with an expert (TAddInNotifier.FileNotification method). In the expert's destructor, the notifier is unregistered and the notifier is destroyed. Now let’s illustrate the use of modular notifiers. Let's create an add-in expert that issues messages about each act of saving a project file (for the sake of brevity, we do not present the implementation of methods already familiar to us): In this example, the add-in expert monitors events corresponding to the opening/closing of projects.

Each time a project is opened, a modular notifier corresponding to the project file is registered. In terms of implementation, modular notifiers are similar to add-in notifiers: we define the TModuleNotifier class, which is a descendant of TIModuleNotifier, and override its Notify and ComponentRenamed methods. The IDE calls the Notify method when certain events related to this module occur; Within this method, the reaction to a particular event is determined. The ComponentRenamed method is called when the name of a component on the module form is changed. Please note that we do not use this method, but we must override it, otherwise when the component name changes, an abstract method of the base class will be called, which leads to unpredictable consequences.

Registering a modular notifier is a slightly more complex process compared to registering an add-in notifier: first we obtain the module interface (TIModuleInterface), and then register the notifier using the module interface. When the project is closed, the modular notifier is unregistered (again using TIModuleInterface) and the notifier is destroyed. In conclusion, we will show how you can determine the position of the cursor in the code editor window. Let's create an expert that, when selecting the appropriate menu item, would display a message containing the name of the active file and the cursor position in it (implementation of only the methods essential for this example is shown): To determine the cursor position, we must obtain the following sequence of interfaces: modular interface (TIModuleInterface); code editor interface (TIEditorInterface); module presentation interface in the editor window (TIEditView).

If, when selecting an expert menu item, the source text file (*.pas) is active, a message is displayed containing the name of the active file and the current cursor position in it. If the active file is not a pas file, then the message is not issued. To get the name of the active file, use the GetCurrentFile method of the TIToolServices class. This concludes our consideration of ways to use open interfaces. The CD-ROM contains the source code for all of the examples given. The CD-ROM also contains a more complex and detailed example, containing an add-in expert that allows the user to bookmark the source text of Delphi modules. A brief guide to installing and using Bookmarks Expert is contained in the file bkmrks97.htm. So, this article provides a general overview of open interfaces and provides examples of their use. Let us repeat once again: thanks to the availability of source codes for open interfaces, you can easily understand the details that interest you. We hope that the variety of possibilities provided by open interfaces will give you more than one bold and useful idea.

only for the result

strict adherence to deadlines

Transparency

project implementation

technical support as a gift

Programming, modifications, consultations on 1C

How we are working

1. We discuss the problem over the phone. In the presence of remote access- show on your computer screen.

2. We estimate the work in rubles if the project is large; if not, the approximate number of hours.

3. We get the job done.

4. You accept the work in your program; if there are any shortcomings, we correct them.

5. We issue an invoice, you pay.

Cost of work

1. All work is divided into 3 categories: consultation, updating a standard configuration, developing or programming a new report, processing, button, etc.

3. Work over 10 hours must be prepared in advance. technical task with description and cost of work. Work begins after the technical specifications have been agreed upon with you.

Technical support

1. If you find any errors in previously accepted work within 3 months, we correct them free of charge.

2. For regular customers, we correct any shortcomings in our work free of charge within a year.

Software to manage your business.

Buy 1C:Enterprise

We are official dealer 1C company, you can purchase from us various software products and licenses. In addition to purchasing the “box,” we will help you set up the program, provide advice and make basic settings.

  • Accounting
  • Store automation
  • Wholesale
  • Installation assistance and initial setup included in the package!
  • Fine-tuning configurations to customer needs, developing new modules in the absence of the necessary functions in the standard configuration.
1c accounting 1C: Trade Management 1C: Retail 1C: Salary and Personnel Management
From 3300 rub. From 6700 rub. From 3300 rub. From 7400 rub.

Server provision.

Instant setup of server + 1C.

No server? No problem, we’ll select and quickly set up a server in the cloud. For a small fee you get a very reliable solution.

  • Availability 24\7
  • No need to keep your own system administrator(the savings will cover your server costs).
  • Quick setup and installation of 1C on the server, in 3 days you will already have a fully working system.
  • You can move to local server, if the solution is not satisfactory.

SMS from your 1C

Do you want your customers to find out about promotions and discounts? Are clients not returning? Set up sending SMS directly from 1C!

Our company will be able to quickly set up sending SMS to your customers directly from 1C. Examples of events that can be automated:

  • Thanks for the purchase and bonuses are awarded immediately after the next purchase.
  • Accrual of bonuses to the card as a gift for a birthday\other significant or holiday.
  • Notification of goods arrival at the warehouse.
  • Expiration of gift bonuses.
  • Notification of receipt of advance payment and reservation of goods.
  • Address with directions to the store/office, telephone numbers.
  • And so on.

Setting up in 1C can be done by our specialists or your employees. You can view the tariffs on the SMS tariffs page.

  • Guaranteed SMS delivery, money is charged only for delivered SMS.
  • Separate pricing for each SMS.
  • Top up your balance in different ways.
  • View the history of all sent SMS at any time.
  • The sender's name instead of the digital number in the message recipient's phone.

Object-oriented programming (OOP), in addition to the concept of a class, also provides for the fundamental concept of an interface.

What is an interface and what are the features of working with it in the Delphi programming language?

Interface is a semantic and syntactic construct in program code used to specify the services provided by a class or component (Wikipedia).

Essentially, an interface defines a list of properties and methods that must be used when working with the class that this interface implements, as well as their signature (name, data type, accepted parameters (for procedures and functions), etc.). Thus, a class that implements a particular interface must implement all its components. Moreover, in strict accordance with how they are described in it.

Interfaces are often compared to abstract classes, but despite all the similarities, this comparison is not entirely correct. At a minimum, abstract classes allow you to control the visibility of members. At the same time, scopes are not defined for interfaces.

Interfaces allow you to make the architecture more flexible, as they unify access to this or that functionality, and also allow you to avoid a number of problems associated with class inheritance (interfaces can also be inherited from one another).

To declare an interface in Delphi, use the interface keyword. This is the same keyword that defines the section of the module that can be accessed from outside (between the interface and implementation keywords). However, when declaring an interface, a different syntax is used, similar to declaring classes.

Delphi/Pascal

IMyNewInterface = interface procedure InterfaceProc; end;

IMyNewInterface =interface

procedure InterfaceProc ;

end ;

Thus, the interface declaration syntax itself is not fundamentally different from other programming languages ​​(the features of the syntax based on Pascal do not count). At the same time, the implementation of interfaces has a number of characteristic features.

The fact is that Delphi interfaces were originally introduced to support COM technology. Therefore, the IInterface interface, which in Delphi is the ancestor for all other interfaces (a kind of analogue of TObject), already contains three basic methods for working with this technology: QueryInterface, _AddRef, _Release. As a result, if a class implements any interface, then it must also implement these methods. Even if this class is not intended for COM work.

Due to this feature of the IInterface interface, in Delphi the use of interfaces, in most cases, leads to the addition of obviously unused capabilities to the class.

There is a library class TInterfaceObject that already contains the implementation of these methods and, when inheriting from it, there is no need to implement them yourself. But since Delphi does not support multiple class inheritance, its use often only causes additional difficulties in the design and implementation of already required functionality.

All this led to the fact that, despite all the possibilities provided by interfaces, they practical use In Delphi, almost nothing has gone beyond working with COM.

Being optimized to work mainly with this technology, the interfaces, or rather the functionality and architectural restrictions they necessarily add, do not justify themselves when solving other problems.

Therefore, many Delphi programmers are still, in fact, deprived of a powerful and flexible tool for developing application architecture.

Creation interface the user is reduced to selecting from a palette of components necessary for the operation of the program Delphi components, employees interface management, as well as interface displaying information, and transferring it to the Form with subsequent layout.

The user interface you create should use standard elements familiar to users and provide maximum convenience. All this is ultimately determined by such a criterion as the effectiveness of the interface - maximum results with minimal effort.
The principles of creating a user-friendly interface are known. As the most general principles when creating user interfaces Three main points can be considered:

  1. The program should help complete a task, and not become that task.
  2. When working with the program, the user should not feel like a fool.
  3. The program should work in such a way that the user does not consider the computer a fool.
First principle- this is the so-called “transparency” of the interface. The user interface should be intuitive, easy to learn, and not create problems for the user that he will have to overcome during the work process. Use standard components without unnecessary embellishment, use familiar control techniques used by similar programs, and you will achieve the performance criteria first principle.

Second principle is to neglect the intellectual abilities of users. From my own experience, I know that often users not only do not know how to work on a computer, but are simply afraid to do anything on their own. Therefore, the user interface should be as friendly as possible.
Moreover, users’ fears are often justified, because the cost of the program, and even the computer itself, cannot be compared with the cost, for example, of a database created over many years of effort. That is why, when creating a user interface, a programmer must always build “foolproof protection” into the program - against incorrect actions and user input of incorrect data. But some programmers get too carried away with such protection, make it too intrusive, and as a result, the program’s operation resembles the famous “one step to the left, a step to the right is considered an escape”! And what the programmer creates as a solution to a problem begins to create problems itself.
To comply second principle there is no need to allow the program to “correct” the user’s actions and indicate exactly how he should act, forcing him into a narrow framework. Also, you should not get too carried away with displaying informational prompt messages, especially dialog ones, as this distracts the user from work. It would be better to provide the ability to disable hints altogether.

Third principle is to create a program with the highest possible "mental" capabilities. Despite the rapid development computer equipment, even widely used programs can only very conditionally be called having artificial intelligence. They interfere with the user's work by displaying dialog boxes with stupid questions that cause confusion even in the simplest situations. As a result, users exclaim in their hearts: “What a stupid machine!”
I personally am irritated by the constant questions from almost everyone text editors about whether to save the changed text, although the original and current text do not differ in any symbol. Yes, I typed something, but then I returned everything back, is it really impossible to figure it out! I have to check if I haven't ruined anything after all.

Try to adhere to the following rules:

Standard interface elements
Use components that are standard for this interface element. Having encountered your program, the user will not waste time getting to know each other, but will immediately start working - this is one of the signs of a professionally made program.
Small palette of tools
Try not to use too many different components. And naturally, having used one standard component somewhere in one place, in a similar case also use it.
Equal spacing between controls
Place interface elements at the same distance from each other. Components scattered haphazardly create the feeling of an unprofessionally made product. And vice versa, carefully verified placement of buttons, switches, checkboxes and other components that make up the interface on the Form is a sign of high-quality work.
TabOrder. "Correct" order
TabOrder is the order in which the screen cursor moves across controls when a key is pressed Tab. In a properly written program, the cursor moves following the logic of the user's work with the program. When creating a program, the programmer often changes components, removes some, and adds others as needed. As a result, in the finished program the cursor jumps chaotically around the Form. After completing the program, do not forget to configure TabOrder.
Font selection
Just leave the fonts alone. Delphi's default fonts are suitable for any system on which your program may run. Use bold font only to highlight important elements. Application italics and especially underlining, which the user might mistake for a hyperlink is bad form.
Choice of colors
As for the colors of interface elements, just as in the case of fonts, it is better to leave them standard, by default. Delphi uses the Windows system palette, and the user can easily customize the colors by changing it.
Alternative management
A professionally made program should be able to be controlled not only with the mouse, but also with the keyboard. There should be no functions that can only be performed with the mouse (drawing with graphic editors doesn't count!). For the most used functions, you should provide “hot keys” for quick access.
Interface bricks
As for specific elements of the user interface, the quality of user interaction with the program depends on:
  • compliance of the control element with the task it performs;
  • rules by which the control element operates.
    This page discusses the rules for creating some interface elements.
And now I want to show what tools Delphi offers for managing components on the Form, their relative position and cursor behavior when a key is pressed Tab.

To arrange the components relative to each other in the correct order, first it is necessary to highlight them. You can simply circle the area on the Form that contains the selected components. Or by holding " Shift", use the same mouse to indicate each component to be selected. Click the mouse again on the selected component (while pressing " Shift") deselects it.

The selected components can be managed as a single whole - moved around the Form, assigned values ​​to the same properties, copied (for installation, for example, on another Form), even deleted.

Now right-click on one of the components and select from the pop-up menu Position -> Align... A dialog box will appear allowing you to adjust the horizontal and vertical position of the components in the group. For example, we need to align our four buttons to the left and make sure they have the same vertical distance between them. To do this, select the radio buttons Horizontal: Left sides And Vertical: Space equally.

By selecting the item Center, we will arrange the components so that their centers will be located on the same line horizontally or vertically, and the item Center in window moves components to the center of the window, also horizontally or vertically.

In the same menu line Tab O rder... causes a dialog box to appear that controls how the cursor moves through interface elements when a key is pressed Tab. When the Form appears on the screen, the cursor will naturally be on the component located on the first line of the dialog box. And then it will move down the list. In the dialog box, two blue up and down arrows control the position of the selected component. Select the required component, use the arrows to move it to the desired line in the list, and so on.

When selecting a menu item Control -> A submenu appears consisting of two items:

  • Bring to Front
  • Send to Back
These are component methods that are also available programmatically. Button1.SendToBack moves the button to the "background" and Button1.BringToFront- bring to Front". That is, if one component is positioned above another, these methods swap their positions. The cases in which this might apply are pretty obvious.


Top