?
Solved

Using "Rectangle" from Win32 API

Posted on 1999-07-01
7
Medium Priority
?
1,397 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:liljegren
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:setiawan
ID: 1521408
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
 

Author Comment

by:liljegren
ID: 1521409
1. True.
2. Yes, 8 cm = 80 mm (about 3 inches).
3. The rectangle gets bigger (about 13,5 mm).
0
 
LVL 1

Expert Comment

by:JustinB
ID: 1521410
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:liljegren
ID: 1521411

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
 
LVL 1

Accepted Solution

by:
JustinB earned 450 total points
ID: 1521412
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
 
LVL 7

Expert Comment

by:Inteqam
ID: 1521413
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
 

Author Comment

by:liljegren
ID: 1521414
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

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question