Using "Rectangle" from Win32 API

I'm having some problems with this API-function. It doesn't seem to care about Printer.ScaleMode. Here's an example:

Public Sub PrintRectangle()
    Dim b As Boolean
   
    Printer.ScaleMode = 6
    Printer.Print
    b = Rectangle(Printer.hdc, 80, 80, 160, 160)
    Printer.EndDoc
End Sub

From this, I get a small (about 1 x 1 cm) rectangle near the top left of the page, instead of a 8 x 8 cm rectangle 8 cm from the top and left edge. In the real code I want to use .CurrentX and .CurrentY as values, so I can't just adjust the numbers. What is wrong and how can it be solved?
liljegrenAsked:
Who is Participating?
 
JustinBConnect With a Mentor Commented:
MM_LOMETRIC Should be declared as follows
Public Const MM_LOMETRIC = 2

but can I ask a silly question? Why are you drawing a rectangle with an API call when you can call the printer.line function to do the same thing?

If you do not know how to use this function this is the syntax:

'set printer.DrawWidth, printer.DrawMode and 'printer.DrawStyle to values required

printer.Line [Step] (x1, y1) [Step] - (x2, y2), [color], [B][F]

The Line method syntax has the following object qualifier and parts:

Part Description
object Optional.Object expression that evaluates to an object in the Applies To list. If object is omitted, the Form with thefocus is assumed to be object.

Step Optional.Keyword specifying that the starting point coordinates are relative to the current graphics position given by the CurrentX and CurrentY properties.

(x1, y1) Optional. Single values indicating the coordinates of the starting point for the line or rectangle.  The ScaleMode property determines the unit of measure used.  If omitted, the line begins at the position indicated by CurrentX and CurrentY.

Step Optional. Keyword specifying that the end point coordinates are relative to the line starting point.
(x2, y2) Required. Single values indicating the coordinates of the end point for the line being drawn.

color Optional. Long integer value indicating the RGB color used to draw the line. If omitted, the ForeColor property setting is used. You can use the RGB function or QBColor function to specify the color.

B Optional. If included, causes a box to be drawn using the coordinates to specify opposite corners of the box.
F Optional. If the B option is used, the F option specifies that the box is filled with the same color used to draw the box. You cannot use F without B. If B is used without F, the box is filled with the current FillColor and FillStyle. The default value for FillStyle is transparent.




0
 
setiawanCommented:
Rectange(Printer.hdc, 80, 80, 160, 160) return true / false ?
what is the metrics of x1,y1,x2,y2 ? (8 cm = 80?)
What happened if You enlarge to 80,80, 240, 240 ?

0
 
liljegrenAuthor Commented:
1. True.
2. Yes, 8 cm = 80 mm (about 3 inches).
3. The rectangle gets bigger (about 13,5 mm).
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JustinBCommented:
Try this
 
' This will set the mode of the DC to 0.1mm units
SetMapMode(printer.hdc, MM_LOMETRIC) ' This is an API call

' Call your rectangle function but with the units * 10
'(for mm)
Rectange(Printer.hdc, 80, 80, 160*10, 160*10)

' That should print a rectangle 16cm by 16cm

I have not been able to test this but give it a try




0
 
liljegrenAuthor Commented:

I tried, but I again, there is no connection to the metric system in the output. The values 500, 500, 800, 800, prints as 1 square inch, not as ~ 3 inches (8 cm or 80 mm). I declared the API function and gave the MM_LOWMETRIC constant value 1. Here's the rest of the code in case I did something wrong:

Public Sub PrintRectangle()
    Dim b As Boolean
   
    Printer.Print
    b = SetMapMode(Printer.hdc, MM_LOMETRIC)
    b = Rectangle(Printer.hdc, 500, 500, 800, 800)
    Printer.EndDoc
End Sub

By the way, is Printer.DrawWidth applicable in this context, or do I need another API call to suit it to the SetMapMode function?
0
 
InteqamCommented:
The Rectangle function draws a rectangle. The rectangle is outlined by using the current pen and filled by using the current brush.

BOOL Rectangle(

    HDC  hdc,      // handle of device context
    int  nLeftRect,      // x-coord. of bounding rectangle's upper-left corner
    int  nTopRect,      // y-coord. of bounding rectangle's upper-left corner
    int  nRightRect,      // x-coord. of bounding rectangle's lower-right corner  
    int  nBottomRect       // y-coord. of bounding rectangle's lower-right corner  
   );      
Parameters

hdc

Identifies the device context.

nLeftRect

Specifies the logical x-coordinate of the upper-left corner of the rectangle.

nTopRect

Specifies the logical y-coordinate of the upper-left corner of the rectangle.

nRightRect

Specifies the logical x-coordinate of the lower-right corner of the rectangle.

nBottomRect

Specifies the logical y-coordinate of the lower-right corner of the rectangle.

Return Value

If the function succeeds, the return value is TRUE.
If the function fails, the return value is FALSE. To get extended error information, call GetLastError.

Remarks

The current position is neither used nor updated by Rectangle.
0
 
liljegrenAuthor Commented:
Thanks for help. I know about the Line function, but is obsessed by the thought of making things in the "optimal" way. :-) Obviously the API function Rectangle is not optimal in this case.
0
All Courses

From novice to tech pro — start learning today.