FIX: Overflow Error When CurrentX Or CurrentY Greater Than 32K

ID: Q100190

2.00 3.00 WINDOWS kbprg kbbuglist

The information in this article applies to:

- Microsoft Visual Basic programming system for Windows,

  versions 2.0 and 3.0

SYMPTOMS

An Overflow error results if you attempt to set CurrentX or CurrentY to a value greater than 32,767 while the current ScaleMode is set to Twips. When using another ScaleMode such as pixels, the same problem occurs if the conversion of the CurrentX or CurrentY value to twips is greater than 32,767.

However, when you use the Print method (or other graphics method) you can correctly cause the value of CurrentX or CurrentY to exceed 32,767 when the ScaleMode is set to twips.

CAUSE

When CurrentX or CurrentY is set explicitly, Visual Basic incorrectly converts the value using the current scale mode to twips. If the result of the conversion to twips is greater the 32,767, an Overflow error occurs. For example, if the ScaleMode is set to Pixels, CurrentX and CurrentY cannot exceed approximately 2731 pixels if the twips per pixel ratio is 12 because 12 times 2731 is 32,772 which is greater than 32767.

When setting CurrentX or CurrentY, Visual Basic should convert the value using the current ScaleMode to pixels rather than twips before comparing the result to 32,767. As a result of this bug, CurrentX and CurrentY are each restricted to a limit 12-14 times smaller (depending on TwipsPerPixelX or TwipsPerPixelY) than they should be.

WORKAROUND

To work around the problem, call the Windows API functions:

An example is shown in the MORE INFORMATION section below.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem has been fixed in Visual Basic version 4.0.

MORE INFORMATION

Because the ratio of twips per pixel varies from one device (or screen resolution) to another, you will need to calculate the limit for the device you are using. To calculate the exact pixel limit of CurrentX, divide 32768 by Screen.TwipsPerPixelX. To calculate the limit of CurrentY, divide 32768 by Screen.TwipsPerPixelY. To find the limit of CurrentX and CurrentY for your printer, use the Printer object in place of the Screen object in the calculations above.

Example for Using API Calls as Workaround

The following example shows how to use the three API calls TextOut, MoveTo, and LineTo to work around the problem. Note that when you call Windows API functions to print or draw, all X and Y coordinates are measured in pixels regardless of the current ScaleMode setting.

1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N)

   if Visual Basic is already running. Form1 is created by default.

2. Add the following declarations to the General section of Form1:

   ' Enter the following Declare statement on one, single line:
   Declare Function TextOut Lib "GDI" (ByVal hDC As Integer,
      ByVal X As Integer, ByVal Y As Integer, ByVal lpString As String,
      ByVal nCount As Integer) As Integer

   ' Enter the following Declare statement on one, single line:
   Declare Function MoveTo Lib "GDI" (ByVal hDC As Integer,
      ByVal X As Integer, ByVal Y As Integer) As Long

   ' Enter the following Declare statement on one, single line:
   Declare Function LineTo Lib "GDI" (ByVal hDC As Integer,
      ByVal X As Integer, ByVal Y As Integer) As Integer

3. Add the following code to the Form_Click event:

   Sub Form_Click ()
      X1% = 100
      Y1% = 100
      X2 %= 200
      Y2 %= 200

      retvaL& = TextOut(FORM1.hDC, 100, 100, "ONE LINE", 8)

      retvaL& = MoveTo(FORM1.hDC, X1%, Y1%)

      retvaL& = LineTo(FORM1.hDC, X2%, Y2%)

   End Sub

4. From the Run menu, choose start (ALT, R, S), or press the F5 key to run
   the program.

5. Click the form, and you will see the words "ONE LINE" on the form and a
   diagonal line from the upper left to the lower right. The line starts
   at the X1 and Y1 coordinates given in the MoveTo API call and ends at
   the X2 and  Y2 coordinates given in the LineTo API call. The words
   "ONE LINE" should appear 100 pixels from the top and 100 pixels from the
   left. Note that TextOut may be used without MoveTo because TextOut gives
   its own coordinates. However using LineTo without using MoveTo results
   in a line stating from the current output position.

KBCategory: kbprg kbbuglist KBSubcategory: PrgCtrlsStd Additional reference words: buglist2.00 buglist3.00 2.00 3.00 fixlist4.00
Keywords          : PrgCtrlsStd kbbuglist
Version           : 2.00 3.00
Platform          : WINDOWS
Solution Type     : kbfix

Last Reviewed: November 1, 1997