Solved

How do I disable File | Print menu item when no default

Posted on 1998-10-09
6
160 Views
Last Modified: 2010-04-06
How do I disable File | Print, The print menuitem and printer setup menuitem when no default printer is installed.
0
Comment
Question by:TCSCode
  • 3
  • 3
6 Comments
 
LVL 1

Accepted Solution

by:
ow earned 60 total points
ID: 1342398
Hi TCSCode,

look at the Registry and disable the MenuItem:

  var
    Registry :tRegistry;
    Print1 :tMenuItem;
    Key :string;
    EnablePrint :boolean;
.
  Registry := tRegistry.Create;
  Registry.RootKey := HKEY_CURRENT_CONFIG;
  Key := '\System\CurrentControlSet\Control\Print\Printers';
  EnablePrint := FALSE;
  if Registry.OpenKey(Key, FALSE) then
    EnablePrint := (Length(Registry.ReadString('Default')) = 0);
  Print1.Enabled := EnablePrint;
  Registry.Free;
.

regards
  ow
0
 

Author Comment

by:TCSCode
ID: 1342399
Hello Ow.
      For some reason I cannot get this code to work for 1 basic reason, I am not good with TRegistry, I tryied the code many different ways and all I come up with is Access violation Errors.
Could you make me a demo showing me where i should put this code.
                                                 Thanks TcsCode.
0
 
LVL 1

Expert Comment

by:ow
ID: 1342400
Hi TCSCode,

when you insert a menu in a form, the form will contain some fields of type "tMenuItem", one of them should be for printing, normaly "Print1 :tMenuItem".
Check for a default printer in tForm.Create:


unit Unit1;

INTERFACE

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Registry, Menus;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Print1: TMenuItem;
    PrinterSetup1: TMenuItem;
    Quit1: TMenuItem;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    end;

var
  Form1: TForm1;

IMPLEMENTATION

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
  var
    Registry :tRegistry;
    Key :string;
    EnablePrint :boolean;
  begin
  Registry := tRegistry.Create;
  Registry.RootKey := HKEY_CURRENT_CONFIG;
  Key := '\System\CurrentControlSet\Control\Print\Printers';
  EnablePrint := FALSE;
  if Registry.OpenKey(Key, FALSE) then
    begin
    TRY
      EnablePrint := (Length(Registry.ReadString('Default')) > 0);
    EXCEPT
      EnablePrint := FALSE;
      end;
    end;
  Print1.Enabled := EnablePrint;
  end;

initialization
end.


regards
  ow
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:TCSCode
ID: 1342401
I just have one last question. What is the initialization for in unit1? and Thanxs Ow..
0
 
LVL 1

Expert Comment

by:ow
ID: 1342402
Hi TCSCode,

well, in fact I write "initialization" in a unit even if there is no init code, cause I like it complete and I don't like single ends. But it's not absolutly necessary.

regards
  ow

0
 

Author Comment

by:TCSCode
ID: 1342403
Hi Ow.
 
Complete end, What good does adding "initialization"??? Thanks TCSCode.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question