Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Saber si hay un disco dentro del lector de CD/DVD (https://www.clubdelphi.com/foros/showthread.php?t=92534)

REHome 22-11-2017 01:10:52

Saber si hay un disco dentro del lector de CD/DVD
 
Hola:

¿Se puede saber pulsando un botón con Delphi si hay un disco dentro o no?

Aquí un ejemplo de abrir y cerrar la bandeja del lector.


Código:

unit Bandeja_Delphi;

interface

// Añadir MMSystem en el uses.
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, MMSystem;

type
  TForm1 = class(TForm)
    GroupBox_Bandeja: TGroupBox;
    Button_Abrir: TButton;
    Button_Cerrar: TButton;
    Label_Mensaje: TLabel;
    procedure Button_AbrirClick(Sender: TObject);
    procedure Button_CerrarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button_AbrirClick(Sender: TObject);
begin
    Label_Mensaje.Caption := 'Abriendo...';
    Application.ProcessMessages;
    mciSendString('Set cdaudio door open wait', nil, 0, 0);
    Label_Mensaje.Caption := 'Abierto.';
end;

procedure TForm1.Button_CerrarClick(Sender: TObject);
begin
    Label_Mensaje.Caption := 'Cerrando...';
    Application.ProcessMessages;
    mciSendString('Set cdaudio door closed wait', nil, 0, 0);
    Label_Mensaje.Caption := 'Cerrado.';
end;

end.

No se si existe esa posibilidad.

Saludos.

ecfisa 22-11-2017 03:30:10

Cita:

Empezado por REHome (Mensaje 522719)
Hola:

¿Se puede saber pulsando un botón con Delphi si hay un disco dentro o no?

Hola.

...check if a disk is in the drive?

Saludos :)

REHome 22-11-2017 11:30:07

Lo he intentado ahcer pero nome sale.
Código:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
function DiskInDrive(Drive: Char): Boolean;
  // Disk can be a floppy, CD-ROM,...

var
  Form1: TForm1;
  ErrorMode: Word;

  begin
  { make it upper case }
  if Drive in ['a'..'z'] then Dec(Drive, $20);
  { make sure it's a letter }
  if not (Drive in ['A'..'Z']) then
    raise EConvertError.Create('Not a valid drive ID');
  { turn off critical errors }
  ErrorMode := SetErrorMode(SEM_FailCriticalErrors);
  try
    { drive 1 = a, 2 = b, 3 = c, etc. }
    if DiskSize(Ord(Drive) - $40) = -1 then
      Result := False
    else
      Result := True;
  finally
    { Restore old error mode }
    SetErrorMode(ErrorMode);
  end;
end;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
if DiskInDrive('a') = False then
    ShowMessage('Drive not ready');
end;

end.


Casimiro Notevi 22-11-2017 12:26:33

Cita:

Empezado por REHome (Mensaje 522732)
Lo he intentado ahcer pero nome sale.

"no me sale", ¿qué error es? ;)

REHome 22-11-2017 13:35:47


Casimiro Notevi 22-11-2017 14:09:10

¿Lo has copiado bien?, porque yo he hecho un simple "copia->pega" y funciona perfectamente.
Código Delphi [-]
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    bt1: TButton;
    procedure bt1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function DiskInDrive(Drive: Char): Boolean;
  // Disk can be a floppy, CD-ROM,...
var
  ErrorMode: Word;
begin
  { make it upper case }
  if Drive in ['a'..'z'] then Dec(Drive, $20);
  { make sure it's a letter }
  if not (Drive in ['A'..'Z']) then
    raise EConvertError.Create('Not a valid drive ID');
  { turn off critical errors }
  ErrorMode := SetErrorMode(SEM_FailCriticalErrors);
  try
    { drive 1 = a, 2 = b, 3 = c, etc. }
    if DiskSize(Ord(Drive) - $40) = -1 then
      Result := False
    else
      Result := True;
  finally
    { Restore old error mode }
    SetErrorMode(ErrorMode);
  end;
end;

procedure TForm1.bt1Click(Sender: TObject);
begin
  if DiskInDrive('a') = False then
    ShowMessage('Drive not ready');
end;

end.

ElDioni 22-11-2017 14:13:21

Creo que no has declarado la funcion en la cabecera, tomo un ejemplo del amigo Dec para ilustrar lo que te digo.

Código Delphi [-]
unit Global;  

interface  

function SayHello() : boolean;   

Var   
Vusuario : string;   
vCia     : string;   
MonedaBase : String;   
Detener   : boolean;  

implementation  

uses   

Dialogs;  

function SayHello() : boolean; 
begin   
Dialogs.ShowMessage( 'Hello!' );   
result := true; 
end;  

end.

Saludos.

ElDioni 22-11-2017 14:15:44

Viendo lo que ha puesto Casimiro, no hagas ni caso a lo que te he dicho yo.

Saludos.

ElDioni 22-11-2017 14:17:07

Por lo menos no del todo,

volviendo a ver tu código y el que ha puesto casimiro, creo que en el tuyo falla algo en el orden de las cosas.

Saludos.

REHome 23-11-2017 01:23:39

Ahora me ejecuta el programa, pero no veo que haya cambios si hay discos o no.

Código:

unit Detectar_disco;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function DiskInDrive(Drive: Char): Boolean;
  // Disk can be a floppy, CD-ROM,...
var
  ErrorMode: Word;
begin
  { make it upper case }
  if Drive in ['a'..'z'] then Dec(Drive, $20);
  { make sure it's a letter }
  if not (Drive in ['A'..'Z']) then
    raise EConvertError.Create('Not a valid drive ID');
  { turn off critical errors }
  ErrorMode := SetErrorMode(SEM_FailCriticalErrors);
  try
    { drive 1 = a, 2 = b, 3 = c, etc. }
    if DiskSize(Ord(Drive) - $40) = -1 then
      Result := False
    else
      Result := True;
  finally
    { Restore old error mode }
    SetErrorMode(ErrorMode);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if DiskInDrive('a') = False then
    ShowMessage('Drive not ready');
end;

end.

Tenga disco o no, siempre me aprece este mensaje.

Drive not ready

Saludos.

Casimiro Notevi 23-11-2017 09:53:15

Es obvio, pero se supone que modificarás el código con la letra de la unidad del cd/dvd que quieres comprobar.

Código Delphi [-]
if DiskInDrive('a') = False then
  ShowMessage('Drive not ready');

if DiskInDrive('d') = False then
  ShowMessage('Drive not ready');

if DiskInDrive('f') = False then
  ShowMessage('Drive not ready');

if DiskInDrive('g') = False then
  ShowMessage('Drive not ready');


La franja horaria es GMT +2. Ahora son las 03:10:14.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi