Author: Subha Narayanan
Interaction between your Console applications and your forms
Answer:
This code demonstrates how to combine console API with usual forms
Create a new -> console application and save it as listing2
Copy the given code in to the console application
Create a form in the same project with name frmDialog1.dfm and dialog1.pas
In the form put three RadioButtons
Now build and run the application
Shift the focus to the console application and type 1 or 2 or 3
Based on the number typed the Radiobutton in the form will be clicked automatically
When u press ctrl + c the application exits
1 program listing2;
2 {$APPTYPE CONSOLE}3 uses4 SysUtils, Windows, Messages, Forms,
5 Dialog1 in 'Dialog1.pas' {frmDialog1};
6 7 var8 hInput: THandle;
9 inRec: TInputRecord;
10 dwCount: DWORD;
11 12 begin13 {Create a Form in the usual way. The Forms unit ensures that14 the Application object is around to "own" the form.}15 16 write('Creating the first Dialog Box...');
17 frmDialog1 := TfrmDialog1.Create(Application);
18 frmDialog1.Show;
19 Writeln('done.');
20 21 Writeln('Press 1, 2 or 3 to change the dialog box. Press Ctrl+ C to exit');
22 23 {Handle the Console input till the user cancels}24 hInput := GetStdHandle(STD_INPUT_HANDLE);
25 {GetStdHandle - Returns handle for Standard input/output device}26 27 while True do28 begin29 {Avoid blocking on user input, so the forms have a chance30 to operate as normal. If we had a message queue present, this31 would be a normal message dispatch loop.}32 Application.ProcessMessages;
33 if WaitForSingleObject(hInput, 0) = WAIT_OBJECT_0 then34 begin35 ReadConsoleInput(hInput, inRec, 1, dwCount);
36 if (inRec.EventType = KEY_EVENT) and inRec.Event.KeyEvent.bKeyDown then37 begin38 case inRec.Event.KeyEvent.AsciiChar of39 '1':
40 begin41 Writeln('->1');
42 frmDialog1.RadioButton1.Checked := True;
43 end;
44 45 '2':
46 begin47 Writeln('->2');
48 frmDialog1.RadioButton2.Checked := True;
49 end;
50 '3':
51 begin52 Writeln('->3');
53 frmDialog1.RadioButton3.Checked := True;
54 end;
55 end;
56 end;
57 end;
58 end;
59 end.