Archive

Posts Tagged ‘better’

Delphi Programming Tips

December 15th, 1999 m3Rlin No comments

Here are some useful tips to make your programs run faster, make the executables smaller and to ease the programming part :-)

  1. When your are going to use only one or two functions or procedures from a unit and you have the unit’s source code don’t include the whole unit in the uses section but insert only the functions you are going to use into your program. This will make redistributing the sources much easier and there will be less problems with unfound units. Even if you are not planning to publish the source code of your program but you may want to send them to somebody who is going to help you or you just want to continue development on a different machine.
  2. Use Windows API functions instead of writing your own functions to do the same task. They are usually better and you will save size too.
  3. When including images in your programs you may want to optimize them first. When including BMP files be sure that the file has the exact size you need else crop it and make sure that you don’t have a 256 color (8 bit) image saved in 16,8 m (24 bit) color mode. When including GIF images you may want to crop them if they are too big and optimize their color pallete with special tools (for example: Paint Shop Pro). When including JPEG images you may want to crop them if they are bigger than you need and use image compression (not too much, though :-) If your are going to include a JPEG image that contains 256 or less colors you should convert it to a GIF image. They are much smaller.
  4. If you have a time-critical function (important loops) you should be very careful. What seems fast on your computer may not be as fast on other PC’s. Here you should create as small code as possible and make it fast. You may want to use assembler x86 for even further optimization. Please be careful with extreme assembler use since it is very hard to maintain and the Delphi compiler does a great job in producing efficient code.
  5. Delphi 3+ – you may want to compile your programs with run-time packages. This can save a lot on size when you have a lot of programs that use the same libraries.
  6. Instead of using non-visible components it’s usually better (not always though) to include the code in your program. There’s no need in using a component that registers file types or check the Windows version when it’s really just a few lines of code.
  7. When using assembler you should comment it. It’s very useful. A month later you may have no idea what the code means. I have heard though, that what was hard to write should be hard to read ;-)
  8. Keep your source codes neat and clean. This will make it easier to read and understand the code. Use variable and constant identifiers like iI, iTemp for Integer variables, btI for Byte variables, sProgName for string variables… Constants should start like variables but should be uppercase, for example: sCAPTION, iINTEGER or wMYAGE. Do the same with the VCL: btnOK – an “OK” Button, pnlForm – TPanel on whole Form or edName – TEdit containg somebody’s name. Don’t ever use the standard naming: Edit1, Edit2, Edit3… The more components you have the more time it’ll take to find the one you want.
  9. Don’t create all the forms on program startup (they are created by default) because this will make your program load slower and require a lot more memory. When you create a form dynamically the user usually won’t notice any slow down but when you create all your forms on startup the form creation times add up… Instead you should use dynamically created forms.

Delphi Component Messages

December 15th, 1999 m3Rlin 1 comment

Delphi sends messages for notification of events that are only applicable to Delphi components. They are like Windows messages in use. If you have a component that publishes the Font property the component probably needs to be repainted if any subproperties in the Font are changed. Changing the Font property does not necessarily generate a Windows event but the control still needs to know about the change. Component messages serve this purpose.

This is a complete listing of the component messages and what they do. They are all defined in the controls.pas unit. This is a list of Delphi 5 component messages. The messages marked Notification Only do not pass any useful information to the message handler and do not expect the message handler to return a value. The messages on which I could not find information about I marked with a question mark “?”.

If you have any information about component messages and they are not listed here let me know, so as I can update this table. m3rlin@delphifaq.net

CM_ACTIONEXECUTE ?
Unknown
CM_ACTIONUPDATE ?
Unknown
CM_ACTIVATE Notification Only
Sent to a form when it becomes the active form.
CM_ALLCHILDRENFLIPPED ?
Sent to a component when the Flip Children|All command in the IDE is executed.
CM_APPKEYDOWN ?
Unknown.
CM_APPSYSCOMMAND ?
Unknown.
CM_BIDIMODECHANGED Notification Only
Sent to a component when it’s BiDiMode property changes.
CM_BORDERCHANGED Notification Only
Sent to a component when it’s Border property changes.
CM_BUTTONPRESSED ?
Unknown.
CM_CHANGED ?
Parameters: TCMCHANGED Return Value: ?.
Unknown.
CM_CHILDKEY ?
Unknown.
CM_CONTROLCHANGE ?
Unknown.
CM_CONTROLLISTCHANGE ?
Parameters: TCMCONTROLCHANGE Return Value: ?.
Unknown.
CM_CTL3DCHANGED Notification Only
Sent to a component when it’s CTL3D property changes.
CM_CURSORCHANGED Notification Only
Sent to a component when it’s Cursor property changes.
CM_DEACTIVATE Notification Only
Sent to a form when it becomes inactive.
CM_DESIGNHITTEST
Parameters: TCMDESIGNHITTEST Return Value: either 0 or 1.
This message is sent in design mode when the mouse
is over the control. It appears that the purpose of the message is to determine
if the control wants to process mouse messages while in design mode. If the return
value is 1 then Delphi lets the control process mouse messages. If it is 0 then
the Delphi handles the messages. If a control sets this message to 1 all the
time then the popup menu will never appear. If the control does not
handle this message or returns 0 all the time then the control cannot response
to mouse messages in design mode.
CM_DIALOGHANDLE ?
Unknown.
CM_DIALOGCHAR ?
Unknown.
CM_DIALOGKEY ?
Unknown.
CM_DOCKCLIENT ?
Parameters: TCMDOCKCLIENT Return Value: ?.
Unknown.
CM_DOCKNOTIFICATION ?
Parameters: TCMDOCKNOTIFICATION Return Value: ?.
Unknown.
CM_DOCWINDOWACTIVATE ?
Unknown.
CM_DRAG ?
Parameters: TCMDRAG Return Value: ?.
Sent to a component when it’s being dragged.
CM_ENABLEDCHANGED NO
Sent to a component when it recieves a WM_ENABLE message.
CM_ENTER ?
Unknown.
CM_EXIT ?
Unknown.
CM_FLOAT ?
Parameters: TCMFLOAT Return Value: ?.
Unknown.
CM_FOCUSCHANGED ?
Parameters: TCMFOCUSCHANGED Return Value: ?.
Unknown.
CM_FONTCHANGED Notification Only
Sent to a component when the it’s Font property changes.
CM_FONTCHANGE Notification Only
Sent to a component when it receives a WM_FONTCHANGE message.
CM_GETDATALINK ?
Unknown.
CM_GOTFOCUS Notification Only
Sent to a component when it receives a WM_SETFOCUS message.
CM_HINTSHOW Notification Only
Sent to a component when it’s hint is shown.
CM_HINTSHOWPAUSE ?
Unknown.
CM_ICONCHANGED ?
Unknown.
CM_INVALIDATE Notification Only
Sent to a control when it needs to be repainted.
CM_INVOKEHELP ?
Unknown.
CM_ISTOOLCONTROL ?
Unknown.
CM_LOSTFOCUS Notification Only
Sent to a component when it receives a WM_KILLFOCUS message.
CM_MENUCHANGED ?
Unknown.
CM_MOUSEENTER Notification Only
Sent to a component when the mouse enters the component’s area and it recieves a WM_MOUSEHOVER message.
CM_MOUSELEAVE Notification Only
Sent to a component when the mouse leaves the component’s area and it recieves a WM_MOUSELEAVE message.
CM_MOUSEWHEEL Notification Only
Parameters: TCMMOUSEWHEEL Return Value: ?.
Sent to a component when it recieves a WM_MOUSEWHEEL message.
CM_PARENTBIDIMODECHANGED Notification Only
Sent to all child components when a parent’s (not Owner’s) BiDiMode property changes. This message is also send when the control is gets a new parent.
CM_PARENTCTL3DCHANGED Notification Only
Sent to all child components when a parent (not Owner) receives a CM_CTL3DCHANGED message. This message is also send when the control is gets a new parent.
CM_PARENTCOLORCHANGED Notification Only
Sent to a component when it’s ParentColor property changes. This message is also sent when the component is read from a stream or gets a new parent.
CM_PARENTFONTCHANGED Notification Only
Sent to all child components when a parent (not Owner) receives a CM_FONTCHANGED message. This message is also send when the control is read from a stream or gets a new parent.
CM_PARENTSHOWHINTCHANGED Notification Only
Sent to a component when it’s ParentShowHint property changes. This message is also sent when the componentis read from a stream or gets a new parent.
CM_RECREATEWND ?
Unknown.
CM_RELEASE ?
Unknown.
CM_SHOWHINTCHANGED Notification Only
Sent to a component when it’s ShowHint property changes.
CM_SHOWINGCHANGED ?
Unknown.
CM_SYSCOLORCHANGE Notification Only
Sent to a component when it recieves a WM_SYSCOLORCHANGE message.
CM_SYSFONTCHANGE Notification Only
Sent to a component when it recieves a WM_SYSFONTCHANGE message.
CM_TABSTOPCHANGED ?
Sent to a comonentn when it’s TabStop property changes.
CM_TEXTCHANGED Notification Only
Unknown.
CM_TIMECHANGE Notification Only
Sent to a component when it receives a WM_TIMECHANGE message.
CM_UIACTIVATE ?
Unknown.
CM_UIDEACTIVATE ?
Unknown.
CM_UNDOCKCLIENT ?
Parameters: TCMUNDOCKCLIENT Return value: ?.
Unknown.
CM_CM_VISIBLECHANGED Notification Only
Sent to a control when it’s Visible property changes.
CM_WANTSPECIALKEY Result value: Cardinal
Sent to a component when a special keys is being handled. These
special keys are VK_TAB, VK_LEFT, VK_RIGHT, VK_UP,
VK_DOWN, VK_RETURN, VK_EXECUTE, VK_ESCAPE and
VK_CANCEL. To receive keystroke messages for these keys you have to handle
the CM_WANTSPECIALKEY messages. The CM_WANTSPECIALKEY allows a much
better way of deciding if you require a special key than responding to the
WM_GETDLGCODE messages.
If the message result is non zero then the key is passed onto the
KeyPress method for you to handle, otherwise it is passed onto the controls
parent form for Delphi’s standard navigation handling. The Delphi standard
navigation is where Delphi handles the Tab, Shift-Tab and arrow key
motion between controls, without needing to use the default Windows dialog
manager at all.
CM_WINDOWHOOK ?
Unknown.
CM_WININICHANGE Notification Only
Parameters: WM_WININICHANGE Return Value: None.
Sent to a component when it receives a WM_SETTINGCHANGE or WM_WININICHANGE message.