*Experts* DiverDan Posted July 22, 2003 *Experts* Posted July 22, 2003 Setting a label's text alignment in either design mode or programmably is pretty simple. But how do you get the label's text alignment properity once its set for printing?? Thanks, Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
*Experts* DiverDan Posted July 22, 2003 Author *Experts* Posted July 22, 2003 Thanks VolteFace, that'll do it. With the text alignment properity known, how can this be applied to printing? The DrawString method does not include text alignment, only text as a string. Thanks, Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
*Experts* mutant Posted July 22, 2003 *Experts* Posted July 22, 2003 Some of the overloads of DrawString accept an argument called StringFormat, this is where you can use it: Dim strfmt As New System.Drawing.StringFormat strfmt.Alignment = StringAlignment.Center 'you have 3 choices here Then just pass in this as the StringFormat argument. Quote
*Experts* Volte Posted July 22, 2003 *Experts* Posted July 22, 2003 Also, use the LineAlignment property of StringFormat to set the vertical alignment. Note that this will only work properly if you supply the DrawString method with a full rectangle to draw with, rather than just the X and Y coordinates. Quote
*Experts* DiverDan Posted July 22, 2003 Author *Experts* Posted July 22, 2003 (edited) Thanks mutant and VolteFace!! Here's my application... Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 'setup the bmp image Dim bmp As Bitmap = New Bitmap(System.Drawing.Image.FromFile _ (Application.StartupPath & "\Data\" & DwgNumber & "a.jpg"), pnlDrawings.Width, pnlDrawings.Height) Dim g As Graphics = Graphics.FromImage(bmp) Dim lbl As Label Dim strfmt As New System.Drawing.StringFormat() For Each lbl In Me.pnlDrawings.Controls If lbl.Text <> Nothing Then If lbl.TextAlign = 1 Then strfmt.Alignment = StringAlignment.Center ElseIf lbl.TextAlign = 2 Then strfmt.Alignment = StringAlignment.Near ElseIf lbl.TextAlign = 3 Then strfmt.Alignment = StringAlignment.Far End If g.DrawString(lbl.Text, lbl.Font, New SolidBrush(Color.Black), lbl.Location.X, lbl.Location.Y, strfmt) End If Next etc... I'm still having a problem with the alignment??? Thanks Dan Edited July 22, 2003 by DiverDan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
*Experts* Volte Posted July 22, 2003 *Experts* Posted July 22, 2003 Did you not see what I said in my last post? You must provide DrawString with a full rectangle (4 coordinates) to draw in. Look for the DrawString method in the MSDN for information about the different overloads. After all, how can it right align text when it does know where "right" is? Quote
*Experts* DiverDan Posted July 22, 2003 Author *Experts* Posted July 22, 2003 Your right VolteFace...I overlooked it. This appears to be working correctly now: Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 'setup the bmp image Dim bmp As Bitmap = New Bitmap(System.Drawing.Image.FromFile _ (Application.StartupPath & "\Data\" & DwgNumber & "a.jpg"), pnlDrawings.Width, pnlDrawings.Height) Dim g As Graphics = Graphics.FromImage(bmp) Dim lbl As Label Dim strfmt As New System.Drawing.StringFormat() For Each lbl In Me.pnlDrawings.Controls If lbl.Text <> Nothing Then If lbl.TextAlign = ContentAlignment.TopCenter Then strfmt.Alignment = StringAlignment.Center ElseIf lbl.TextAlign = ContentAlignment.TopLeft Then strfmt.Alignment = StringAlignment.Near ElseIf lbl.TextAlign = ContentAlignment.TopRight Then strfmt.Alignment = StringAlignment.Far End If Dim lblRect As New RectangleF(lbl.Location.X, lbl.Location.Y, lbl.Width, lbl.Height) g.DrawString(lbl.Text, lbl.Font, New SolidBrush(Color.Black), lblRect, strfmt) End If Next etc... Thanks again VolteFace Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.