![]() |
In this page you can find several tips & tricks made by myself. Tips are divided by Begginer Tips or Advanced Tips |
Several of these questions were made from people who coded with Visual Basic.
1. What is the easyst way to Grab Windows images?
procedure TScrnFrm.GrabScreen;
var
DeskTopDC: HDc;
DeskTopCanvas: TCanvas;
DeskTopRect: TRect;
begin
DeskTopDC:= GetWindowDC(GetDeskTopWindow);
DeskTopCanvas:= TCanvas.Create;
DeskTopCanvas.Handle:= DeskTopDC;
DeskTopRect:= Rect(0,0,Screen.Width,Screen.Height);
ScrnForm.Canvas.CopyRect(DeskTopRect,DeskTopCanvas,DeskTopRect);
ReleaseDC(GetDeskTopWindow,DeskTopDC);
end;
2. How can i Stop Delphi execution (like Stop in
V.B.)?
There isn't a way in Delphi to stop the program execution. However in Delphi 3.0 you can
use the debug assertion proc to do this. For example if Delphi find Assert(False,
'Program Stopped');
the compiler stop project execution and you can use Step Over
or Trace Into to debug your proc. If you have a copy of Delphi 2.0 you can make a VBStop
procedure making an error in a try .. except section.
3. How do i drag and move components in runtime?
This will work for a TPanel.
procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton; Shift:
TShiftState; X, Y: Integer);
const
SC_DragMove = $F012;
begin
ReleaseCapture;
(sender as TWinControl).perform(WM_SysCommand, SC_DragMove, 0);
end;
4. How can i trap for my own hotkeys?
First: set the form's KeyPreview := true;
Then, you do something like this:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (ssCtrl in Shift) and (chr(Key) in ['A', 'a']) then
ShowMessage('Ctrl-A');
end;
5. Windows, web pages, multimedia
programs, etc. have backgrounds...
Of course you could place an Image component on your form and set it's
Alignment to Client to place a background on your form. But, here's another way to do it:
(1) Add following to your form's Public declarations section:
bmpBackground : TBitmap;
(2) Double click on your form and add bitmap initialization code to the
FormCreate procedure:
bmpBackground := TBitmap.Create;
bmpBackground.LoadFromFile('c:\windows\setup.bmp');
(3) Go to the form's events list and double click on OnPaint. Add following
line to the FormPaint procedure:
Canvas.Draw( 0, 0, bmpBackground );
(4) Finally insert the following code to FormDestroy
procedure (OnDestroy event):
bmpBackground.Free;
1. Where's a way to get joystick messages?
You can use JoySetCapture API and keep MM_JOY1MOVE or MM_JOY2MOVE, if you want a component that do it download this (it's in beta
testing however).
2. I've seen a draggable ListBox (Access
'95), ...
It is very simple to make draggable ListBox in Delphi, just overrite
ParamCreate method (download this control)
3. What O.S. is running?
The Win32Platform
variable (that is defined in SysUtils.pas)
return the O.S. that is running your Delphi project. The result can be:
Val | Means | Result |
VER_PLATFORM_WIN32S |
Win32s | 0 |
VER_PLATFORM_WIN32S_WINDOWS |
Windows 95 | 1 |
VER_PLATFORM_WIN32_NT |
Windows NT | 2 |
4. How programs right align a menu item?
Not so difficult: try this proc
procedure TForm1.FormCreate(Sender: TObject);
var
MInfo: TMenuItemInfo;
Buffer: array [0..79] of char;
begin
MInfo.cbSize := SizeOf(TMenuItemInfo);
MInfo.fMask := MIIM_TYPE;
MInfo.dwTypeData := Buffer;
MInfo.cch := SizeOf(Buffer);
// Ottiene le informazioni del menu
if GetMenuItemInfo(MainMenu.Handle, 2, True, MInfo) then
begin
MInfo.fMask := MIIM_TYPE;
MInfo.fType := MInfo.fType or MFT_RIGHTJUSTIFY;
// Giustifica il menu a destra
SetMenuItemInfo(MainMenu.Handle, 2, True, MInfo);
end;
end;
5. Standard Windows are so boring...
Are you looking for a more interesting window? The follow code make a
Rounded Form, try to execute this app.
procedure TForm1.FormCreate(Sender: TObject);
var
R: HRgn;
begin
R:= CreateEllipticRgn(1,1,Width, Height);
SetWindowRgn(Handle, R, True);
end;
Very nice!
6. Here there is a
component that install an icon in SysTray Notification Area!
7. How to make a Kiosk application?
You can easily make a kiosk application in Win '95 adding this code to
your OnCreate event.
var B: LongBool;
begin
SystemParametersInfo (SPI_SCREENSAVERRUNNING, Word(True), @B, 0);
BorderStyle := bsNone;
BorderIcons := [];
FormStyle := fsStayOnTop;
Height := Screen.Height;
Width := Screen.Width;
Top := 0;
Left := 0;
Show;
ShowWindow(Application.handle, SW_HIDE);
end;
In FormDestory simply add this code:
var B: LongBool;
begin
SystemParametersInfo (SPI_SCREENSAVERRUNNING, Word(False), @B, 0);
end;
Remember to add a button with Close method...
It's a funny application no?
8. Why Delphi executable are so big?
Delphi is a RAD envirenment, so a lot of information are stored in
executable, also if their aren't needed...
But there is a way to make small executable (less than 50Kb)? Yes, and No, you can make an
application using only API call, and so the size will be decresed to only 17Kb, if you add
CommCtrl unit the size increse to 34Kb, but when you use Forms Unit your executable will
be About 200Kb! :-(