home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1991 / number4 / prsetup.pas < prev    next >
Pascal/Delphi Source File  |  1991-08-13  |  6KB  |  225 lines

  1. { prsetup.pas -- Demonstrate printer setup dialogs }
  2.  
  3. program prSetup;
  4.  
  5. {$R prsetup.res}
  6.  
  7. uses WinTypes, WinProcs, WObjects, Strings;
  8.  
  9. const
  10.   id_Menu        = 100;  { Menu resource ID }
  11.   cm_FileSetup   = 101;  { File:Printer setup command ID }
  12.   cm_FileExit    = 102;  { File:Exit command ID }
  13.   id_Setup       = 100;  { Setup dialog resource ID }
  14.   id_ListBox     = 101;  { Setup's listbox control ID }
  15.   id_SetupButton = 102;  { Setup's setup button control ID }
  16.  
  17. type
  18.   TAppObject = object(TApplication)
  19.     procedure InitMainWindow; virtual;
  20.   end;
  21.  
  22.   PTMainWindow = ^TMainWindow;
  23.   TMainWindow = object(TWindow)
  24.     DefaultDevice: array[0 .. 40] of Char;  { Default printer }
  25.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  26.     procedure CMFileSetup(var Msg: TMessage);
  27.       virtual cm_First + cm_FileSetup;
  28.     procedure CMFileExit(var Msg: TMessage);
  29.       virtual cm_First + cm_FileExit;
  30.   end;
  31.  
  32.   PTSetupDialog = ^TSetupDialog;
  33.   TSetupDialog = object(TDialog)
  34.     Selection: PChar;             { Selected device }
  35.     constructor Init(AParent: PWindowsObject;
  36.       AName: PChar; OwnerSelection: PChar);
  37.     procedure SetupWindow; virtual;
  38.     procedure Ok(var Msg: TMessage);
  39.       virtual id_First + id_Ok;
  40.     procedure Setup(var Msg: TMessage);
  41.       virtual id_First + id_SetupButton;
  42.   end;
  43.  
  44.   TExtDeviceMode = function(HWindow: HWnd; HDriver: THandle;
  45.       DevModeOutput: PDevMode; DeviceName, OutputName: PChar;
  46.       DevModeInput: PDevMode; Profile: PChar;
  47.       Mode: Word): Integer;
  48.  
  49. var
  50.   ExtDeviceMode: TExtDeviceMode;
  51.   DeviceMode: TDeviceMode;
  52.  
  53.  
  54. {- Return pointer to next token in P or previous P if P = nil }
  55. function NextToken(P: PChar; C: Char): PChar;
  56. const
  57.   Next: PChar = nil;
  58. begin
  59.   if P = nil then P := Next;
  60.   Next := StrScan(P, C);
  61.   if Next <> nil then
  62.   begin
  63.     Next^ := #0;
  64.     Next := @Next[1]
  65.   end;
  66.   NextToken := P
  67. end;
  68.  
  69.  
  70. { TAppObject }
  71.  
  72. {- Initialize the application }
  73. procedure TAppObject.InitMainWindow;
  74. begin
  75.   MainWindow := New(PTMainWindow,
  76.     Init(nil, 'Printer Setup Demonstration'))
  77. end;
  78.  
  79.  
  80. { TMainWindow }
  81.  
  82. {- Construct main window object }
  83. constructor TMainWindow.Init(AParent: PWindowsObject;
  84.   ATitle: PChar);
  85. var
  86.   P: PChar;
  87.   Buffer: array[0 .. 1024] of Char;
  88. begin
  89.   TWindow.Init(AParent, ATitle);
  90.   Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
  91.   GetProfileString('windows', 'device', ',,', Buffer,
  92.     Sizeof(Buffer));
  93.   P := NextToken(Buffer, ',');
  94.   if P <> nil then
  95.     StrLCopy(DefaultDevice, P, 40)
  96.   else
  97.     DefaultDevice[0] := #0;
  98. end;
  99.  
  100. {- Execute File:Printer setup command }
  101. procedure TMainWindow.CMFileSetup(var Msg: TMessage);
  102. begin
  103.   Application^.ExecDialog(New(PTSetupDialog,
  104.     Init(@Self, PChar(id_Setup), DefaultDevice)))
  105. end;
  106.  
  107. {- Execute File:Exit command }
  108. procedure TMainWindow.CMFileExit(var Msg: TMessage);
  109. begin
  110.   CloseWindow
  111. end;
  112.  
  113.  
  114. { TSetupDialog }
  115.  
  116. {- Construct TSetupDialog object }
  117. constructor TSetupDialog.Init(AParent: PWindowsObject;
  118.   AName: PChar; OwnerSelection: PChar);
  119. begin
  120.   TDialog.Init(AParent, AName);
  121.   Selection := OwnerSelection;
  122. end;
  123.  
  124. {- Insert DeviceNames strings into dialog list box }
  125. procedure TSetupDialog.SetupWindow;
  126. var
  127.   I: Integer;
  128.   P: PChar;
  129.   Buffer: array[0 .. 4096] of Char;
  130. begin
  131.   GetProfileString('devices', nil, #0'', Buffer,Sizeof(Buffer));
  132.   I := 0;
  133.   P := NextToken(Buffer, #0);
  134.   while StrLen(P) <> 0 do
  135.   begin
  136.     SendDlgItemMsg(id_ListBox, lb_AddString, 0, LongInt(P));
  137.     if StrComp(Selection, P) = 0 then
  138.       SendDlgItemMsg(id_ListBox, lb_SetCurSel, I, 0);
  139.     P := NextToken(nil, #0);
  140.     Inc(I)
  141.   end;
  142. end;
  143.  
  144. {- Respond to Ok button selection }
  145. procedure TSetupDialog.Ok(var Msg: TMessage);
  146. var
  147.   Item: Word;   { Selected listbox-item index }
  148.   Len: Integer; { Length of selected item }
  149.   Buffer: array[0 .. 80] of Char;
  150. begin
  151.   Item := SendDlgItemMsg(id_Listbox, lb_GetCurSel, 0, 0);
  152.   if Item <> lb_Err then
  153.     SendDlgItemMsg(id_Listbox, lb_GetText, Item,
  154.       LongInt(Selection));
  155.   TDialog.Ok(Msg)
  156. end;
  157.  
  158. {- Respond to Setup button selection }
  159. procedure TSetupDialog.Setup(var Msg: TMessage);
  160. var
  161.   Item: Word;
  162.   DriverName, OutputName: PChar;
  163.   Buffer: array[0 .. 80] of Char;
  164.   DeviceName: array[0 .. 40] of Char;
  165.   HDriver: THandle;
  166.   Size: Integer; { Size of DevMode structure }
  167.   DriverExtName: array[0 .. 12] of Char;
  168.   DevModeOutput: PDevMode;
  169.   P: TFarProc;
  170. begin
  171.   Item := SendDlgItemMsg(id_Listbox, lb_GetCurSel, 0, 0);
  172.   if Item <> lb_Err then
  173.   begin
  174.     SendDlgItemMsg(id_Listbox, lb_GetText, Item,
  175.       LongInt(@DeviceName));
  176.     GetProfileString('devices', DeviceName, ',,', Buffer,
  177.       Sizeof(Buffer));
  178.     DriverName := NextToken(Buffer, ',');
  179.     OutputName := NextToken(nil, ',');
  180.     if (StrLen(DriverName) = 0) or (StrLen(OutputName) = 0) then
  181.     begin
  182.       MessageBox(HWindow, 'Bad device format', 'Error', mb_Ok);
  183.       Exit
  184.     end;
  185.     StrLCat(StrCopy(DriverExtName, DriverName), '.DRV', 12);
  186.     HDriver := LoadLibrary(DriverExtName);
  187.     if HDriver < 32 then
  188.       MessageBox(HWindow, 'Failed to load driver', 'Error',
  189.         mb_IconExclamation or mb_Ok)
  190.     else begin
  191.       P := GetProcAddress(HDriver, 'ExtDeviceMode');
  192.       if P <> nil then
  193.       begin
  194.         ExtDeviceMode := TExtDeviceMode(P);
  195.         Size := ExtDeviceMode(HWindow, HDriver, nil, DeviceName,
  196.           OutputName, nil, nil, 0);
  197.         GetMem(DevModeOutput, Size);
  198.         ExtDeviceMode(HWindow, HDriver, DevModeOutput,
  199.           DeviceName, OutputName, nil, nil,
  200.           dm_Prompt or dm_Copy);
  201.         FreeMem(DevModeOutput, Size)
  202.       end else
  203.       begin
  204.         P := GetProcAddress(HDriver, 'DeviceMode');
  205.         if P <> nil then
  206.         begin
  207.           DeviceMode := TDeviceMode(P);
  208.           DeviceMode(HWindow, HDriver, DeviceName, OutputName)
  209.         end
  210.       end;
  211.       FreeLibrary(HDriver);
  212.     end;
  213.   end;
  214. end;
  215.  
  216. var
  217.  
  218.   App: TAppObject;
  219.  
  220. begin
  221.   App.Init('PrSetup');
  222.   App.Run;
  223.   App.Done
  224. end.
  225.