📄 pascal编程技巧.htm
字号:
<html><head><style type="text/css"><!--.p9 { font-family: "宋体"; font-size: 9pt}a {text-transform: none; text-decoration: none;}a:hover {text-decoration: underline; color: #FF0000;}--></style><title></title></head><body background="di2001.jpg"><h3 align="center"><font COLOR="#AOAO99">Pascal编程技巧</font></h3><table width="100%"> <tr> <td><font color="0000FF"><a name="1">DELPHI4.0动态数组</a></font></td> </tr> <tr> <td> 方法:<br> var<br> A:array of Integer;<br> begin<br> ShowMessage(IntToStr(High(A))); //<- 值为1<Br> SetLenght(A,3);<br> ShowMessage(IntToStr(High(A))); //<- 值为3<Br> end;<br> 注:这是DELPHI4.0新添的功能.<br> </td> </tr> <tr> <td><font color="0000FF"><a name="2">6.DELPHI中怎样建立一个动态数组? 如何Resize 一个数组? </a></font></td> </tr> <tr> <td>问 How Do I Create A Dynamic Arrays In Delphi? And How Can I Resize A Array?<br> 答 You cannot resize a non-dynamic array in Pascal. You can<br> create and resize a dynamically created array. To do this, you<br> must create the dynamic array, turn range checking off,<br> and access the array members via a variable only, or you<br> will receive runtime and compile time errors. Since you will<br> access the array through a pointer variable, you can dynamically<br> resize the array by creating a new array in memory, then copy all the<br> valid elements of the original array to the new array, then free the<br> memory<br> for the original array, and assign the new array's pointer back to the<br> original array pointer.<br> Example:<br> type<br> TSomeArrayElement = integer;<br> PSomeArray = ^TSomeArray;<br> TSomeArray = array[0..0] of TSomeArrayElement;<br> procedure CreateArray(var TheArray : PSomeArray;<br> NumElements : longint);<br> begin<br> GetMem(TheArray, sizeof(TSomeArrayElement) * NumElements);<br> end;<br> procedure FreeArray(var TheArray : PSomeArray;<br> NumElements : longint);<br> begin<br> FreeMem(TheArray, sizeof(TSomeArrayElement) * NumElements);<br> end;<br> procedure ReSizeArray(var TheArray : PSomeArray;<br> OldNumElements : longint;<br> NewNumElements : longint);<br> var<br> TheNewArray : PSomeArray;<br> begin<br> GetMem(TheNewArray, sizeof(TSomeArrayElement) * NewNumElements);<br> if NewNumElements > OldNumElements then<br> Move(TheArray^,<br> TheNewArray^,<br> OldNumElements * sizeof(TSomeArrayElement)) else<br> Move(TheArray^,<br> TheNewArray^,<br> NewNumElements * sizeof(TSomeArrayElement));<br> FreeMem(TheArray, sizeof(TSomeArrayElement) * OldNumElements);<br> TheArray := TheNewArray;<br> end;<br> procedure TForm1.Button1Click(Sender: TObject);<br> var<br> p : PSomeArray;<br> i : integer;<br> begin<br> {$IFOPT R+}<br> {$DEFINE CKRANGE}<br> {$R-}<br> {$ENDIF}<br> CreateArray(p, 200);<br> for i := 0 to 199 do<br> p^[i] := i;<br> ResizeArray(p, 200, 400);<br> for i := 0 to 399 do<br> p^[i] := i;<br> ResizeArray(p, 400, 50);<br> for i := 0 to 49 do<br> p^[i] := i;<br> FreeArray(p, 50);<br> {$IFDEF CKRANGE}<br> {$UNDEF CKRANGE}<br> {$R+}<br> {$ENDIF}<br> end;<br> -----------------<br> 另外,在使用动态实数数组的时候,数组元素类型最好声明为 single 或 double<br> 因为 single 和 double 类型使用的是 FPU 运算,而 real 不是<br> ---------------------<br> 回答2<br> It seems that the "best" way to deal with this is not necessarily by<br> declaring a pointer type and doing dynamic memory allocation etc.(as most of<br> you approached this problem) but (as one of you pointed out) to use Delphi's<br> "Variant support routines" (see Help) where you can create dynamic arrays of<br> Variants and manipulate them using the SYSTEM unit. Some of the function<br> names are:<br> VarArrayCreate -- Creates a variant array of given bounds and type.<br> VarArrayDimCount -- Returns the number of dimensions of a given variant.<br> VarArrayHighBound -- Returns the high bound of a given dimension of a given variant array.<br> VarArrayLock -- Locks a given variant array.<br> VarArrayLowBound -- Returns the low bound of a given dimension of a given variant array.<br> VarArrayOf -- Returns a given variant array with specified elements.<br> VarArrayRedim -- Resizes a given variant array by changing the high bound.<br> VarArrayUnLock -- Unlocks a given variant array.<br> </td> </tr> <tr> <td><font color="0000FF"><a name="3">怎样启动16位的应用程序? </a></font></td> </tr> <tr> <td>In a D3/Win95/Word 6.0 environment:<br> I need to bring up winword for the user if it's not already up, in a way<br> that will leave it running after my app is down. Can't use WinExec (etc.) as it's<br> a 16bit version of winword. There has to be some more-or-less direct way<br> of doing this... but all the documented methods assume 32bit. Any<br> suggestions?<br> 回复:<br> Try the following. It uses the ExecuteFile() function that can be found in the FMXUtils.pas file that came in the Demos\Doc\Filemanex directory with Delphi 1.0 and 2.0. (As far as I know it should also be in D3) ExecuteFile() is basically just a wrapper for the ShellExecute() function so this shows how to convert the params from pascal for<br> use with it. I used WinSight32 to find WinWord's ClassName which is used in the FindWindow() function. I have<br> no idea why it's OpusApp?? This will open it if it's not already running and will bring it to the front if it is<br> running.<br> uses<br> ShellAPI;<br> function ExecuteFile(const FileName, Params, DefaultDir: string;<br> ShowCmd: Integer): THandle;<br> var<br> zFileName, zParams, zDir: array[0..79] of Char;<br> begin<br> Result := ShellExecute(Application.MainForm.Handle, nil,<br> StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),<br> StrPCopy(zDir, DefaultDir), ShowCmd);<br> end;<br> procedure TForm1.Button1Click(Sender: TObject);<br> begin<br> if FindWindow('OpusApp',Nil) = 0 then<br> ExecuteFile('E:\MSOFFICE\WINWORD\WINWORD.EXE','','',SW_SHOW)<br> else<br> if IsIconic(FindWindow('OpusApp',Nil)) then<br> ShowWindow(FindWindow('OpusApp',Nil),SW_MAXIMIZE)<br> else<br> BringWindowToTop(FindWindow('OpusApp',Nil));<br> end;<br> -------------------<br> 另一回复:<br> In addition to Rodney's reply, you really should try the WinExec function which you say you cannot use. In D2 you can just do:<br> if WinExec(pchar(16exenamestring),SW_NORMAL) <32 then<Br> ShowMessage('program did not start'); {otherwise the program started}<br> </td> </tr> <tr> <td><font color="0000FF"><a name="4">怎样发现是否有 SOFTICE在运行?</a></font></td> </tr> <tr> <td> ----------一个有意思的话题<br> I am trying to figure out how can I check if NuMega SoftIce is running? Any API calls?<br> function SoftIce95Running : Boolean;<br> var<br> hFile : THandle;<br> begin<br> Result := False;<br> hFile := CreateFile ('\\.\SICE',<br> GENERIC_READ or GENERIC_WRITE,<br> FILE_SHARE_READ or FILE_SHARE_WRITE,<br> nil,<br> OPEN_EXISTING,<br> FILE_ATTRIBUTE_NORMAL,<br> 0);<br> if hFile <> INVALID_HANDLE_VALUE then begin<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -