Archive

Posts Tagged ‘coding’

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.

How to Hide TWebBrowser Scrollbars

December 15th, 1999 m3Rlin No comments

If you want to block the ability to use scrollbars in TWebBrowser then you can simply hide them. Here’s the code that’ll do that.

...
with WebBrowser1.OleObject.Document.Body.Style do
  OverflowX := 'hidden';
  OverflowY := 'hidden';
end;
...