Showing posts with label form. Show all posts
Showing posts with label form. Show all posts

Friday, 2 June 2017

How to dock a form to a panel programmatically

This is a quick solution to a question I had on how to have a form that is set so it can dock into another form's panel. I have a main form with a panel on the right with the 'Docksite' property set to true. I have another form with the 'DragKind' property set to dkDock and the 'DragMode' set to dmAutomatic. I create the docking form at runtime and wanted it so that by default it was docked into the right panel. The quick solution that I found hard to find on the net is after I create the form do the following:

DockingForm.ManualDock(PanelRight);
DockingForm.Show;

Friday, 21 June 2013

Delphi After Resize Event

Here is the way to handle what happens after a user has resized a form. This works fine for standard forms, but does not work for forms within a form.

procedure WMEXITSIZEMOVE(var Message: TMessage); message      WM_EXITSIZEMOVE;

procedure TForm1.WMEXITSIZEMOVE(var Message: TMessage);
begin
    ShowMessage('Just Resized');
end;

I would like to know if there is a way to capture an event when resizing a form in a form.

There is also a similar Windows message for when a user starts to resize a form, which is WM_ENTERSIZEMOVE.

Thursday, 28 February 2013

Iterate through controls on a form

Here is how to iterate through all the controls on a form, this is very useful for setting a property value of more than one control, for example the enabled property.

procedure IterateControls(AControl: TControl);
    var
        iControl: Integer;
    begin
        if AControl = nil then
            Exit;
        if AControl is TWinControl then
        begin
            for iControl := 0 to TWinControl(AControl).ControlCount - 1 do
                IterateControls(TWinControl(AControl).Controls[iControl]);
        end;

        // Do Control stuff here
        AControl.Enabled := false;
    end;

Where you want to set the control property value it is possible to check for the class something like this.

if (AControl is TButton) then
   (AControl as TButton).Enabled := false;

And, of course the boolean value can be passed as a parameter to the procedure.

Monday, 26 November 2012

How to draw a gradient background

My previous post I wrote about how to reduce flicker on a gradient background and did not mention how to create a gradient background. Here is how I create a gradient background.

Interface

procedure GradVertical(Canvas: TCanvas; Rect: TRect; FromColor, ToColor: TColor);

Implemention


procedure TForm1.GradVertical(Canvas: TCanvas; Rect: TRect; FromColor, ToColor: TColor);
var
    Y                     : Integer;
    dr, dg, db            : Extended;
    C1, C2                : TColor;
    r1, r2, g1, g2, b1, b2: Byte;
    R, G, B               : Byte;
    cnt                   : Integer;
begin
    C1 := FromColor;
    r1 := GetRValue(C1);
    g1 := GetGValue(C1);
    b1 := GetBValue(C1);

    C2 := ToColor;
    r2 := GetRValue(C2);
    g2 := GetGValue(C2);
    b2 := GetBValue(C2);

    dr := (r2 - r1) / Rect.Bottom - Rect.Top;
    dg := (g2 - g1) / Rect.Bottom - Rect.Top;
    db := (b2 - b1) / Rect.Bottom - Rect.Top;

    cnt   := 0;
    for Y := Rect.Top to Rect.Bottom - 1 do
    begin
        R := r1 + Ceil(dr * cnt);
        G := g1 + Ceil(dg * cnt);
        B := b1 + Ceil(db * cnt);

        Canvas.Pen.Color := RGB(R, G, B);
        Canvas.MoveTo(Rect.Left, Y);
        Canvas.LineTo(Rect.Right, Y);
        Inc(cnt);
    end;
end;


procedure TForm1.ButtonClick(Sender: TObject);
var
    Rect: TRect;
begin
    Rect := GetClientRect;
    GradVertical(Self.Canvas, Rect, clBlack, clRed);
end;

Friday, 19 October 2012

How to put a single line border around a form

Recently I wanted to put a single line border around a form without having a title bar. The way I solved the problem was to set the border style to bsNone and put the following in the OnPaint event of the form.


var
    YFrame: Integer;
    Rect  : TRect;
begin
    inherited;
    if not(fsModal in FormState) then
    begin
        YFrame      := 1;
        Rect.Left   := 0;
        Rect.Top    := 0;
        Rect.Right  := Width;
        Rect.Bottom := Height;

        Canvas.Handle      := GetWindowDC(Handle);
        Canvas.Brush.Color := clBlack;
        Canvas.Brush.Style := bsSolid;
        Canvas.Rectangle(Rect.Left, Rect.Top, Rect.Right, YFrame);
        Canvas.Rectangle(Rect.Left, Rect.Top, YFrame, Rect.Bottom);
        Canvas.Rectangle(Rect.Right - YFrame, Rect.Top, Rect.Right, Rect.Bottom);
        Canvas.Rectangle(Rect.Left, Rect.Bottom - YFrame, Rect.Right, Rect.Bottom);
    end;
end;