大佬教程收集整理的这篇文章主要介绍了delphi – 如何从我的GUI应用程序启动控制台窗口的处理?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
type TEnumWindowsData = record ProcessId: Cardinal; WinHandle: THandle; List: TStrings; // For test only end; PEnumWindowsData = ^TEnumWindowsData; function FindWindow(hWnd: THandle; lParam: LPARAM): BOOL; stdcall; var ParamData: TEnumWindowsData; ProcessId: Cardinal; Wintitle: arraY[0..200] of Char; // For test only begin ParamData := PEnumWindowsData(lParam)^; GetWindowThreadProcessId(hWnd,ProcessId); if ProcessId <> ParamData.ProcessId then Result := True else begin ParamData.WinHandle := hWnd; Result := false; end; // For test only GetWindowText(hWnd,Wintitle,Length(WintitlE) - 1); ParamData.List.Add(IntToStr(ProcessId) + ' ' + IntToStr(hWnd) + ' ' + WintitlE); end; procedure TForm1.button1Click(Sender: TObject); function RunApp(const AProgram: String): Cardinal; var StartupInfo: TStartupInfo; ProcessInformation: TProcessInformation; begin Result := 0; ... if CreateProcess(nil,PChar(AProgram),nil,false,NORMAL_PRIORITY_CLASS,StartupInfo,ProcessInformation) then Result := ProcessInformation.dwProcessId; ... end; var ParamData: TEnumWindowsData; begin ParamData.ProcessId := RunApp('cmd.exe /C D:\TMP\TEST.exe'); ParamData.WinHandle := 0; ParamData.List := Memo1.Lines; EnumWindows(@FindWindow,THandle(@ParamData)); FWindowHandle := ParamData.WinHandle; end;
AttachConsole
功能附加到新创建的控制台,并从该连接的控制台使用
GetConsoleWindow
功能使用窗口句柄.
以下代码最大的缺点是CreateProcess
函数在此时立即返回,当控制台尚未完全初始化时,当您尝试立即附加控制台后,您将失败.不幸的是,没有WaitForInputIdle
功能for console applications
,所以作为一种可能的解决办法,我会选择尝试在一些有限的循环计数中附加控制台,一旦成功,获取句柄并分离控制台.
在代码中可能如下所示. RunApp函数应该返回控制台窗口的句柄(假设您只能运行控制台应用程序),并且应该等待大约. 1秒钟的控制台应用程序你开始可以附加.您可以通过更改尝试变量的初始值和/或更改睡眠间隔来修改此值.
function GetConsoleWindow: HWND; stdcall; external kernel32 name 'GetConsoleWindow'; function AttachConsole(dwProcessId: DWORD): BOOL; stdcall; external kernel32 name 'AttachConsole'; function RunApp(const ACmdLine: String): HWND; var CmdLine: String; Attempt: Integer; StartupInfo: TStartupInfo; ProcessInfo: TProcessInformation; begin Result := 0; FillChar(StartupInfo,SizeOf(TStartupInfo),0); FillChar(ProcessInfo,SizeOf(TProcessInformation),0); StartupInfo.cb := SizeOf(TStartupInfo); StartupInfo.dwFlags := STARTF_USESHOWWINDOW; StartupInfo.wShowWindow := SW_SHOWNORMAL; CmdLine := ACmdLine; UniqueString(CmdLinE); if CreateProcess(nil,PChar(CmdLinE),CREATE_NEW_CONSOLE,ProcessInfo) then begin Attempt := 100; while (Attempt > 0) do begin if AttachConsole(ProcessInfo.dwProcessId) then begin Result := GetConsoleWindow; FreeConsole; Break; end; Sleep(10); Dec(Attempt); end; CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess); end; end;
那么你可以以这种方式更改您的lauched应用程序的控制台窗口的标题:
procedure TForm1.button1Click(Sender: TObject); var S: String; ConsoleHandle: HWND; begin ConsoleHandle := RunApp('cmd.exe'); if ConsoleHandle <> 0 then begin S := 'Hello! I''m your console,how can I serve ?'; SendTextmessage(ConsoleHandle,WM_SETTEXT,S); end; end;
以上是大佬教程为你收集整理的delphi – 如何从我的GUI应用程序启动控制台窗口的处理?全部内容,希望文章能够帮你解决delphi – 如何从我的GUI应用程序启动控制台窗口的处理?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。