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

📄 zmisc3.htm

📁 对于学习很有帮助
💻 HTM
📖 第 1 页 / 共 3 页
字号:
a quick look at this.<P>

When you declare a variable of some classtype such as... <P>

<HR><PRE>var 
   MyVar: TMyClass;
</PRE><HR>
....all you've asked the compiler to do is set up enough space to hold a 
pointer to an instance of your class on the heap. You haven't allocated 
memory for that class, just allocated memory for the pointer. I'd like to 
say that the compiler always presets this pointer to $FFFFFFFF, but that 
may not be accurate. Anyway, suffice it to say that it does *not* point 
to a valid memory location, and does *not* contain your class' 
information.<P>

Delphi handles all the messiness of memory allocation and disposal for 
you, but you do have to do a little bit of work. When you use one of 
Delphi's classes, or derive one of your own, you must instantiate the 
object. What that means is this: you must allocate the memory for it and 
set the pointer to that block of memory. In some languages that would be 
ugly; in Delphi it's as easy as...<P>

<HR><PRE>MyVar := TMyClass.Create;
</PRE><HR>

It's that easy because the Create constructor method of the class 
TMyClass is a class method--it operates on the class, not on the 
individual object. When you call the constructor, Delphi allocates 
memory, and returns a pointer value. Take a look: doesn't it look like a 
function call? Well, if you weren't sure what it was returning, now you 
know. The call to TMyClass.Create returns a pointer to an object of type 
TMyClass.<P>

In the end, all you really need to remember is this... <P>
<OL>
	<LI>Declare an object variable of some type.
 	<LI>Instantiate that object with a call to the class constructor method.
	<LI>Use the object as normal.
	<LI>Free the object.
</OL>

==begin useless code block<P>

<HR><PRE>procedure Example;
var
   MyObj: TMyClass;   // a class that you've created
   MyList: TList;     // a native class
begin
   MyObj := TMyClass.Create;  
// now MyObj is instantiated--it means something
   MyList := TList.Create;    
// same for MyList
....
do some stuff here
....
   MyList.Free;
// MyList's resources are cleared from the heap
   MyObj.Free;
// same for MyObj
end;
</PRE><HR>


<P><H1><A NAME="zmisc35">Example of variable number of parameters</P></A></H1>
<P><I>From: hallvard@falcon.no (Hallvard Vassbotn)</I></P>

<HR><PRE>program VarPar;

{ A simple program to demonstrate use of type-safe variable number of
  parameters in Delphi.

  Written Mars 1995 by Hallvard Vassbotn
  hallvard@falcon.no
}

uses WinCrt, SysUtils;

{ These are predefined in System:
const
    vtInteger  = 0;
    vtBoolean  = 1;
    vtChar     = 2;
    vtExtended = 3;
    vtString   = 4;
    vtPointer  = 5;
    vtPChar    = 6;
    vtObject   = 7;
    vtClass    = 8;

  type
    TVarRec = record
      case Integer of
        vtInteger:  (VInteger: Longint; VType: Byte);
        vtBoolean:  (VBoolean: Boolean);
        vtChar:     (VChar: Char);
        vtExtended: (VExtended: PExtended);
        vtString:   (VString: PString);
        vtPointer:  (VPointer: Pointer);
        vtPChar:    (VPChar: PChar);
        vtObject:   (VObject: TObject);
        vtClass:    (VClass: TClass);
    end;
}

const
  TypeNames : array [vtInteger..vtClass] of PChar  =
   ('Integer', 'Boolean', 'Char', 'Extended', 'String',
    'Pointer', 'PChar', 'Object', 'Class');

{
According to the on-line docs (search for TVarRec), array of const
parameters are treated like array of TVarRec by the compiler.
This example will work just as well if you change the declaration of
TestMultiPar to:

  procedure TestMultiPar(const Args: array of TVarRec);

This would make the implementation of the routine cleaner (no absolute
variable declaration), but the interface would be less understandable
to the user of the routine.

The compiler looks at the parameters and builds the array directly on the
stack. For each item in the array it also sets the VType field to one
of the pre-defined constants vtXXXX. The actual value is always sent as
four bytes of information. For the Boolean and Char types, only the first
byte contains useful information.

So, go ahead, now you can write all those neat routines with variable
number of parameters - and still keep the type safety!
}

function PtrToHex(P: pointer): string;
begin
  Result := IntToHex(Seg(P^), 4) + ':' + IntToHex(Ofs(P^), 4);
end;

procedure TestMultiPar(const Args: array of const);
var
  ArgsTyped : array [0..$fff0 div sizeof(TVarRec)] of TVarRec absolute Args;
  i         : integer;
begin
  for i := Low(Args) to High(Args) do
    with ArgsTyped[i] do
    begin
      Write('Args[', i, '] : ', TypeNames[VType], ' = ');
      case VType of
        vtInteger:  writeln(VInteger);
        vtBoolean:  writeln(VBoolean);
        vtChar:     writeln(VChar);
        vtExtended: writeln(VExtended^:0:4);
        vtString:   writeln(VString^);
        vtPointer:  writeln(PtrToHex(VPointer));
        vtPChar:    writeln(VPChar);
        vtObject:   writeln(PtrToHex(Pointer(VObject)));
        vtClass:    writeln(PtrToHex(Pointer(VClass)));
      end;
    end;
end;

var
  MyObj : TObject;
begin
  Writeln('Test of type-safe variable number of parameters in Delphi:');
  MyObj := TObject.Create;
  TestMultiPar([123, 45.67, PChar('ASCIIZ'), 'Hello, world!', true, 'X',
                @ShortDayNames, TObject, MyObj]);
  MyObj.Free;

  { To verify that the type-safety is used in the supplied formatting routines,
    try this: }
  writeln(Format('%d', ['hi']));
  { The supplied parameter is not of the type expected. The '%d' format string
    signals that the parameter should be an integer value, but instead we
    send a string. At run-time this will generate a exception, and if you
    have enabled IDE-trapping of exceptions, Delphi will show you the offending
    line. Using c-type sprintf funtions like this will result in undefined
    behaviour (read: system crash, GP or whatever) }
end.
</PRE><HR>

<P><H1><A NAME="zmisc36">My new TWrapGrid component: Allows word wrap in TStringGrid.</P></A></H1>
<P><I>From: delarosa@ix.netcom.com (Luis de la Rosa)</I></P>

I have finally created a custom component, TWrapGrid that allows you to
use a TStringGrid, but also wrap the text in a cell.
This is the beta version, so I encourage you to experiment with it,
try it out, and send me comments on what you think of it.
When you use it, remember to se the RowHeights (or DefaultRowHeight)
large enough so that when it wraps, it shows up in the cell.<P>

To install, copy the following text and paste it into a Unit.  Save it
under the name 'Wrapgrid.PAS'. Then follow the directions I put in the
header of the component.<P>

I'm also looking for feedback on this component, so please try it and tell me
what you think.

Here is the code! <P>

<HR><PRE>
{  This is a custom component for Delphi.
   It is wraps text in a TStringGrid, thus the name TWrapGrid.
   It was created by Luis J. de la Rosa.
   E-mail: delarosa@ix.netcom.com
   Everyone is free to use it, distribute it, and enhance it.

   To use:  Go to the 'Options' - 'Install Components' menu selection in Delphi.
            Select 'Add'.
            Browse for this file, which will be named 'Wrapgrid.PAS'.
            Select 'OK'.
            You have now added this to the Samples part of your component
               palette.
            After that, you can use it just like a TStringGrid.

   Please send any questions or comments to delarosa@ix.netcom.com
   Enjoy!

   A few additional programming notes:
   I have overridden the Create and DrawCell methods.  Everything else should
   behave just like a TStringGrid.
   The Create sets the DefaultDrawing to False, so you don't need to.

   Also, I am using the pure block emulation style of programming, making my
   code easier to read.
}
   
unit Wrapgrid;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, Grids;

type
  TWrapGrid = class(TStringGrid)
  private
    { Private declarations }
  protected
    { Protected declarations }
    { This DrawCell procedure wraps text in the grid cell }
    procedure DrawCell(ACol, ARow : Longint; ARect : TRect;
      AState : TGridDrawState); override;
  public
    { Public declarations }
    { The Create procedure is overriden to use the DrawCell procedure by
         default }
    constructor Create(AOwner : TComponent); override;
  published
    { Published declarations }
  end;

procedure Register;

implementation

constructor TWrapGrid.Create(AOwner : TComponent);
begin
   { Create a TStringGrid }
   inherited Create(AOwner);

   { Make the drawing use our DrawCell procedure by default }
   DefaultDrawing := FALSE;
end;

{ This DrawCell procedure wraps text in the grid cell }
procedure TWrapGrid.DrawCell(ACol, ARow : Longint; ARect : TRect;
   AState : TGridDrawState);
var
   Sentence,                  { What is left in the cell to output }
   CurWord : String;          { The word we are currently outputting }
   SpacePos,                  { The position of the first space }
   CurX,                      { The x position of the 'cursor' }
   CurY : Integer;            { The y position of the 'cursor' }
   EndOfSentence : Boolean;   { Whether or not we are done outputting the cell }
begin
   { Initialize the font to be the control's font }
   Canvas.Font := Font;

   with Canvas do begin
      { If this is a fixed cell, then use the fixed color }
      if gdFixed in AState then begin
         Pen.Color   := FixedColor;
         Brush.Color := FixedColor;
      end
      { else, use the normal color }
      else begin
         Pen.Color   := Color;
         Brush.Color := Color;
      end;

      { Prepaint cell in cell color }
      Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
   end;

   { Start the drawing in the upper left corner of the cell }
   CurX := ARect.Left;
   CurY := ARect.Top;

   { Here we get the contents of the cell }
   Sentence := Cells[ACol, ARow];

   { for each word in the cell }
   EndOfSentence := FALSE;
   while (not EndOfSentence) do begin
      { to get the next word, we search for a space }
      SpacePos := Pos(' ', Sentence);
      if SpacePos > 0 then begin
         { get the current word plus the space }
         CurWord := Copy(Sentence, 0, SpacePos);

         { get the rest of the sentence }
         Sentence := Copy(Sentence, SpacePos + 1, Length(Sentence) - SpacePos);
      end
      else begin
         { this is the last word in the sentence }
         EndOfSentence := TRUE;
         CurWord := Sentence;
      end;

      with Canvas do begin
         { if the text goes outside the boundary of the cell }
         if (TextWidth(CurWord) + CurX) > ARect.Right then begin
            { wrap to the next line }
            CurY := CurY + TextHeight(CurWord);
            CurX := ARect.Left;
         end;

         { write out the word }
         TextOut(CurX, CurY, CurWord);
         { increment the x position of the cursor }
         CurX := CurX + TextWidth(CurWord);
      end;
   end;
end;

procedure Register;
begin
   { You can change Samples to whichever part of the Component Palette you want
     to install this component to }
   RegisterComponents('Samples', [TWrapGrid]);
end;

end.
</PRE><HR>

<P><H1><A NAME="zmisc37">Resizing panels?</P></A></H1>
<P><I>From: dionkk@ix.netcom.com (Dion Kurczek)</I></P>

Here's the source code for a resizable panel.  Give the panel an align 
property of alClient, throw some controls on it, and watch them resize 
at run time when you resize the form.  There is some code that prohibits 
resizing during design time, but this can be taken out.  This may not be 
perfect, because I threw it together in a few minutes, but it's worked 
for me so far.<P>

<HR><PRE>
unit Elastic;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, ExtCtrls;

type
  TElasticPanel = class( TPanel )
  private
     FHorz, FVert: boolean;
     nOldWidth, nOldHeight: integer;
     bResized: boolean;
  protected
     procedure WMSize( var message: TWMSize ); message WM_SIZE;
  public
     nCount: integer;
     constructor Create( AOwner: TComponent ); override;
  published
     property ElasticHorizontal: boolean read FHorz write FHorz default 
TRUE;
     property ElasticVertical: boolean read FVert write FVert default 
TRUE;
  end;

procedure Register;

⌨️ 快捷键说明

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