📄 delsec07.txt
字号:
SECTION 7 - Object Pascal
This document contains information that is most often provided
to users of this section. There is a listing of common
Technical Information Documents that can be downloaded from the
libraries, and a listing of the most frequently asked
questions and their answers.
TI1171 Bug Report Form
Index Of Questions
------------------
1) I am using a form from the gallery and I am getting an 'unknown identifier'
error when I compile. What is wrong?
2) How can I do standard output with writeln?
3) How to I cast a floating point number (e.g. real) to an integer?
4) How do I make it so that when the user hits <enter>, it goes to the next
object as though he had hit the tab key? (Note: This will not work within
a DBGrid, since the next field is not a separate object.)
5) In a DBGrid, how do I Tab to the next field when the Enter Key is pressed?
6) How do I process command line parameters in a Delphi app?
7) How do you select a specific field on a TDBGrid to get focus?
8) I have a paradox table that uses a password. How do I make it so that the
form that uses the table comes up without prompting the user for the
password?
9) How do I design a form so that the display is the same no matter what the
resolution is?
10) How do I do date math on calculated fields?
11) Where is the exponent function?
12) How do I keep the form in icon form when I run it?
13) I have a form that is a sort of template. I want to be able to call the
same form several times (with different data displayed). How do I use
one form several times?
14) How can the component that was right clicked be determined while in an
event handler of a popup MenuItem?
15) How can I capture windows messages and process them before my
application.run line is executed?
16) How do I use a case statement to determine which object calls the procedure?
17) How can you do scrolling functions in a TForm component using keyboard
commands? For example, scrolling up and down when a PgUp or PgDown is
pressed. Is there some simple way to do this or does it have to be
programmed by capturing the keystrokes and manually responding to them?
18) How do I transfer the text in a TMemo component on a form to a TMemofield
in a Paradox table?
19) How do I make an Fixed Length ASCII text table from a table?
20) How do I set a format for a data field?
21) How do I format a number to place commas as a thousanth's separator?
22) I have a CPU intensive function. I want to be able to check to see if the
user wants to cancel. How do I do that?
23) How do you extract the high or low order byte from a word? How do you
insert it?
24) I have an OBJ file that has several assembler routines compiled into it.
I wouldn't want to have to rewrite them. Is there a way to use them in
a Delphi app?
26) If a component doesn't handle the windows messages that I need it to
handle, do I have to write my own version that passes the messages that
I need, or is there way to tap (from a TForm or else where) into the
message loop, and grap what I need?
27) Borland decided that accessing environment variables from Windows
Programs is a Bad Thing. Why do they force you to use the "obsolete"
WinDos unit?
28) How would I write a basic screen saver (like, blank the screen) using Delphi?
29) How can I determine the Length in pixels of a string after a specific font has
been aplied to it?
30) Where is the best place to open a splash screen on program start up?
31) How do I call a function from a DLL?
32) What is a Callback function, and how do I create one?
Questions and Answers
---------------------
1) Q: I am using a form from the gallery and I am getting an 'unknown
identifier' error when I compile. What is wrong?
A: If you change the name of some item that has code associated with it, that
code will not be changed by Delphi. Delphi does not go through your code
and second guess your intent. The code that you may assume is generated
by Delphi is actually programmed into the form by the designer of that
form. Just go through the code and make the modifications.
2) Q: How can I do standard output with writeln?
A: Include WinCRT in your uses statement.
3) Q: How to I cast a floating point number (e.g. real) to an integer?
A: There are a couple of methods.
var
i: integer;
r: real;
begin
r := 34.56;
i := trunc(r); {i = 34} {First Method}
i := round(r); {i = 35} {Second Method}
end;
4) Q: How do I make it so that when the user hits <enter>, it goes to the next
object as though he had hit the tab key? (Note: This will not work within
a DBGrid, since the next field is not a separate object.)
A: You need to trap the keystroke and set up your own response to it. Try
this:
procedure TMainForm.FormCreate(Sender: TObject);
begin
keyPreview := true; {To turn the event "ON".}
end;
procedure TMainForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then
begin
Key := #0;
PostMessage(Handle, WM_NEXTDLGCTL, 0, 0);
end;
end;
5) Q: In a DBGrid, how do I Tab to the next field when the Enter Key is pressed?
A: The action of the tab key is acted upon after the OnKeyDown event but the
Enter key is acted on in the OnKeyPress. Therefore, the Enter key has to
be accounted for in both areas for the desired effect to occur. Try this:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift:
TShiftState);
begin
if Key = VK_RETURN then Key = VK_TAB;
end;
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then Key := #9;
end;
6) Q: How do I process command line parameters in a Delphi app?
A: Use the ParamCount and ParamString methods.
7) Q: How do you select a specific field on a TDBGrid to get focus?
A: Using this code:
DBGrid1.SelectedField := Table1SomeField;
DBGrid1.SetFocus;
8) Q: I have a paradox table that uses a password. How do I make it so that the
form that uses the table comes up without prompting the user for the
password?
A: The table component's ACTIVE property must be set to FALSE. Then, put
this code on the form's create event:
session.AddPassword('My secret password');
table1.active := true;
9) Q: How do I design a form so that the display is the same no matter what the
resolution is?
A: Here is some code to show how it is done:
const
ScreenHeight: real = 800; {I designed my form in 800x600 mode.}
ScreenWidth: real = 600;
procedure TForm1.FormCreate(Sender: TObject);
var
x, y: LongInt; {Integers will not not a large enough value.}
begin
form1.scaled := true;
x := getSystemMetrics(SM_CXSCREEN);
y := getSystemMetrics(SM_CYSCREEN);
if (x <> ScreenHeight) or (y <> ScreenWidth) then
begin
form1.height := form1.height * x DIV ScreenHeight;
form1.width := form1.width * y DIV ScreenWidth;
scaleBy(x, ScreenHeight);
end;
end;
Note: you may want to add code that will change the font sizes
proportionally.
10) Q: How do I do date math on calculated fields?
A: When doing date math on calculated fields, it is important to ensure that
all values being used are properly matched as to type. The double method
(not in the docs) casts the value to a useable type.
In the following method, d1, and d2 (part of table1) can be of either
date or dateTime type and d3 is an integer field.
procedure TForm1.Table1CalcFields(DataSet: TDataset);
var
t1, t2: tDateTime;
begin
table1d1.asDateTime := Date + 2; {or table1d1.value := date + 2;}
table1d2.asDateTime := Date - 2;
t1 := table1d1.asDateTime;
t2 := table1d2.asDateTime;
table1d3.asInteger := trunc(double(t1) - double(t2));
end;
11) Q: Where is the exponent function?
A: For ExpXY(X, Y) (i.e. Y^X), try:
ExpXY := Exp(Y * Ln(X))
To use a generic function, declare the formal parameters and function
result as Extended, and the conversions from the actual parameters and
back to your result variable will be done automatically. (Note: Ln is
the natural logarithm of a number).
12) Q: How do I keep the form in icon form when I run it?
A: In the private section of the form object's declaration, put:
PROCEDURE WMQueryOpen(VAR Msg : TWMQueryOpen); message WM_QUERYOPEN;
In the implementation section, put this method:
PROCEDURE TForm1.WMQueryOpen(VAR Msg : TWMQueryOpen);
begin
Msg.Result := 0;
end;
That's it! The form will always remain iconic. OBTW, of course you must
set WindowState to wsMinimized in the form's properties initially.
13) Q: I have a form that is a sort of template. I want to be able to call the
same form several times (with different data displayed). How do I use
one form several times?
A: You need to make modeless window like this:
with TMyForm.create(self) do show;
Now we want to control it. Here is a way to change the caption of each
form that is created. You have access to it through the form's component
array. This example uses an about box (named "box") as the other form.
procedure TForm1.Button1Click(Sender: TObject);
begin
with tBox.create(self) do show;
{ComponentCount-1 is needed because it is zero based.}
(form1.Components[form1.ComponentCount-1] as tForm).caption :=
'About Box # ' + intToStr(form1.ComponentCount-2);
end;
14) Q: How can the component that was right clicked be determined while in an
event handler of a popup MenuItem?
A: Use the PopupComponent property of the PopupMenu component to determine
what control was right clicked.
procedure TForm1.PopupItem1Click(Sender: TObject);
begin
Label1.Caption := PopupMenu1.PopupComponent.ClassName;
end;
The form's ActiveControl property can also be used, however, the active
control may not necessarily be the control that caused the popup menu to
appear.
15) Q: How can I capture windows messages and process them before my
application.run line is executed?
A: The following project source demonstrates how to get Window messages
before the application's window procedure is called. It is rare, if ever,
that this needs to be done. In most cases assigning a procedure to the
Application.OnMessage will accomplish the same thing.
program Project1;
uses
Forms, messages, wintypes, winprocs,
Unit1 in 'UNIT1.PAS' {Form1};
{$R *.RES}
var
OldWndProc: TFarProc;
function NewWndProc(hWndAppl: HWnd; Msg, wParam: Word;
lParam: Longint): Longint; export;
begin
result := 0; { Default WndProc return value }
{*** Handle messages here; The message number is in Msg ***}
result := CallWindowProc(OldWndProc, hWndAppl, Msg, wParam, lParam);
end;
begin
Application.CreateForm(TForm1, Form1);
OldWndProc := TFarProc(GetWindowLong(Application.Handle,
GWL_WNDPROC));
SetWindowLong(Application.Handle, GWL_WNDPROC,
longint(@NewWndProc));
Application.Run;
end.
16) Q: How do I use a case statement to determine which object calls the
procedure?
A: Use the object's TAG property. You set the value of the tag property of
each object so that you know what it is. (Using constants that describe
the object is ideal.)
Note: This code assumes that only a tButton will call the procedure.
case (sender as tButton).tag of
0: blah;
1: blah_blah;
end;
17) Q: How can you do scrolling functions in a TForm component using keyboard
commands? For example, scrolling up and down when a PgUp or PgDown is
pressed. Is there some simple way to do this or does it have to be
programmed by capturing the keystrokes and manually responding to them.
A: Form scrolling is accomplished by modifying the VertScrollbar or
HorzScrollbar Postion properties of the form. The following code
demonstrates how to do this:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
const
PageDelta = 10;
begin
With VertScrollbar do
if Key = vk_Next then Position := Position+PageDelta
else if Key = vk_Prior then Position := Position-PageDelta;
end;
18) Q: How do I transfer the text in a TMemo component on a form to a TMemofield
in a Paradox table?
A: Here is an example.
procedure TForm1.Button1Click(Sender: TObject);
var
t: TTable;
begin
t := TTable.create(self);
with t do
begin
DatabaseName := 'MyAlias'; {personal alias}
TableName := 'MyTbl.db';
open;
edit;
insert;
fieldByName('TheField').assign(memo1.lines); {This is it!}
post; {required!!!}
close;
end; { End of the with statement. }
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -