⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 forms.htm

📁 对于学习很有帮助
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>

<head>
<title>UDDF - Forms</title>
<META NAME="Description" CONTENT="Forms section of the Delphi Developers FAQ" >
<META NAME="KeyWords" CONTENT="" >

</head>

<body  bgcolor="#FFFFFF">
<CENTER>
<IMG SRC="../images/uddf.jpg"> </CENTER>
<HR SIZE="6" color="#00FF00">

<p align="center"><font color="#FF0000" size="7"
face="Arial Black">Forms</font> </p>

<H1><A NAME="forms0">Delphi Applications without Forms?</A></H1>

<p><i>From: bpeck@prairienet.org (Bob Peck)</i></p>

<p>You bet! First, select File|New Project and choose &quot;CRT
Application&quot; from the Browse Gallery dialog. This will
provide you with a project that is still a Windows program, but
WriteLn, ReadLn will be allowed in a Window but work like they
did in DOS. If you wish, you can remove the WinCrt unit from the
uses statement (if no user input/output is required, but is nice
for debugging).</p>

<p>I've done this before just to see how small an app can be and
I've been able to create a simple EXE (it just beeps) that is
only 3200 bytes or so in size! Try to do that in C++ these days!</p>

<p>BTW, these &quot;formless&quot; apps are still Windows
applications, so they can still call the Windows API routines.
You'll just need to add WinProcs, WinTypes to your uses clause.
You'll probably also want to add SysUtils and any other unit you
find yourself needing.</p>

<H1><A NAME="forms1">Showing own logo on start-up</A></H1>

<p><i>From: &quot;Mark R. Holbrook&quot; &lt;markh@sysdyn.com&gt;</i></p>

<p>It's pretty simple.</p>

<p>Create a form and put the logo on it using a Timage component.
My example below assumes you have created a logo form called
&quot;logoform&quot; Go to your project options and set the form
to NOT be autocreated.</p>

<p>Then in your PROJECT.DPR file just after the begin statement
do something like the following:</p>

<hr>

<pre>   logoform := TLogoform.Create(nil);
   logoform.Show;            { NOTE! show!   NOT showmodal }

   .
   . { Do other app startup stuff here like open databases etc... }
   .

   .  { Just after the block of code that creates all your forms and
         before the Application.Run statement do: }

   logoform.Hide;
   logoform.Release;
</pre>

<hr>

<p>This will display your logo form until you actually start the
app running.</p>

<H1><A NAME="forms2">Moving a form without a caption bar</A></H1>

<p><i>From: mger@sbox.tu-graz.ac.at (Matthias Gerstgrasser)</i></p>

<p>The following is from DKBS Helpfile (Delphi Knowledge Base
System), they state, that this is one of Borland's TI's:</p>

<p>Q: How can I make a form move by clicking and dragging in the
client area instead of on the caption bar?</p>

<p>A: The easiest way to do this is to &quot;fool&quot; Windows
into thinking that you're actually clicking on the caption bar of
a form. Do this by handling the wm_NCHitTest windows message...</p>

<hr>

<pre>type
  TForm1 = class(TForm)
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    procedure WMNCHitTest(var M: TWMNCHitTest); message wm_NCHitTest;
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.WMNCHitTest(var M: TWMNCHitTest);
begin
  inherited;                    { call the inherited message handler }
  if  M.Result = htClient then  { is the click in the client area?   }
    M.Result := htCaption;      { if so, make Windows think it's     }
                                { on the caption bar.                }
end;
</pre>

<hr>

<H1><A NAME="forms3">Preventing the user from resizing my window vertically</A></H1>

<p><i>From: Bill Dekleris &lt;quasar@prometheus.hol.gr&gt;</i></p>

<p>You must trap WM_GETMINMAXINFO message:</p>

<p>in your form's class declaration put this :</p>

<hr>

<pre>procedure WMGetMinMaxInfo(var Msg: TWMGetMinMaxInfo); message WM_GETMINMAXINFO;
</pre>

<hr>

<p>and in the implementation section :</p>

<hr>

<pre>procedure TMyForm.WMGetMinMaxInfo(var Msg: TWMGetMinMaxInfo);
begin
   { ---------------------------------------------}
   { Put your numbers in place of                 }
   { MIN_WIDTH, MIN_HEIGHT, MAX_WIDTH, MAX_HEIGHT }
   {                                              }
   { To allow only horizontal sizing, put         }
   { form's 'Height' property in place of MIN_HEIGHT, MAX_HEIGHT }
   { ---------------------------------------------}
   Msg.MinMaxInfo^.ptMinTrackSize := Point(MIN_WIDTH, MIN_HEIGHT);
   Msg.MinMaxInfo^.ptMaxTrackSize := Point(MAX_WIDTH, MAX_HEIGHT);
   inherited
end;
</pre>

<hr>

<p>It should work fine.</p>

<H1><A NAME="forms4">Hack: Want VCL controls in a form's title bar caption area?</A></H1>

<p><i>From: &quot;James D. Rofkar&quot;
&lt;jim_rofkar%lotusnotes1@instinet.com&gt;</i></p>

<p>Here's the trick:</p>

<p>Treat your controls like they belong in a separate modeless
dialog that just so happens to track the movement and resizing of
your main form. In addition, it always appear over the main
form's caption area. </p>

<p>This said, here's a simple hack that involves 2 forms and a
drop-down listbox. After running this program, the drop-down
listbox will appear in the Main form's caption area. Two key
issues are: 1) trapping the Main form's WM_MOVE message; and 2)
returning focus back to the Main form after users press any
focus-grabbing controls (like a TComboBox, TButton, etc.)</p>

<p>[FYI, I'm using 32-bit Delphi 2.0 Developer under Win95 --
even though this technique should work for all versions of
Delphi]</p>

<p>Here's the source for the Main form:</p>

<hr>

<pre>   unit Unit1;

   interface

   uses
     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
     Dialogs,  StdCtrls;

   type
     TForm1 = class(TForm)
       procedure FormResize(Sender: TObject);
       procedure FormShow(Sender: TObject);
       procedure FormHide(Sender: TObject);
     private
       { Private declarations }
     public
       { Public declarations }
       procedure WMMove(var Msg: TWMMove); message WM_MOVE;
     end;

   var
     Form1: TForm1;

   implementation

   uses Unit2;

   {$R *.DFM}

   procedure TForm1.FormResize(Sender: TObject);
   begin
        with Form2 do
           begin
           {Replace my magic numbers with real SystemMetrics info}
           Width := Form1.Width - 120;
           Top := Form1.Top + GetSystemMetrics(SM_CYFRAME);
           Left := ((Form1.Left + Form1.Width) - Width) - 60;
           end;
   end;

   procedure TForm1.FormShow(Sender: TObject);
   begin
        Form2.Show;
   end;

   procedure TForm1.FormHide(Sender: TObject);
   begin
        Form2.Hide;
   end;

   procedure TForm1.WMMove(var Msg: TWMMove);
   begin
        inherited;
        if (Visible) then FormResize(Self);
   end;

   end.
</pre>

<hr>

<p>Here's the source for the pseudo-caption area form. This is
the form that contains the VCL controls you wish to place in the
Main form's caption area. Essentially, it's a modeless dialog
with the following properties:</p>

<hr>

<pre>   Caption='' {NULL string}
   Height={height of caption area}
   Width={width of all controls in form}
   BorderIcons=[] {none}
   BorderStyle=bsNone
   FormStyle=fsStayOnTop
</pre>

<hr>

<p>Anyhow, here's the source for Form2:</p>

<hr>

<pre>   unit Unit2;

   interface

   uses
     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
     Dialogs, StdCtrls;

   type
     TForm2 = class(TForm)
       ComboBox1: TComboBox;
       procedure FormCreate(Sender: TObject);
       procedure ComboBox1Change(Sender: TObject);
       procedure FormResize(Sender: TObject);
     private
       { Private declarations }
     public
       { Public declarations }
     end;

   var
     Form2: TForm2;

   implementation

   uses Unit1;

   {$R *.DFM}

   procedure TForm2.FormCreate(Sender: TObject);
   begin
        Height := ComboBox1.Height - 1;
        Width := ComboBox1.Width - 1;
   end;

   procedure TForm2.ComboBox1Change(Sender: TObject);
   begin
        Form1.SetFocus;
   end;

   procedure TForm2.FormResize(Sender: TObject);
   begin
        ComboBox1.Width := Width;
   end;

   end.
</pre>

<hr>

<p>The project file (.DPR) is fairly straightforward:</p>

<hr>

<pre>   program Project1;

   uses
     Forms,
     Unit1 in 'Unit1.pas' {Form1},
     Unit2 in 'Unit2.pas' {Form2};

   {$R *.RES}

   begin
     Application.Initialize;
     Application.CreateForm(TForm1, Form1);
     Application.CreateForm(TForm2, Form2);
     Application.Run;
   end.
</pre>

<hr>

<p>That's it!</p>

<p>Although some Delphi book authors state:</p>

<p>&quot;You can't place a Delphi component on the title bar, so
there's literally no way to put a button there.&quot;</p>

<p>you can at least &quot;fake&quot; the illusion...</p>

<H1><A NAME="forms5">Storing TForm and/or its properties in a BLOB</A></H1>

<p><i>From: Oliver.Bollmann@t-online.de (Oliver Bollmann)</i></p>

<p>Hallo, here are examples you need, I hope:</p>

<hr>

<pre>procedure SaveToField(FField:TBlobField;Form:TComponent);
var
  Stream: TBlobStream;
  FormName: string;
begin
  FormName := Copy(Form.ClassName, 2, 99);
  Stream := TBlobStream.Create(FField, bmWrite);
  try
    Stream.WriteComponentRes(FormName, Form);
  finally
    Stream.Free;
  end;
end;

procedure LoadFromField(FField:TBlobField;Form:TComponent);
var
  Stream: TBlobStream;
  I: integer;
begin
  try
    Stream := TBlobStream.Create(FField, bmRead);
    try
      {delete all components}
      for I := Form.ComponentCount - 1 downto 0 do
        Form.Components[I].Free;
      Stream.ReadComponentRes(Form);
    finally
      Stream.Free;
    end;
  except
    on EFOpenError do {nothing};
  end;
end;
</pre>

<hr>

<H1><A NAME="forms6">Removing icon on taskbar</A></H1>

<p><i>From: AVONTURE Christophe
&lt;Christophe.AVONTURE@is.belgacom.be&gt;</i></p>

<hr>

<pre> ShowWindow (Form1.Handle, SW_HIDE);</pre>

<hr>

<H1><A NAME="forms7">How can I hide the form caption bar??</A></H1>

<p><i>From: &quot;James D. Rofkar&quot;
&lt;jim_rofkar%lotusnotes1@instinet.com&gt;</i></p>

<p>First, override the &quot;CreateParams&quot; method of your
Form, by declaring this in either your Form's protected or public
section: </p>

<hr>

<pre>   procedure CreateParams(var Params: TCreateParams); override;
</pre>

<hr>

<p>Then, in the actual CreateParams() method, specify something
like this:</p>

<hr>

<pre>   procedure TForm1.Createparams(var Params: TCreateParams);
   begin
        inherited CreateParams(Params);
        with Params do
           Style := (Style or WS_POPUP) and (not WS_DLGFRAME);
   end;
</pre>

<hr>

<p>Hopefully, you'll provide some UI mechanism for moving and
closing the window.</p>


<P><H1><A NAME="forms8">Floating toolbar - here's some code to do it</P></A></H1>
<P><I>From: ao@atlas.sto.foa.se (Anders Ohlsson)</I></P>

Someone asked for some code to make a form with no title bar moveable, kind of like a
floating toolbar, for example FreeDock. Actually, for some of the stuff in here I
spied on the FreeDock sources...<p>

This requires the use of some WinAPI functions. All WinAPI functions are however
available at a touch of a key (F1 - OnLine Help)...<p>

Here's some code that does this (about 100 lines)... <p>

To make this work like intended: <p>

  OR start a new project, make the form's borderstyle bsNone, add a panel, set the border
  style of the panel to bsSingle, add another panel with some caption, add a button that
  says 'toggle title bar', cut out the below code and insert it were it should be, enable
  the panel's three event handlers (MouseDown, MouseMove, MouseUp), enable the button's
  event handler (Click). Hope I didn't forget anything... ;-) It's done faster in Delphi
  than it's written here... ;-) <p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -