terça-feira, 24 de novembro de 2009

Dicas e truques com o Delphi

Pessoal, olhando algumas coisas antigas achei algumas dicas interessantes para se brincar com o Delphi. Acredito que nem tudo funciona no Windows XP/Vista, mas vale a pena conferir.

Lendo o volume do HD

Function ExtractDiskSerial(Drive:String):String;

Var

Serial:DWord;

DirLen,Flags: DWord;

DLabel : Array[0..11] of Char;

begin

GetVolumeInformation(PChar(Drive+':\'),dLabel,12,@Serial,DirLen,Flags,nil,0);

Result := IntToHex(Serial,8);

end;


Descobrindo o número serial do HD

procedure TForm1.Button1Click(Sender: TObject);

var

SerialNum : pdword;

a, b : dword;

Buffer : array [0..255] of char;

begin

if GetVolumeInformation('c:\', Buffer, SizeOf(Buffer), SerialNum, a, b, nil, 0) then

Label1.Caption := IntToStr(SerialNum^);

end;

Abrir e fechar a bandeja do CD-ROM

{Para Abrir:}

mciSendString('Set cdaudio door open wait', nil, 0, handle);

{Para Fechar:}

mciSendString('Set cdaudio door closed wait', nil, 0, handle);


Como saber se o CD está no drive

Function MidiaPresente(MediaPlayer: TMediaPlayer): Boolean;
var
Params: MCI_STATUS_PARMS;
S: array [0.255] of char;
r: Integer;
begin
//verifica se existe um cd inserido
Params.dwItem:= MCI_STATUS_MEDIA_PRESENT;
r:= MCISendCommand(MediaPlayer.DeviceID, MCI_STATUS, MCI_STATUS_ITEM, Integer(Addr(Params)));
if r <> 0 then
begin
MCIGetErrorString(r, S, SizeOf(S));
ShowMessage('Erro: ' + StrPas(S));
end
else
Result:= Params.dwReturn = 1;
end;


Verificar se tem disquete no drive

function TForm1.TemDiscoNoDrive(const drive : char): boolean;

var

DriveNumero : byte;

EMode : word;

begin

result := false;

DriveNumero := ord(Drive);

if DriveNumero >= ord('a') then

dec(DriveNumero,$20);

EMode := SetErrorMode(SEM_FAILCRITICALERRORS);

try

if DiskSize(DriveNumero-$40) = -1 then

Result := true

else

messagebeep(0);

finally

SetErrorMode(EMode);

end;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

if TemDiscoNoDrive('a') then

ShowMessage('Tem disco No drive A:')

else

ShowMessage('Não tem Disco no Drive A:');

end;


Ligar/Desligar o monitor

Desligar

SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 0);

Ligar

SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1);

Nenhum comentário:

Postar um comentário

Obrigado pela participação. Continue visitando Dúvidas de Programação - Programming Doubts

Links

Related Posts with Thumbnails