Help with Graphics classes
Hope someone can help with the coding:I dont have the clue...any advise will be appreciated. This is my first post!:)
Need to design a simple greetings card application, the card could be folded horizontally across the middle and one line of text positioned in the bottom half. Th egraphics needs to be displaye don a panel
IN SUMMARY :The application has 2 classes:
card class that stores text,font,coulour and picture
cardadmin class that will inherit from PrintDocument and have instance of card as a field. It will also have a bitmap, display as a read only property.
1) Need to code the event handler for the paint event of card panel for displaying the card on the panel
2) Also need to code the forms update view() method so that the view on the form is in consistent with the model?
3) In the printcard method of cardadmin, a rectangle to match the printing area is created. How does the printcard code ensure that the position and size of the printed text match what is drawn on the bitmap property display.
Clases below:
======
card class:
===========
ublic Class Card
Private fText As String
Private fTextFont As Font
Private fTextColor As Color
Private fPicture As Bitmap
Public Property Text() As String
Get
Return fText
End Get
Set(ByVal value As String)
fText = value
End Set
End Property
Public Property TextFont() As Font
Get
Return fTextFont
End Get
Set(ByVal value As Font)
fTextFont = value
End Set
End Property
Public Property TextColor() As Color
Get
Return fTextColor
End Get
Set(ByVal value As Color)
fTextColor = value
End Set
End Property
Public Property Picture() As Bitmap
Get
Return fPicture
End Get
Set(ByVal value As Bitmap)
fPicture = value
End Set
End Property
Public Sub New()
Text = "Your Greeting"
TextFont = New Font(System.Drawing.FontFamily.GenericSansSerif, 14, FontStyle.Bold)
TextColor = Color.Black
Picture = Nothing
End Sub
End Class
cardadmin class:
================
Imports System.Drawing.Printing
Public Class CardAdmin
Inherits PrintDocument
Private fCard As Card
Private fDisplay As Bitmap
Private fTextPosition As Point
Public ReadOnly Property Display() As Bitmap
Get
Return fDisplay
End Get
End Property
Public Property TextPosition() As Point
Get
Return fTextPosition
End Get
Set(ByVal value As Point)
fTextPosition = value
createDisplay()
End Set
End Property
''' <summary>
''' An instance of CardAdmin is created so that the Display
''' property returns a bitmap of size 276 by 390 (proportional to A4 print area). ''' </summary>
''' <remarks></remarks>
Public Sub New()
MyBase.New()
fCard = New Card
fDisplay = New Bitmap(DefaultPageSettings.Bounds.Width \ 3, DefaultPageSettings.Bounds.Height \ 3)
fTextPosition = New Point(0, Display.Height \ 2)
createDisplay()
End Sub
Private Function findPictureRectangle(ByVal pageSize As RectangleF, ByVal pic As Bitmap) As RectangleF
Dim w As Single
Dim h As Single
Dim scale As Single
If pic.Width / pic.Height > 1 Then
w = Convert.ToSingle(0.9 * pageSize.Width)
scale = Convert.ToSingle(w / pic.Width)
h = scale * pic.Height
Else
h = Convert.ToSingle(0.45 * (pageSize.Height))
scale = Convert.ToSingle(h / pic.Height)
w = scale * pic.Width
End If
Return New RectangleF((pageSize.Width - w) / 2, (3 * pageSize.Height / 2 - h) / 2, w, h)
End Function
Public Sub setText(ByVal someText As String)
fCard.Text = someText
createDisplay()
End Sub
Public Sub setTextFont(ByVal aFont As Font, ByVal col As Color)
fCard.TextFont = aFont
fCard.TextColor = col
createDisplay()
End Sub
Public Sub setPicture(ByVal fileName As String)
fCard.Picture = New Bitmap(fileName)
createDisplay()
End Sub
Public Sub createDisplay()
Dim g As Graphics
Dim rect As RectangleF
rect = New Rectangle(0, 0, Display.Width, Display.Height)
g = Graphics.FromImage(Display)
drawCard(g, rect, fCard.TextFont, TextPosition.X, TextPosition.Y)
g.Dispose()
End Sub
Private Sub drawCard(ByVal g As Graphics, ByVal rect As RectangleF, ByVal curFont As Font, ByVal textX As Single, ByVal textY As Single)
Dim textBrush As SolidBrush
textBrush = New SolidBrush(fCard.TextColor)
g.Clear(Color.White)
If fCard.Picture IsNot Nothing Then
g.DrawImage(fCard.Picture, findPictureRectangle(rect, fCard.Picture))
End If
g.DrawString(fCard.Text, curFont, textBrush, textX, textY)
End Sub
Public Sub printCard(ByVal e As PrintPageEventArgs)
Dim top As Single
Dim left As Single
Dim width As Single
Dim height As Single
Dim rect As RectangleF
Dim aFont As Font
Dim scaleX As Single
Dim scaleY As Single
scaleX = Convert.ToSingle(DefaultPageSettings.Bounds.Width / Display.Width)
scaleY = Convert.ToSingle(DefaultPageSettings.Bounds.Height / Display.Height)
top = DefaultPageSettings.Margins.Top
left = DefaultPageSettings.Margins.Left
width = DefaultPageSettings.Bounds.Width
height = DefaultPageSettings.Bounds.Height
rect = New RectangleF(left, top, width, height)
aFont = New Font(fCard.TextFont.FontFamily, fCard.TextFont.Size * scaleY, fCard.TextFont.Style)
drawCard(e.Graphics, rect, aFont, TextPosition.X * scaleX, TextPosition.Y * scaleY)
e.HasMorePages = False
End Sub
Public Sub close()
fDisplay.Dispose()
End Sub
End Class