1、 VER_PLATFORM_WIN32_NT: Do_SomeThingElse; end;end;B 内存知多少? 下面介绍一种方法可以决定系统内存的多少、使用状态等信息。更重要的是,应用程序可以利用这项技术来决定客户机的可用内存的大小,利用这些信息,应用程序可以动态地优化程序的性能。例如,如果有足够的内存可以利用双缓存优化位图的操作。利用Windows API函数GlobalMemoryStatus可以完成上述功能。GlobalMemoryStatus接收一个类型为TMemoryStatus的变参,通过这个参数就可以获得Windows当前的内存状态。TMemoryStatus的结构如下:t
2、ypedef struct _MEMORYSTATUS / mst DWORD dwLength; / sizeof(MEMORYSTATUS),该记录结构的大小 DWORD dwMemoryLoad; / 使用内存所占百分比 DWORD dwTotalPhys; / 物理内存的字节数 DWORD dwAvailPhys; / 自由物理可用内存字节数 DWORD dwTotalPageFile; / 页文件字节数 DWORD dwAvailPageFile; / 页文件的自由字节数 DWORD dwTotalVirtual; / 地址空间的用户字节数 DWORD dwAvailVirtual;
3、 / 自由用户字节数 MEMORYSTATUS, *LPMEMORYSTATUS;下面是使用GlobalMemoryStatus函数的一个例子: MemoryStatus: TMemoryStatus; MemoryStatus.dwLength := sizeof(MemoryStatus); GlobalMemoryStatus(MemoryStatus); Label1.Caption := Total Physical Memory: + IntToStr(MemoryStatus.dwTotalPhys);C 获得消逝时间在测试硬件或软件的效率时或跟踪用户的响应速度时,需要测定消逝的
4、时间。多数程序员使用一个TDateTime变量和Now函数来实现测定消逝时间的目的。但是,一种更简单的方法是使用Windows API函数GetTickCount。GetTickCount函数返回从启动Windows后消逝的毫秒数。如果函数成功地返回,返回值就是从启动Windows后消逝的毫秒数。下面是一个使用实例: i: longint; StartTime, EndTime: Double;const CLOCK_TICK: Double = 1000; i := 0; StartTime := GetTickCount; while (i 10000000) do i:= i+1; En
5、dTime := GetTickCount - StartTime; ShowMessage(Format(消逝时间: %0.2f 秒,EndTime/CLOCK_TICK);D 隐藏/显示Windows 95的任务栏想不想让你编写的Delphi程序具有隐藏/显示Windows 95任务栏的功能,在程序中使用下面的两个过程就可以实现这一功能。procedure hideTaskbar;var wndHandle : THandle;wndClass : array0.50 of Char; StrPCopy(wndClass0, Shell_TrayWnd); wndHandle := Fin
6、dWindow(wndClass0, nil); / 隐藏任务栏 ShowWindow(wndHandle, SW_HIDE);procedure showTaskbar; / 显示任务栏 ShowWindow(wndHandle, SW_RESTORE);E 捕获文件的日期和时间标志希望显示文件的日期和时间标志吗?Delphi中没有一个简单的函数来完成这项功能,但是我们可以将两个函数结合起来实现这一功能。首先,FileGetDate函数返回文件的DOS日期和时间,然后,FileDateToDateTime函数将日期和时间转换为TDateTime类型的变量,最后,DateTimeToStr过程
7、将TDateTime类型的变量转换为字符串。实例如下:procedure TForm1.Button1 TheFileDate: string; Fhandle: integer; FHandle := FileOpen(YourFileName, 0); tryTheFileDate := DateTimeToStr(FileDateToDateTime(FileGetDate(FHandle); finallyFileClose(FHandle);使用DateTimeToStr的格式化参数可以调整输出结果的形式。即使你不需要显示日期和时间,也可以使用这项技术比较和计算文件日期。F 避免驱动
8、器A没有准备好错误(Not Ready error)当你的程序存取A驱动器时,可能会被Drive Not Ready系统错误所中断,可以使用下面的函数来测试驱动器,以避免这种情况发生,代码如下:function DiskInDrive(Drive: Char): Boolean; ErrorMode: word; Drive: = UpCase(Drive); if not (Drive in A.Z) then raise EConvertError.Create(Not a valid drive ID ErrorMode := SetErrorMode(SEM_FailCriticalE
9、rrors); if DiskSize(Ord(Drive) - $40) = -1 then DiskInDrive := False else= True; SetErrorMode(ErrorMode);本函数的工作原理是:首先将驱动器符转换为大写字母,然后关闭系统错误报告功能,执行磁盘操作,操作成功返回True,表明驱动器里存在磁盘;操作失败返回False,表明发生错误,函数结束时打开系统错误报告功能。G 隐藏应用程序假如你不仅想让应用程序隐藏窗体,同时不想让应用程序在任务栏上显示,可以使用如下命令: ShowWindow (Application.handle, SW_HIDE);这
10、条命令对使用托盘区(System Tray)图标来激活的应用程序十分有用。H 重定向DOS应用程序有时,你需要重定向一个DOS应用程序。下面的代码可以帮助你完成这项工作:-CreateDOSProcessRedirected- Description : executes a (DOS!) app defined in the CommandLine parameter redirected to take input from InputFile and give output to OutputFile Result : True on success Parameters : Comma
11、ndLine : the command line for the app, including its full path InputFile : the ascii file where from the app takes input OutputFile : the ascii file to which the apps output is redirected ErrMsg : additional error message string. Can be empty Error checking : YES Target : Delphi 2, 3, 4 Author : The
12、odoros Bebekis, email bebekisotenet.gr Notes : Example call : CreateDOSProcessRedirected(C:MyDOSApp.exe,InputPut.txtOutPut.txtPlease, record this message)-function CreateDOSProcessRedirected(const CommandLine, InputFile, OutputFile, ErrMsg :string):boolean; ROUTINE_ID = function: CreateDOSProcessRed
13、irected ; OldCursor : TCursor; pCommandLine : array0.MAX_PATH of char; pInputFile, pOutPutFile : StartupInfo : TStartupInfo; ProcessInfo : TProcessInformation; SecAtrrs : TSecurityAttributes; hAppProcess, hAppThread, hInputFile, hOutputFile := False; Check for InputFile existence if not FileExists(I
14、nputFile) thenraise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +Input file * %s * + #10 +does not exist + #10 + #10 + ErrMsg, InputFile); Save the cursor = Screen.Cursor; Screen.Cursor := crHourglass; Copy the parameter Pascal strings to null terminated strings StrPCopy(pCommandLine, CommandLine); S
15、trPCopy(pInputFile, InputFile); StrPCopy(pOutPutFile, OutputFile); TRY Prepare SecAtrrs structure for the CreateFile calls. This SecAttrs structure is needed in this case because we want the returned handle can be inherited by child process. This is true when running under WinNT. As for Win95, the d
16、ocumentation is quite ambiguous FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);SecAtrrs.nLength := SizeOf(SecAtrrs);SecAtrrs.lpSecurityDescriptor := nil;SecAtrrs.bInheritHandle : Create the appropriate handle for the input file hInputFile := CreateFile( pInputFile, pointer to name of the file GENERIC_READ
17、 or GENERIC_WRITE, access (read-write) mode FILE_SHARE_READ or FILE_SHARE_WRITE, share mode SecAtrrs, pointer to security attributes OPEN_ALWAYS, how to create FILE_ATTRIBUTE_NORMAL or FILE_FLAG_WRITE_THROUGH, file attributes 0 ); handle to file with attributes to copy Is hInputFile a valid handle?
18、if hInputFile = INVALID_HANDLE_VALUEthen raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +WinApi function CreateFile returned an + invalid handle valuefor the input file * %s * Create the appropriate handle for the output file hOutputFile : pOutPutFile, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_REA
19、D or FILE_SHARE_WRITE, SecAtrrs, CREATE_ALWAYS, how to create or FILE_FLAG_WRITE_THROUGH, file attributes Is hOutputFile a valid handle?if hOutputFile = INVALID_HANDLE_VALUE +for the output file * %s * ErrMsg, OutputFile); Prepare StartupInfo structure FillChar(StartupInfo, SizeOf(StartupInfo), #0);
20、StartupInfo.cb := SizeOf(StartupInfo);StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;StartupInfo.wShowWindow := SW_HIDE;StartupInfo.hStdOutput := hOutputFile;StartupInfo.hStdInput := hInputFile; Create the app Result := CreateProcess(nil, pointer to name of executable module pCo
21、mmandLine, pointer to command line string nil, pointer to process security attributes pointer to thread security attributes True, handle inheritance flag HIGH_PRIORITY_CLASS, creation flags pointer to new environment block pointer to current directory name StartupInfo, pointer to STARTUPINFO Process
22、Info); pointer to PROCESS_INF wait for the app to finish its job and take the handles to free them later if Result begin WaitforSingleObject(ProcessInfo.hProcess, INFINITE); hAppProcess := ProcessInfo.hProcess; hAppThread := ProcessInfo.hThread; endelse raise Exception.Create(ROUTINE_ID + #10 + #10
23、+Function failure ErrMsg); FINALLY Close the handles. Kernel objects, like the process and the files we created in this case, are maintained by a usage count. So, for cleaning up purposes, we have to close the handles to inform the system that we dont need the objects anymore if hOutputFile 0 then CloseHandle(hOutputFile);if hInputFile 0 then CloseHandle(hInputFile);if hAppThread 0 then CloseHandle(hAppThread);if hAppProcess 0 then CloseHandle(hAppProcess); Restore the old cursor Screen.Cursor:= OldCursor; END; CreateDOSProcessRedirected
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1