Vous trouverez dans cette rubrique, les trucs et astuces les plus
fréquemment utilisés lorsque l'on est
débutant en programmation .
Cette page vous est ouverte et je suis prêt à recevoir vos trucs (pour cela, il vous suffit de m'envoyer un e-mail tomarobe@yahoo.fr avec votre astuces (au besoin, vous pouvez me joindre un fichier.zip pour exemple)
Si vous voulez découvrird'autres astuces, allez "fouiner" sur les sites (voir dans ma rubrique Liens )
procedure
TForm1.Button1Click(Sender:TObject);
var
buffer
: array [0..255] of Char;
begin
GetTempPath(255,buffer);
label1.caption:='Repertoire
temporaire:' + buffer;
GetCurrentDirectory(255,buffer);
label2.caption:='Repertoire
courant :' + buffer;
end;
Arret
de Windows
ExitWindowsEx(EWX_SHUTDOWN,0);
Redémarragede
Windows
ExitWindowsEx(EWX_REBOOT,0);
ExitWindowsEx(EWX_FORCE,0);
Function
begin
Result:=FormatDateTime('ddd, dd mmm yyyy', GetDate(T));
end;
procedure
TForm1.Button1Click(Sender:TObject);
begin
label1.caption:=DateToStr(Date);
procedure
TForm1.Button2Click(Sender:TObject);
begin
label2.caption:=TimeToStr(Date);
6Afficher ou Masquer la barre des taches de Windows
Function
Var BarreDeTaches : HWnD;
begin
BarreDeTaches:=FindWindow('Shell_TrayWnd', NIL);
if BarreDeTaches<>Null then
if Affiche then
ShowWindow(BarreDeTaches, SW_SHOW) //
Afficher
Else
ShowWindow(BarreDeTaches, SW_HIDE); //
Masquer
Result:=BarreDeTaches<>Null;
end;
Il faut utiliser la fonction ChangeFileExt(const FileName, Extension:string): string;
Exemple
:
var
begin
FileName
:= 'c:\fichierx.txt';
NewName
:=ChangeFileExt(filename, '.htm');
MoveFile(PChar(FileName),PChar(NewName));
end;
var
dir
: String;
begin
dir:=ExtractFileDir(Application.ExeName);
end;
pour ouvrir le lecteur cd rom :
mciSendString('Set
cdaudiodoor open wait', nil, 0, handle);
pour fermer le lecteur cd rom :
mciSendString('Set
cdaudiodoor closed wait', nil, 0, handle);
P.S :Ne pas oublier d'ajouter l'unité "mmsystem" dans laclause "Uses"
Appeler la procédure windows suivante :
PostMessage(GetDesktopWindow,WM_SYSCOMMAND,
SC_SCREENSAVE, 0);
procedure
creerbouton;
Var
MonBouton
: TButton;
Begin
MonBouton
:=TButton.Create(Form1);
with
MonBouton do begin
Parent
:= MyForm;
height
:= 20;
width := 2000;
caption := 'nouveau bouton';
left
:= 20;
top
:= 20;
end;
end;
function
litnumser:string;
var NumSerie :
DWORD;
MaxComp,
SySflags : integer;
begin
GetVolumeInformation('A:',nil, 0,@NumSerie,MaxComp, SysFlags,nil, 0);
result:=NumSerie;
end;
Sleep (durée); // Attentionla durée doit etre exprimée en millisecondes
// C'est à dire que (5000)équivaut à 5 secondes temps
procedure
TForm1.Edit1KeyPress(Sender: TObject;var Key: Char);
begin
if Key =
Chr(VK_RETURN) then key:= #0;
end;
rajouter Shellapi dans laclause USES
WinExec('rundll32.exeshell32.dll,Control_RunDLL
timedate.cpl',SW_SHOWNORMAL);
Ajouter MMSystem dans laclause Uses
tapez le chemin d'accéscomplet de votre fichier Wave (ex : C:\Windows\Media\Music.Wav)
cela donnera dans laprocédure
begin
sndPlaySound('C:\Windows\Media\Music.Wav',SND_ASYNC);
end;
Procedure
TForm1.CacherBtnDemarrerClick(Sender:TObject);
// pour cacher
var
Rgn :
hRgn;
Begin
Rgn :=
CreateRectRgn(0, 0, 0, 0);
SetWindowRgn(FindWindowEx(FindWindow('Shell_TrayWnd',nil),
0, 'Button', nil), Rgn, true);
End;
Procedure
TForm1.AfficherBtnDemarrerClick(Sender:TObject);
// pour afficher
Begin
SetWindowRgn(FindWindowEx(FindWindow('Shell_TrayWnd',nil),
0, 'Button', nil), 0, true); End;
Procedure
TForm1.FormCreate(Sender:
TObject);
begin
Form1.Brush.Style
:= bsClear;
Form1.BorderStyle
:= bsNone;
end;
begin
Form1.ScaleBy(60,
100);
//rétrecie la fiche Form1 esttous ses controles à 60% de leurs dimensions.
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
deletefile('A:\fichier.txt'); // vous pouvez effacez n'importe quel fichier, le tout est de bien préciser l'extension end;
Procedure
Form1.FormPaint(Sender:
TObject);
Var Ligne : integer;
Begin
For
Ligne:=0 to Form1.Height do
Begin
Form1.Canvas.pen.color:=RGB(0,MulDiv(Ligne,255,Form1.height),0);
//vert Form1.Canvas.MoveTo(0, Ligne);
Form1.Canvas.LineTo(Form1.Width,
Ligne) ;
end;
end;
// (
rouge : RGB(MulDiv(Ligne,255,Form1.height),0,0)
// ( bleu : RGB(0,0,MulDiv(Ligne,255,Form1.height)) )
Utiliser cette procédure
SetCursorPos(CoordonnéeX,CoordonnéeY);
Si vous placez un composantPrint dans une fiche, il est possible d'imprimer le contenu de la fiche dela maniére suivante :
procedure
TForm1.PrintClick
(Sender: Tobject);
begin
Print;
end;
Utilisez le caracrère #13 (retourchariot) pour séparer 2 lignes :
Label1.Caption:='premièreligne'+#13+'deuxième ligne';
Procedure
TForm1.Button1Click(Sender:
TObject);
var
Monep :
string;
i :
integer;
begin
Monrep:=extractfilepath(application.exename);
For i :=
Length(Monrep) downTo 1 Do
If
Monrep [i] = ' \ ' then
begin
Showmessage
(Copy (Monrep,1 , Pred (i) ) );
exit;
end;
end;
Appeler la procédure windowssuivante ( temps en milliseconde ) :
SetCaretBlinkTime(250);
procedure TForm1.SetCDAutoRun(AAutoRun:Boolean);
const
DoAutoRun : array[Boolean]of Integer = (0,1);
var
Reg:TRegistry;
begin
try Reg :=TRegistry.Create;
Reg.RootKey :=HKEY_LOCAL_MACHINE;
ifReg.KeyExists('System\CurrentControlSet\Services\Class\CDROM') then
begin
ifReg.OpenKey('System\CurrentControlSet\Services\Class\CDROM',FALSE) then
Reg.WriteBinaryData('AutoRun',DoAutoRun[AAutoRun],1);
finally
Reg.Free;
end;
ShowMessage('Your settingswill take effect on the next reboot of Windows.');
end;
procedure TForm1.Button1Click(Sender:TObject);
begin
ifFindWindow('TAppBuilder', nil)<=0 then
begin
ShowMessage('Delphi n''estpas ouvert');
end
else
begin
ShowMessage('Delphi isouvert');
end;
end;
29 Actualiser le bureau Windows
procedure TForm1.Button1Click(Sender:TObject);
begin
SendMessage(FindWindow('Progman','Program Manager'),WM_COMMAND,$A065,0);
end;
function GetDesktopListViewHandle: THandle;
var
S: String;
begin
Result :=FindWindow('ProgMan', nil);
Result :=GetWindow(Result, GW_CHILD);
Result :=GetWindow(Result, GW_CHILD);
SetLength(S, 40);
GetClassName(Result,PChar(S), 39);
if PChar(S) <>'SysListView32' then Result := 0;
end;
SendMessage(GetDesktopListViewHandle,LVM_ALIGN,LVA_ALIGNLEFT,0);
var
NomduBitmap : string ;
begin
NomduBitmap : = 'C:\windows\image1.bmp';
SystemParametersInfo(SPI_SETDESKWALLPAPER,0,PChar('NomduBitmap'),0);
end;
32 Comment savoir si la touche Caps Lock (Majuscule) est active
function SiCapsLockActive : boolean;
begin
Result : = 0<> (GetKeyState(VK_CAPITAL) and $01);
end;
33 Comment calculer une mensualité (Finance)
function CalcMensualite (C,T,R : Double) : Double;
// C : capital d'emprunt,
// T : taux d'emprunt,
// R : nombre de remboursements mensuels
begin
Resultat := (C*(T*Puiss(1+T,R) ) ) / (Puiss(1+T,R) - 1);
end;
34 Récupérer le N° de ligne et de colonne du curseur dans un TMemo
var
line, col : integer;
begin
line : = SendMessage(Memo1.Handle, EM_LineFromChar, Memo1.SelStart,0);
col : = Memo1.SelStart - SendMessage (Memo1.Handle, EM_LineIndex, line,0);
end;
if (key=#13)and((sender as TRichEdit).lines.Count>=10) then
Key :=#0;
// le chiffre 10 représente le nombre de lignes limite...
// vous pouvez remplacer cechiffre par un autre de votre choix
36 Lancer un fichier d'aide (*.hlp)
procedure TForm1.Button1Click(Sender:TObject);
begin
Application.HelpFile := 'Nomdufichier.Hlp';
Application.HelpCommand(HELP_CONTENTS,0);
end;
37Afficher l'icone de votre application
Placer un composant TImage sur votre form et mettez l'évènement ci dessous dans la propriété OnShow de la form
procedureTform1.FormShow(Sender: TObject);
begin
Image1.picture.assign(application.icon);
end;
38Mettre le moniteur en état de veille ou le réactiver
Mettre le moniteur en veille
SendMessage(Application.Handle, WM_SYSCOMMAND,SC_MONITORPOWER,0);
Réactiver le moniteur enveille
SendMessage(Application.Handle, WM_SYSCOMMAND,SC_MONITORPOWER,-1);