Some way to rearrange components positions, sizes, ownership (properties in general) at run-time following a design-time rule

∥☆過路亽.° 提交于 2019-12-11 06:29:15

问题


We have an application that have many components on the forms (panels, tabs, edits, comboboxes, etc...). But depending on the user profile, most of them could be filled automatically and/or not be visible. So, users could do their work faster.

The question: Is there any easier way to create, position, change ownership etc, at runtime? I would like to create 2 .dfm files for a given unit and then have something to tell the application what .dfm to use. Like: "Hey! User is advanced, use the Unit1Advanced.dfm!" A working example would be nice. I would like to use that in Delphi 7 too, but it has to work at least in Delphi XE.

What I know that exist till now:

  1. ComponentsToCode function from GExperts can create code from a given component as gabr pointed in this answer.
  2. I could create 2 forms and create the desired one at runtime. But that means one additional .pas file to each additional .dfm file. This would be harder to maintain.
  3. This answer seems to give a hint. But I am not used to TReader and TWriter classes...

回答1:


Warning: This answer is for completeness sake to the question and is only for experimental purposes. It should never be used in real world scenarios.

You want two separate form definition files for only one source code file.

The key is to make use of the CreateNew constructor. To quote the documentation on it:

Use CreateNew instead of Create to create a form without using the associated .DFM file to initialize it.

  1. First, write your advanced form:

    unit Advanced;
    
    interface
    
    uses
      Classes, Controls, Forms, StdCtrls;
    
    type
      TAdvancedForm = class(TForm)
        StandardGroupBox: TGroupBox;
          StandardButton: TButton;
        AdvancedGroupBox: TGroupBox;
          AdvancedButton: TButton;
        procedure StandardButtonClick(Sender: TObject);
        procedure AdvancedButtonClick(Sender: TObject);
      end;
    
    implementation
    
    {$R *.dfm}
    
    procedure TAdvancedForm.StandardButtonClick(Sender: TObject);
    begin
      Caption := Caption + ' Button1Click';
    end;
    
    procedure TAdvancedForm.AdvancedButtonClick(Sender: TObject);
    begin
      Caption := Caption + ' Button2Click';
    end;
    
    end.
    
  2. Build your app, and copy Advanced.dfm to Standard.dfm.

  3. Open Standard.dfm in a text editor and remove the advanced components (in this case the advanced group box containing a button), and rename the form and form type to (T)StandardForm:

    object StandardForm: TStandardForm
      ...
      object StandardGroupBox: TGroupBox
        ...
        object StandardButton: TButton
          ...
        end
      end
    end
    
  4. Add the resource for the standard form to Advanced.pas:

    {$R *.dfm}
    {$R Standard.dfm}
    
  5. And now with the following code, you can open both form definitions for the same source file:

    uses
      Advanced;
    
    procedure TForm1.OpenAdvancedFormClick(Sender: TObject);
    var
      Form: TAdvancedForm;
    begin
      Form := TAdvancedForm.Create(Application);
      Form.Show;
    end;
    
    procedure TForm1.OpenStandardFormClick(Sender: TObject);
    var
    {
      Form: TAdvancedForm; // This is tricky! The form we are about to create has
                           // no AdvancedGroupBox nor AdvancedButton, so make sure
                           // you are not calling it with code completion.
      Form: TStandardForm; // Compiler has no knowledge of TStandardForm!
    }
      Form: TForm;         // So declare your form as TForm!
    begin
      // But create it as TAdvancedForm, otherwise components will not be found!
      Form := TAdvancedForm.CreateNew(Application);
      ReadComponentRes('TStandardForm', Form);
      Form.Show;
    end;
    



回答2:


I can give you a solution based on your point 2: Start with the form for the unexperienced user, place all controls as needed and implement the necessary code in the pas file. Then make a new form inherited from the first one and adjust it to the needs of the experienced user. If necessary you can also add some implementation.

A more flexible approach may be to inherit both forms from a common anchestor. The actual implementation of this scheme depends heavily on your situation.




回答3:


Your desire to have only one .pas file for multiple .dfm files is nearly impossible but also a little incomprehensible: the code would be limited to the less advanced form possible, but really not to recommend.

Regarding your separation requirements:

  • Creation: Make two designs, and do this by Visual Form Inheritance (VFI) like Uwe mentions, so you do not have the separated maintenance problem.
  • Position and sizes: This is a very bad idea. Let controls stay at the same place for each user. Users might get advanced privileges, and be shocked by an unfamiliar arrangement. The only exception I would make for is the minimum size of the form: The advanced version may be taller and/or wider.
  • Change ownership: Yeah, I understand what you mean, but controls are not owned by the user, nor should they. Implement some kind of controller which deals with what type of form should be created.

In case VFI is not an option, then I suggest you design only the advanced form, and control the hiding of advanced controls by setting the Visible property of the linked actions. Do this in or via the setter for the Advanced: Boolean property, which should exist. Or group all advanced controls on one or more containers: group boxes, panels or frames like LU RD comments. Then simply set the visibility of that container. Present scroll bars will adjust or disappear accordingly automatically. Note that you still can address these controls, even if you not want to.




回答4:


The DevExpress ExpressLayout components might be helpful - see http://devexpress.com/Products/VCL/ExLayoutControl/

They provide Runtime Form Customization - Runtime Control Customization - Screen Resolution Independence, and much more



来源:https://stackoverflow.com/questions/8456735/some-way-to-rearrange-components-positions-sizes-ownership-properties-in-gene

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!