J
jilimi
Unregistered / Unconfirmed
GUEST, unregistred user!
新建一个应用程序,在FORM1上放入四个BUTTON,NAME分别为:ShowBtn,HideBtn,WriteBtn,ExitBtn;<br>Caption分别为:Show,Hide,Write Message,Exit。代码如下:<br><br>unit main;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> ShowBtn: TButton;<br> HideBtn: TButton;<br> ExitBtn: TButton;<br> WriteBtn: TButton;<br> procedure MyConsoleWndProc(var Message: TMessage);<br> procedure FormCreate(Sender: TObject);<br> procedure FormDestroy(Sender: TObject);<br> procedure ShowBtnClick(Sender: TObject);<br> procedure HideBtnClick(Sender: TObject);<br> procedure ExitBtnClick(Sender: TObject);<br> procedure WriteBtnClick(Sender: TObject);<br> private<br> FConsoleHanle :Integer;<br> FConsoleWnd :HWND;<br> FDefConsoleProcointer;<br> function GetConsoleWHND: HWND;<br> procedure SetConsoleWin(bz: integer);<br> procedure HideConsoleWin;<br> procedure ShowConsoleWin;<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>function MyConRoutine(dwCtrlTypeWORD):BOOL; // control signal type<br>begin<br> Result:=BOOL(True);<br>end;<br><br>function TForm1.GetConsoleWHND:HWND;<br>begin<br> if Win32Platform=VER_PLATFORM_WIN32_NT then //若操作系统为 NT,则找'ConsoleWindowClass'<br> Result:=FindWindow('ConsoleWindowClass',nil)<br> else //操作系统为 Windows,则找'tty'<br> Result:=FindWindow('tty',nil);<br>end;<br><br>procedure TForm1.MyConsoleWndProc(var Message: TMessage);<br>begin<br> with Message do<br> case Msg of<br> WM_SYSCOMMAND:<br> Result:=1;<br> else<br> Result := CallWindowProc(FDefConsoleProc, FConsoleWnd, Msg, wParam, lParam);<br> end;<br>end;<br><br>Procedure TForm1.SetConsoleWin(bz:integer);<br>begin<br> showwindow(FConsoleWnd,bz);<br>end;<br><br>Procedure TForm1.ShowConsoleWin;<br>begin<br> SetConsoleWin(SW_SHOW);<br>end;<br><br>Procedure TForm1.HideConsoleWin;<br>begin<br> SetConsoleWin(SW_HIDE);<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br> ACoord : TCOORD;<br>begin<br> AllocConsole;<br> SetConsoleTitle('信息窗口');<br> SetConsoleCtrlHandler(@MyConRoutine,True);<br> FConsoleHanle:=GetStdHandle(STD_OUTPUT_HANDLE);<br> ACoord.X :=400;<br> ACoord.Y :=100;<br> SetConsoleScreenBufferSize(FConsoleHanle,ACoord);<br> SetConsoleMode(FConsoleHanle,ENABLE_PROCESSED_OUTPUT);<br> FConsoleWnd :=GetConsoleWHND;<br> SetWindowLong(FConsoleWnd,GWL_STYLE,GetWindowLong(FConsoleWnd,GWL_STYLE) xor WS_SYSMENU xor WS_MINIMIZEBOX);<br> FDefConsoleProc:=Pointer(SetWindowLong(FConsoleWnd, GWL_WNDPROC,Longint(MakeObjectInstance(MyConsoleWndProc))));<br> SendMessage(FConsoleWnd, WM_NULL, 0, 0);<br>end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br> FreeConsole;<br> FreeObjectInstance(Pointer(SetWindowLong(FConsoleWnd, GWL_WNDPROC, Integer(FDefConsoleProc))));<br> SetConsoleCtrlHandler(@MyConRoutine,False);<br>end;<br><br>procedure TForm1.ShowBtnClick(Sender: TObject);<br>begin<br> ShowConsoleWin;<br>end;<br><br>procedure TForm1.HideBtnClick(Sender: TObject);<br>begin<br> HideConsoleWin;<br>end;<br><br>procedure TForm1.ExitBtnClick(Sender: TObject);<br>begin<br> Close;<br>end;<br><br>procedure TForm1.WriteBtnClick(Sender: TObject);<br>begin<br> writeln('test message');<br>end;<br><br>end.<br><br> 在WIN9X下,如果选择了“标记”功能(工具栏中的第一个按钮),在控制台中选取了一块区域的话,<br>则退程序时,将无法正常关闭此控制台。如果是在WINNT/2000下,则不需选择“标记”功能,只要在控<br>制台中选取了一块区域就会出现此问题。<br> 请问这是什么原因?要如何解决?<br> 我想,如果禁用掉此“标记”功能,也许可以解决问题,但要如何才能做到呢?在WINNT/2000下又<br>要如何做呢?<br> 还有一个问题:运行程序后,如果直接关闭此控制台(点击右上角的X),则主程序也会跟着一起退<br>出,可我不希望这样,有办法解决吗?