Home   .......

Using Windows API in VB Tutorial

Some Windows Specifics

Products  
eXamples  
Files  
e-Mail Us  
 

 

This topic is intended to give you a clue about some Windows specifics that are not the same under VB.

Windows identifies every form, control, menu, menu item or whatever you can think of by its handle. When your application is run, every control on it is assigned a handle which is used later to separate the button from the rest of the controls. If you want to perform any operation on the button through an API you must use this handle. Where to get it from? Well VB has provided a Hwnd property for all controls that have handles in Windows.

Windows works with pixels, not twips. So, it is a good idea to have the controls you'll use API functions over set their ScaleMode properties to Pixel(3) so that you can use ScaleXXX properties to get their metrics. But even though, you have this opportunity, you may still need to convert twips to pixels and vice versa. You do it using TwipsPerPixelX and TwipsPerPixelY or the global Screen object. Here it is:

pixXValue = twipXValue \ Screen.TwipsPerPixelX
pixYValue = twipYValue \ Screen.TwipsPerPixelY

twipXValue = pixXValue * Screen.TwipsPerPixelX
twipYValue = pixYValue * Screen.TwipsPerPixelY

I haven't really seen the TwipsPerPixelX and TwipsPerPixelY value to be different, but its always better to make difference, at least for the good programing style. Also note that \ (for integer division) is used instead of / as pixels must always be whole numbers.

Another thing to mention is that Windows uses different coordinate systems for the functions. So, be careful.

And lastly, don't forget that VB is safe till the moment you begin to use APIs. A single syntax error in an API call may cause VB to crash (save often!). Also VB cannot debug APIs and if your program is crashing or behaving awkwardly, firstly check the API calls - for missed ByVal, for mistaken type or parameter, everything).

 
Copyright (c) 1998, Billy&George Software and Peter Dimitrov
Revised March 2000