Shading 
Author Message
 Shading

Is there any routine in VB (or an API) that would fill an area with
shading?  In particuliar, using the PrinterObject a triangle is drawn
- can it be filled?

Additional info:  I've created a "miniAudo-Cad" application that will
accept coordinates as depicted on a land deed.  (SE 36 Degrees, 12
Minutes, 6 Seconds, etc. )  I use the PrinterObject to "draw" these
lines.  For each line drawn I know the StartX & Y and the EndX & Y.

Occasionally a deed will provided the coordinates of an "entire" track
of land and then give the coordinates of a plot that falls within this
track but is excluded.  My cad program handles this by allowing the
user to enter a coordinate and "not" draw the line - just position the
print position to the StartX & Y.  Those are the areas I would like to
shade.

Lee Peedin
VP RexxLA www.rexxla.org
Start planning now for the 25th Anniversary of Rexx Symposium,
Spring 2004 in Boeblingen, Germany.



Mon, 19 Dec 2005 02:24:48 GMT  
 Shading
You can select a brush of the desired color into the DC and then use
the Polygon API function to draw the triangle or other shape.

HTH,
Bryan
____________________________________________________________
New Vision Software                   "When the going gets weird,"
Bryan Stafford                        "the weird turn pro."

Microsoft MVP-Visual Basic     Fear and Loathing in LasVegas

On Wed, 02 Jul 2003 14:24:48 -0400, Lee Peedin

Quote:

>Is there any routine in VB (or an API) that would fill an area with
>shading?  In particuliar, using the PrinterObject a triangle is drawn
>- can it be filled?

>Additional info:  I've created a "miniAudo-Cad" application that will
>accept coordinates as depicted on a land deed.  (SE 36 Degrees, 12
>Minutes, 6 Seconds, etc. )  I use the PrinterObject to "draw" these
>lines.  For each line drawn I know the StartX & Y and the EndX & Y.

>Occasionally a deed will provided the coordinates of an "entire" track
>of land and then give the coordinates of a plot that falls within this
>track but is excluded.  My cad program handles this by allowing the
>user to enter a coordinate and "not" draw the line - just position the
>print position to the StartX & Y.  Those are the areas I would like to
>shade.

>Lee Peedin
>VP RexxLA www.rexxla.org
>Start planning now for the 25th Anniversary of Rexx Symposium,
>Spring 2004 in Boeblingen, Germany.



Mon, 19 Dec 2005 03:14:53 GMT  
 Shading
Look for help on GradientFill API. Please note that this is not
supported on Win 95 platform.

Searching for "GradientFill VB" in google should get you a number of
sample demonstrating its use in VB.

- Vikram
--------------------------------------
Components For Your Software Solutions
http://www.viklele.com

Quote:

> Is there any routine in VB (or an API) that would fill an area with
> shading?  In particuliar, using the PrinterObject a triangle is drawn
> - can it be filled?

> Additional info:  I've created a "miniAudo-Cad" application that will
> accept coordinates as depicted on a land deed.  (SE 36 Degrees, 12
> Minutes, 6 Seconds, etc. )  I use the PrinterObject to "draw" these
> lines.  For each line drawn I know the StartX & Y and the EndX & Y.

> Occasionally a deed will provided the coordinates of an "entire" track
> of land and then give the coordinates of a plot that falls within this
> track but is excluded.  My cad program handles this by allowing the
> user to enter a coordinate and "not" draw the line - just position the
> print position to the StartX & Y.  Those are the areas I would like to
> shade.

> Lee Peedin
> VP RexxLA www.rexxla.org
> Start planning now for the 25th Anniversary of Rexx Symposium,
> Spring 2004 in Boeblingen, Germany.



Mon, 19 Dec 2005 18:12:53 GMT  
 Shading
Thanks,
Here's a script from AllApi.net that has been modified for different
coordinates,  It draws and fills fine on the screen, but I'm having
trouble printing. I simply changed me.hdc to Printer.
It gives an error "Object doesn't support this property or method.

I may be out of my league here :-).

Private Type COORD
    x As Long
    y As Long
End Type
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any,
ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long,
lpPoint As Any, ByVal nCount As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal
hRgn As Long, ByVal hBrush As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As
Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As
Long) As Long
Const ALTERNATE = 1 ' ALTERNATE and WINDING are
Const WINDING = 2 ' constants for FillMode.
Const BLACKBRUSH = 4 ' Constant for brush type.
Private Sub Form_Paint()
    'KPD-Team 1999
    'URL: http://www.allapi.net/

    Dim poly(1 To 6) As COORD, NumCoords As Long, hBrush As Long, hRgn
As Long
    Me.Cls
    ' Number of vertices in polygon.
    NumCoords = 6
    ' Set scalemode to pixels to set up points of triangle.

    Me.ScaleMode = vbPixels
    ' Assign values to points.

    poly(1).x = 25.25
    poly(1).y = 226.920325621065

    poly(2).x = 35.9334944186
    poly(2).y = 52.2467358972382

    poly(3).x = 292.717713514417
    poly(3).y = 86.0530196822316

    poly(4).x = 281.150846728748
    poly(4).y = 275.169619939223

    poly(5).x = 200.910035680077
    poly(5).y = 253.443848554724

    poly(6).x = 25.2511588075554
    poly(6).y = 226.920623338239

    ' Polygon function creates unfilled polygon on screen.
    ' Remark FillRgn statement to see results.
    Polygon Me.hdc, poly(1), NumCoords
    ' Gets stock black brush.
    hBrush = GetStockObject(BLACKBRUSH)
    ' Creates region to fill with color.
    hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
    ' If the creation of the region was successful then color.
    If hRgn Then FillRgn Me.hdc, hRgn, hBrush
    DeleteObject hRgn
End Sub
Private Sub Form_Resize()
    Form_Paint
End Sub

On Wed, 02 Jul 2003 13:14:53 -0600, alpine

Quote:

>You can select a brush of the desired color into the DC and then use
>the Polygon API function to draw the triangle or other shape.

>HTH,
>Bryan
>____________________________________________________________
>New Vision Software                   "When the going gets weird,"
>Bryan Stafford                            "the weird turn pro."

>Microsoft MVP-Visual Basic     Fear and Loathing in LasVegas

>On Wed, 02 Jul 2003 14:24:48 -0400, Lee Peedin

>>Is there any routine in VB (or an API) that would fill an area with
>>shading?  In particuliar, using the PrinterObject a triangle is drawn
>>- can it be filled?

>>Additional info:  I've created a "miniAudo-Cad" application that will
>>accept coordinates as depicted on a land deed.  (SE 36 Degrees, 12
>>Minutes, 6 Seconds, etc. )  I use the PrinterObject to "draw" these
>>lines.  For each line drawn I know the StartX & Y and the EndX & Y.

>>Occasionally a deed will provided the coordinates of an "entire" track
>>of land and then give the coordinates of a plot that falls within this
>>track but is excluded.  My cad program handles this by allowing the
>>user to enter a coordinate and "not" draw the line - just position the
>>print position to the StartX & Y.  Those are the areas I would like to
>>shade.

>>Lee Peedin
>>VP RexxLA www.rexxla.org
>>Start planning now for the 25th Anniversary of Rexx Symposium,
>>Spring 2004 in Boeblingen, Germany.



Mon, 19 Dec 2005 21:21:37 GMT  
 Shading
Hmm..  This didn't go through last time it seems, let's try again...

Quote:
> Is there any routine in VB (or an API) that would fill an area with
> shading?  In particuliar, using the PrinterObject a triangle is drawn
> - can it be filled?

> Additional info:  I've created a "miniAudo-Cad" application that will
> accept coordinates as depicted on a land deed.  (SE 36 Degrees, 12
> Minutes, 6 Seconds, etc. )  I use the PrinterObject to "draw" these
> lines.  For each line drawn I know the StartX & Y and the EndX & Y.

> Occasionally a deed will provided the coordinates of an "entire" track
> of land and then give the coordinates of a plot that falls within this
> track but is excluded.  My cad program handles this by allowing the
> user to enter a coordinate and "not" draw the line - just position the
> print position to the StartX & Y.  Those are the areas I would like to
> shade.

The Printer object has a .hDC property to which you can use the API drawing
calls to draw to.  Here's an example of drawing a filled triangle with no
border to the printer:

'*** (Warning; untested)
Private Declare Function Polygon Lib "gdi32" (ByVal hDC As Long, _
    ByRef lpPoint As PointAPI, ByVal nCount As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As
Long) As Long
Private Declare Function SelectObject Lib "gdi32" ( _
    ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As
Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long)
As Long

Private Type PointAPI
    X As Long
    Y As Long
End Type

Private Const NULL_PEN As Long = 8

Private Sub PrintTri()
    Dim Points(2) As PointAPI
    Dim DrawBrush As Long
    Dim OldBrush As Long, OldPen As Long

    Points(0) = NewPoint(200, 200)
    Points(1) = NewPoint(50, 200)
    Points(2) = NewPoint(200, 50)

    DrawBrush = CreateSolidBrush(vbRed)
    OldBrush = SelectObject(Printer.hDC, DrawBrush)
    OldPen = SelectObject(Printer.hDC, GetStockObject(NULL_PEN))

    Call Polygon(Printer.hDC, Points(0), 3)

    Call DeleteObject(SelectObject(Printer.hDC, OldBrush))
    Call SelectObject(Printer.hDC, OldPen)

    Call Printer.EndDoc
End Sub

Private Function NewPoint(ByVal inX As Long, ByVal inY As Long) As PointAPI
    NewPoint.X = inX
    NewPoint.Y = inY
End Function
'***

If you wanted to draw it with a border then you'd need to create and select
a pen into the DC as well as the brush, a quick search of these groups for
CreatePen() should turn up plenty of results on that though.  You mentioned
a custom scale mode too, if you want to implement this directly into the
drawing coordinates then you'd need to look into the SetMapMode() and
SetWindow/ViewportExtEx() calls too, again you should be able to find
information on those on these groups but if not post back here and we'll
provide some sample code for you.
Hope this helps,

    Mike

 - Microsoft Visual Basic MVP -

WWW: Http://www.mvps.org/EDais/



Mon, 19 Dec 2005 21:30:52 GMT  
 Shading
Mike,
Thanks for the help - your code executes without error, but nothing
happens :-).  Like I said, I may be out of my league, but I'm trying.
Lee

On Thu, 3 Jul 2003 14:30:52 +0100, "Mike D Sutton"

Quote:

>Hmm..  This didn't go through last time it seems, let's try again...

>> Is there any routine in VB (or an API) that would fill an area with
>> shading?  In particuliar, using the PrinterObject a triangle is drawn
>> - can it be filled?

>> Additional info:  I've created a "miniAudo-Cad" application that will
>> accept coordinates as depicted on a land deed.  (SE 36 Degrees, 12
>> Minutes, 6 Seconds, etc. )  I use the PrinterObject to "draw" these
>> lines.  For each line drawn I know the StartX & Y and the EndX & Y.

>> Occasionally a deed will provided the coordinates of an "entire" track
>> of land and then give the coordinates of a plot that falls within this
>> track but is excluded.  My cad program handles this by allowing the
>> user to enter a coordinate and "not" draw the line - just position the
>> print position to the StartX & Y.  Those are the areas I would like to
>> shade.

>The Printer object has a .hDC property to which you can use the API drawing
>calls to draw to.  Here's an example of drawing a filled triangle with no
>border to the printer:

>'*** (Warning; untested)
>Private Declare Function Polygon Lib "gdi32" (ByVal hDC As Long, _
>    ByRef lpPoint As PointAPI, ByVal nCount As Long) As Long
>Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As
>Long) As Long
>Private Declare Function SelectObject Lib "gdi32" ( _
>    ByVal hDC As Long, ByVal hObject As Long) As Long
>Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As
>Long
>Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long)
>As Long

>Private Type PointAPI
>    X As Long
>    Y As Long
>End Type

>Private Const NULL_PEN As Long = 8

>Private Sub PrintTri()
>    Dim Points(2) As PointAPI
>    Dim DrawBrush As Long
>    Dim OldBrush As Long, OldPen As Long

>    Points(0) = NewPoint(200, 200)
>    Points(1) = NewPoint(50, 200)
>    Points(2) = NewPoint(200, 50)

>    DrawBrush = CreateSolidBrush(vbRed)
>    OldBrush = SelectObject(Printer.hDC, DrawBrush)
>    OldPen = SelectObject(Printer.hDC, GetStockObject(NULL_PEN))

>    Call Polygon(Printer.hDC, Points(0), 3)

>    Call DeleteObject(SelectObject(Printer.hDC, OldBrush))
>    Call SelectObject(Printer.hDC, OldPen)

>    Call Printer.EndDoc
>End Sub

>Private Function NewPoint(ByVal inX As Long, ByVal inY As Long) As PointAPI
>    NewPoint.X = inX
>    NewPoint.Y = inY
>End Function
>'***

>If you wanted to draw it with a border then you'd need to create and select
>a pen into the DC as well as the brush, a quick search of these groups for
>CreatePen() should turn up plenty of results on that though.  You mentioned
>a custom scale mode too, if you want to implement this directly into the
>drawing coordinates then you'd need to look into the SetMapMode() and
>SetWindow/ViewportExtEx() calls too, again you should be able to find
>information on those on these groups but if not post back here and we'll
>provide some sample code for you.
>Hope this helps,

>    Mike

> - Microsoft Visual Basic MVP -

>WWW: Http://www.mvps.org/EDais/



Mon, 19 Dec 2005 22:02:31 GMT  
 Shading

Quote:
> Thanks for the help - your code executes without error, but nothing
> happens :-).  Like I said, I may be out of my league, but I'm trying.

Ok, try adding this line at the start of the function:

'***
Printer.Print ""
'***

Sometimes you need to kick-start the printer before it's device context is
created correctly.
Hope this helps,

    Mike

 - Microsoft Visual Basic MVP -

WWW: Http://www.mvps.org/EDais/



Mon, 19 Dec 2005 22:42:38 GMT  
 Shading
You da man - that did it.  Now I just got to work on converting my X/Y
coordinates (that are in inches) to pixels.
BTW: Is it correct that Polygon only works in pixels?  What I know so
far is that my printer is 600 DPI X & Y - now I'll need to find a
conversion routine.
Thanks
Lee

On Thu, 3 Jul 2003 14:42:38 +0000 (UTC), "Mike D Sutton"

Quote:

>> Thanks for the help - your code executes without error, but nothing
>> happens :-).  Like I said, I may be out of my league, but I'm trying.

>Ok, try adding this line at the start of the function:

>'***
>Printer.Print ""
>'***

>Sometimes you need to kick-start the printer before it's device context is
>created correctly.
>Hope this helps,

>    Mike

> - Microsoft Visual Basic MVP -

>WWW: Http://www.mvps.org/EDais/



Mon, 19 Dec 2005 23:50:42 GMT  
 Shading

Quote:
> You da man - that did it.  Now I just got to work on converting my X/Y
> coordinates (that are in inches) to pixels.
> BTW: Is it correct that Polygon only works in pixels?  What I know so
> far is that my printer is 600 DPI X & Y - now I'll need to find a
> conversion routine.

I mentioned this briefly in the original post, but you can use the mapping
mode of the DC (Device Context) to perform the scale conversions for you.
Here's an example of how to use mapping modes on a DC in VB:

'***
Private Declare Function SetWindowExtEx Lib "gdi32" _
    (ByVal hDC As Long, ByVal nX As Long, ByVal nY As Long, _
    ByRef lpSize As Any) As Long
Private Declare Function SetViewportExtEx Lib "gdi32" _
    (ByVal hDC As Long, ByVal nX As Long, ByVal nY As Long, _
    ByRef lpSize As Any) As Long
Private Declare Function SetMapMode Lib "gdi32" _
    (ByVal hDC As Long, ByVal nMapMode As Long) As Long
Private Declare Function LineTo Lib "gdi32" _
    (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" _
    (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, _
    ByRef lpPoint As Any) As Long
Private Declare Function CreatePen Lib "gdi32" _
    (ByVal nPenStyle As Long, ByVal nWidth As Long, _
    ByVal crColor As Long) As Long
Private Declare Function SelectObject Lib "gdi32" _
    (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" _
    (ByVal hObject As Long) As Long
Private Declare Function Rectangle Lib "gdi32" _
    (ByVal hDC As Long, ByVal X1 As Long, ByVal Y1 As Long, _
    ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function DPtoLP Lib "gdi32" _
    (ByVal hDC As Long, ByRef lpPoint As PointAPI, _
    ByVal nCount As Long) As Long

Private Type PointAPI
    X As Long
    Y As Long
End Type

Private Const MM_ANISOTROPIC As Long = &H8
Private Const MM_ISOTROPIC As Long = &H7
Private Const PS_SOLID As Long = &H0
Private Const PS_DOT As Long = &H2

Private Sub Form_Paint()
    Dim DrawPen As Long, OldPen As Long
    Dim OldView As PointAPI, OldWnd As PointAPI
    Dim PenWidths(1) As PointAPI
    Dim OldMode As Long
    Dim DrawDC As Long

    ' Clear the current drawing, and grab the DC handle
    Call Form1.Cls
    DrawDC = Form1.hDC

    ' Set the mapping mode and the window scale
    '   This is our custom scale, i.e. the mapping mode we want to draw at
    '   From GDI's point of view, this is referred to as 'logical space'
    ' Try changing this to Anisotropic to see the difference between the two
    OldMode = SetMapMode(DrawDC, MM_ISOTROPIC)
    Call SetWindowExtEx(DrawDC, 1000, 1000, OldWnd)

    With Form1
        ' Set the viewport to the size of the form
        '   From GDI's point of view, this is referred to as 'device space'
        Call SetViewportExtEx(DrawDC, _
            .ScaleX(.ScaleWidth, .ScaleMode, vbPixels), _
            .ScaleY(.ScaleHeight, .ScaleMode, vbPixels), OldView)
    End With

    ' Set the size's of the pens - this will keep the pen's size
    '   constant regardless of the scale mode we've chosen.
    '   You can use the same principle for making sure anything
    '   else such as shape coordinates or text sizes stay the
    '   same size regardless of scale
    PenWidths(0).X = 1 ' This is the dotted border
    PenWidths(0).Y = 1
    PenWidths(1).X = 5 ' This is the cross
    PenWidths(1).Y = 5

    ' Convert the pen width's from pixels (Device space) to
    ' our custom scale mode (Logical space) - Hence; "DP to LP"
    Call DPtoLP(DrawDC, PenWidths(0), 2)

    ' Draw the viewport area at this scalemode
    DrawPen = CreatePen(PS_DOT, PenWidths(0).X, vbBlack)
    OldPen = SelectObject(DrawDC, DrawPen)
    Call Rectangle(DrawDC, 0, 0, 1000, 1000)
    Call DeleteObject(SelectObject(DrawDC, OldPen))

    ' Create the red pen for the cross
    DrawPen = CreatePen(PS_SOLID, PenWidths(1).X, vbRed)
    OldPen = SelectObject(DrawDC, DrawPen)

    ' Draw the cross in the middle of the window
    Call MoveToEx(DrawDC, 100, 100, ByVal 0&)
    Call LineTo(DrawDC, 900, 900)
    Call MoveToEx(DrawDC, 100, 900, ByVal 0&)
    Call LineTo(DrawDC, 900, 100)

    ' De-select and destroy the pen
    Call DeleteObject(SelectObject(DrawDC, OldPen))

    ' Re-set the DC's properties to keep VB happy
    Call SetMapMode(DrawDC, OldMode)
    Call SetWindowExtEx(DrawDC, OldWnd.X, OldWnd.Y, ByVal 0&)
    Call SetViewportExtEx(DrawDC, OldView.X, OldView.Y, ByVal 0&)
End Sub
'***

This one just draws on the form's surface when it paints, however you can
use the same principle with the printer (They're both DC's.)
This example show's you how to create an arbitrary scalemode, however you
can use one of the pre-defined ones and skip the SetViewPort/WindowExtEx()
calls.  In your case, the MM_*ENGLISH modes would most likely work well
since they're based on inches, however be warned that the Y axis is reversed
from the normal (Cartesian) coordinate system.  You'll find more information
on mapping modes here:
http://msdn.microsoft.com/library/en-us/gdi/cordspac_3c6d.asp
Hope this helps,

    Mike

 - Microsoft Visual Basic MVP -

WWW: Http://www.mvps.org/EDais/



Tue, 20 Dec 2005 05:48:41 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. shading/coloring values

2. Macro/VBA Code for changing the cell shade color

3. Word Color Shading problems in Excel VBA

4. Shading a frame in a style

5. Shade only a checkbox

6. Page shading

7. Table row shading

8. Mapping Borders and Shading from XL to WD

9. Borders and Shading Built-in dialog box

10. Problem with background shading some text

11. Printing textbox without shading

12. shaded color for form fields

 

 
Powered by phpBB® Forum Software