📄 DELPHI编程手记 ㈡ 目 录.htm
字号:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="GENERATOR" content="Microsoft FrontPage 3.0"><title>DELPHI编程手记 ㈡ </title></head><body bgcolor="#FFFFFF"><p><strong><font color="#000000">◆</font><font color="#FF0000"> </strong>DELPHI编程手记 ㈡ 目 录 </font></p><p>6、<a href="question2.htm#EXECAPPLICATION">如何执行外部程序[并一直等到它结束]</a></p><p>7、<a href="question2.htm#GETAPPICATIONPATH">如何得知执行的程序所在的目录?</a> </p><p>8、<a href="question2.htm#CLOSEAPPLICATION">如何在 Delphi 应用程序中, 去关闭外部已开启的应用程序?</a></p><p>9、<a href="question2.htm#MOUSEPOSTION">如何将鼠标锁定在一定的范围内? </a></p><p>10、<a href="question2.htm#GETWINDOWSYSINFO">如何得到 Windows 的用户名称和产品序列号? </a></p><p>[ <a href="scribe.htm" target="_parent">首 页</a> | <a href="question1.htm"target="_parent"><前一页</a> | <a href="question3.htm" target="_parent">下一页></a> ]</p><hr><p><a name="EXECAPPLICATION"></a>6、如何执行外部程序[并一直等到它结束]</p><blockquote> <p>㈠ 编程语言Delphi 1.0,操作系统 Window3.1</p> <p>以下是一个执行外部程序ARJ.EXE的例子的部分代码<br> 相信看过就会用了。</p> <pre>var sCommandLine: string; bCreateProcess: boolean; lpStartupInfo: TStartupInfo; lpProcessInformation: TProcessInformation;begin sCommandLine := 'ARJ.EXE /?'; bCreateProcess := CreateProcessA(nil, PChar(sCommandLine), nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, lpStartupInfo, lpProcessInformation); if bCreateProcess then WaitForSingleObject(lpProcessInformation.hProcess, INFINITE);end;</pre> <p>㈡ 编程语言Delphi3.0,操作系统 Window95 <br> 同样是上面的例子的部分代码 </p> <pre>var pWindowsList: pointer; hActiveWindow: HWnd; hExeHandle: THandle;begin pWindowsList := DisableTaskWindows(0); hActiveWindow := GetActiveWindow; try hExeHandle := WinExec('arj.exe /?',SW_SHOWNORMAL); while GetModuleUsage(hExeHandle) <> 0 do Application.ProcessMessages; finally EnableTaskWindows(pWindowsList); SetActiveWindow(hActiveWindow); end;end;</pre> <pre>// <font color="#FF0000">相信你明白了</font>。</pre></blockquote><blockquote> <font color="#0000FF"><p>题外话:如果执行的是 MSDOS 外部程序,如何能让它的窗口不显示出来呢? </font></p> <blockquote> <pre>[ 接上例 ]:TStartupInfo 这个结构中有一个 sShowWindow 栏位, 将之设为 SW_HIDE即可,同时, dwFlags 标志中至少需含有 STARTF_USESHOWWINDOW, 否则CreateProcess时, sShowWindow 栏位的设定会无效, 以下是修改过的程式:var sCommandLine: string; bCreateProcess: boolean; lpStartupInfo: TStartupInfo; lpProcessInformation: TProcessInformation;begin // sCommandLine 的内容请视您的情况修改 sCommandLine :='Xcopy d:\temp\temp1\*.* d:\temp\temp2 /v/y'; lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW; lpStartupInfo.wShowWindow := SW_HIDE; bCreateProcess := CreateProcess(nil, PChar(sCommandLine),nil,nil,True, HIGH_PRIORITY_CLASS, nil, nil,lpStartupInfo, lpProcessInformation); if bCreateProcess then WaitForSingleObject(lpProcessInformation.hProcess, INFINITE);end;</pre> </blockquote> <pre>// <font color="#FF0000">又有进步了</font></pre></blockquote><p><br><a name="GETAPPICATIONPATH"></a>7、如何得知执行的程序所在的目录? </p><blockquote> <p>说明:Delphi 1.0 请用 ExtractFilePath(Application.Exename);(因为没有ExtractFileDir() 函数)。Delphi 2.0/3.0 的 SysUtils 单元中有 ExtractFileDir 与 ExtractFilePath两个类似的函数, 用哪一个?没有太大的关系。不过有以下的差别:<br> ExtractFilePath 传回值的最後一个字元是反斜杠“/”。</p> <p>样例代码:<br> procedure TForm1.Button1Click(Sender: TObject);<br> begin<br> ShowMessage(ExtractFileDir(Application.Exename));<br> // ie: c:\temp *)<br> ShowMessage(ExtractFilePath(Application.Exename));<br> // ie: c:\temp\ *)<br> end;</p> <blockquote> <pre><font color="#FF0000">相同点:</font> 如果执行文件在根目录下(如:C:\SAMPLE.EXE)的话, 两者的传回值相同, 且最后一个字符都是“/”。</pre> </blockquote></blockquote><p><a name="CLOSEAPPLICATION"></a>8、如何在 Delphi 应用程序中, 去关闭外部已开启的应用程序?</p><blockquote> <p>下面给出一段在 Delphi 中关闭“计算器”程序为例:<br> ...<br> var<br> HWndCalculator : HWnd;<br> begin<br> // find the exist calculator window<br> HWndCalculator := Winprocs.FindWindow(nil, '计算器');</p> <pre> // close the exist Calculator } if HWndCalculator <> 0 then SendMessage(HWndCalculator, WM_CLOSE, 0, 0);end;</pre> <pre>在此特意再摘录一段 Win32 SDK 中的说明文字:</pre> <pre>Typically, an application sends the WM_CLOSE message beforedestroying awindow, giving the window the opportunity to prompt the user for confirmation before the window is destroyed.A window that includes a System menu automatically receives the WM_CLOSE message when the user chooses the Close command from the menu. If the user confirms that the window should be destroyed,the application calls DestroyWindow. [END] </pre></blockquote><p><a name="MOUSEPOSTION"></a>9、如何将鼠标锁定在一定的范围内? </p><blockquote> <p>例子是最好的说明: </p> <pre>请在Form中放置二个 Button, 然後分别为这两个按钮定义OnClick响应事件如下: // 限制 procedure TForm1.Button1Click(Sender: TObject); var rtButton2: TRect; begin rtButton2 := Button2.BoundsRect; MapWindowPoints(handle, 0, rtButton2, 2); // 座标换算 ClipCursor(@rtButton2); // 限制鼠标移动区域 end;</pre> <pre> // 还原 procedure TForm1.Button2Click(Sender: TObject); var rtScreen: TRect; begin rtScreen := Rect(0, 0, Screen.Width, Screen.Height); ClipCursor(@rtScreen); end;</pre> <blockquote> <pre>// <font color="#FF0000">试一试吧</font></pre> </blockquote></blockquote><p><a name="GETWINDOWSYSINFO"></a>10、如何得到 Windows 的用户名称和产品序列号? </p><blockquote> <p>1. 可以用 WNetGetUser() 这个函数来得到 user name; <br> 2. Windows 95 的产品序号可以用 TRegistry 到 Registry Database 中找出来; </p> <pre>// 取得用户名称function GetUserName: AnsiString;var lpName: PAnsiChar; lpUserName: PAnsiChar; lpnLength: DWORD;begin Result := ''; lpnLength := 0; WNetGetUser(nil, nil, lpnLength); // 取得字串所需的长度 if lpnLength > 0 then begin GetMem(lpUserName, lpnLength); if WNetGetUser(lpName, lpUserName, lpnLength) = NO_ERROR then Result := lpUserName; FreeMem(lpUserName, lpnLength); end;end; { GetUserName }</pre> <pre>// 取得 Windows 产品序号function GetWindowsProductID: string;var reg: TRegistry;begin Result := ''; reg := TRegistry.Create; with reg do begin RootKey := HKEY_LOCAL_MACHINE; OpenKey('Software\Microsoft\Windows\CurrentVersion', False); Result := ReadString('ProductID'); end; reg.Free;end;</pre> <p>[ <a href="scribe.htm" target="_parent">首 页</a> |<a href="question1.htm" target="_parent"><前一页</a> | <a href="question3.htm" target="_parent">下一页></a> ] </p></blockquote><hr><blockquote> <p align="right"><em><font color="#0000FF">好望角工作室</font><br> <font color="#FF0000">纪海军</font></em></p></blockquote><p> </p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -