Tudo sobre programação, banco de dados, internet, tecnologias, engenharia de software, dicas, tutoriais, dúvidas, apostilas e muito mais...
segunda-feira, 14 de dezembro de 2009
Comandos Delphi - Parte 2
Comandos Delphi - Parte 1
terça-feira, 24 de novembro de 2009
Ler e escrever arquivos texto no Delphi
//Rotina para escrever em um arquivo texto:
Var
F:TextFile;
Begin
AssignFile(f,'c:\arquivo_qualquer.txt');
Rewrite(f); //abre o arquivo para escrita
Writeln(f,'Dúvidas de Programação'); escreve no arquivo e desce uma linha
Write(f,'duvidasdeprogramacao.blogspot.com'); //escreve no arquivo sem descer a linha
Closefile(f); //fecha o handle de arquivo
End;
//Rotina para ler de um arquivo texto:
var
f:TextFile;
linha:String;
begin
AssignFile(f,'c:\arquivo_qualquer.txt');
Reset(f); //abre o arquivo para leitura;
While not eof(f) do
begin
Readln(f,linha); //lê do arquivo e desce uma linha. O conteúdo lido é transferido para a variável linha
Memo1.lines.add(linha); //adiciona a um campo Memo
End;
Closefile(f);
end;
Dicas e truques com o Delphi
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
Ligar
SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1);