Class: GDIRectangle in Category Microsoft Visual Basic 6 : Graphics from Total Visual SourceBook

Class that draws a rectangle, using Windows API GDI calls in VB6.

A rectangle is a region whose location is determined by the Left, Top, Right and Bottom properties of this class. The angles of the corners of the rectangle are always drawn at 90 degrees. The routines in this class allow you to draw the rectangle by calling the Windows API Rectangle function.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CGDIRectangle class.
BorderColor Property Get the current value of the color used to draw the rectangle.
BorderWidth Property Get the current value of the BorderWidth used to draw the rectangle.
Bottom Property Get the Bottom location of the rectangle.
BrushStyle Property Get the current style used to fill the rectangle. The value of this property should be set to one of the Windows API Brush Style constants (BS_SOLID, BS_HOLLOW, BS_HATCHED etc.), which are exposed as EnumRectangleBrushStyles constants in this class.
FillColor Property Get the current value of the color used to fill the rectangle.
ForegroundMixMode Property Get the current value of the ForegroundMixMode property, which defines how colors from the pen and the colors in the existing image are to be combined.
HatchStyle Property Get the current value of the style used to fill the rectangle when the BrushStyle is set to hatched. The value of this property should be set to one of the Windows API Hatch Style constants (HS_HORIZONTAL, HS_BDIAGONAL, HS_CROSS etc.) which are exposed as EnumRectangleHatchStyles constants in this class.
Left Property Get the Left location of the rectangle.
PenStyle Property Get the value of the style used to draw the rectangle. The value of this property should be set to one of the Windows API Pen Style constants (PS_SOLID, PS_DASHDOT etc.), which are exposed as EnumRectanglePenStyles constants in this class.
Right Property Get the Right location of the rectangle.
Top Property Get the Top location of the rectangle.
Class_Initialize Initialize Set initial values to defaults which may be overridden with property settings.
Draw3DRectangle Method Draws two Rectangles on a selected device so that they create a 3D effect. This method ignores the regular BorderColor and FillStyle properties, and instead uses the colors which are passed to it to draw a 3D rectangle.
DrawRectangle Method Draw a rectangle on a selected device.
TranslateVBColor Private Translates VBA/VB6 color constants for system colors into GDI equivalents. VBA/VB6 color constants, such as vbRed, vbBlue, or values returned using the QBColor() or RGB() functions, can be used directly for the color values used with Windows API calls. However, the VBA/VB6 color constants for system colors (vbButtonFace, vb3DShadow etc.) do not map correctly to those expected by Windows. This function accepts either form of constant and translates the value, if necessary, into the equivalent value expected by the Windows GDI subsystem.
' Example of the CGDIRectangle class
'
' To try this example, do the following:
' 1. Create a new form
' 2. Paste all the code from this example to the new form's module
' 3. Run the form and click on the background. Two overlapping rectangles are drawn on the form around the location of the click.
'    A 3d rectangle is drawn around the outside of the other two rectangles

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)

  Dim GDIRectangle As CGDIRectangle
  Set GDIRectangle = New CGDIRectangle

  ' Erase the form background
  cls

  ' Draw two overlapping rectangles

  With GDIRectangle
    ' First rectangle is solid green with a red border
    .BorderColor = vbRed
    .FillColor = vbGreen
    .BrushStyle = rbsBS_SOLID

    .Left = ScaleX(X, vbTwips, vbPixels) - 50
    .Top = ScaleY(Y, vbTwips, vbPixels) - 50
    .Right = ScaleX(X, vbTwips, vbPixels) + 50
    .Bottom = ScaleY(Y, vbTwips, vbPixels) + 50

    ' Draw the first rectangle
    .DrawRectangle Me.hDC

    ' Second rectangle is hatched yellow with a blue border
    .Left = .Left + 25
    .Top = .Top + 25
    .Right = .Right + 25
    .Bottom = .Bottom + 25

    .BorderColor = vbBlue
    .FillColor = vbYellow
    .BrushStyle = rbsBS_HATCHED
    .HatchStyle = rhsHS_DIAGCROSS

    ' Draw the second rectangle
    .DrawRectangle Me.hDC

    ' Draw the 3D rectangle
    .Left = ScaleX(X, vbTwips, vbPixels) - 75
    .Top = ScaleY(Y, vbTwips, vbPixels) - 75
    .Right = ScaleX(X, vbTwips, vbPixels) + 100
    .Bottom = ScaleY(Y, vbTwips, vbPixels) + 100

    .Draw3DRectangle Me.hDC, vb3DShadow, vb3DHighlight

  End With

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS