Setting RichTextBox Margins from Database

  • Thread starter Thread starter Jason Bodine
  • Start date Start date
J

Jason Bodine

Guest
Hi all,

I have a MDI WinForms application which uses a database to store and retrieve settings. I am trying to get the RichTextBox on the document form to display its contents based on Top, Bottom, Left, and Right margins from the database and have tried it a number of ways, but no matter what I do, the display of the RTB is always at 0,0! Can someone please help? Here's the code I currently have.

In the document form, I have:

Public Sub GetMargins()
Dim LineWidth As Integer = 0
LineWidth = SetMargins(Me.rtfDocument, frmMain.PrintDocument1)
Me.rtfDocument.Refresh()
End Sub


I call this from the form's Load event, and the sub, in turn, calls from a module:

Imports PJE7.PJE7
Imports CustomControls.CustomControls
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Module modMargins
Public Const WM_USER As Long = &H400
Public Const EM_FORMATRANGE As Long = WM_USER + 57
Public Const EM_SETTARGETDEVICE As Long = WM_USER + 72
Public Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As Integer, ByVal lpInitData As Integer) As Integer
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Function SetMargins(ByRef RTF As ExRichTextBox, ByRef pd As PrintDocument)
Dim LineWidth As Integer, PrinterhDC As Integer, s As Integer, L As Integer, R As Integer, T As Integer, B As Integer
Dim DB As New Reader
DB.ProfileName = Declarations.ActiveProfile
Select Case DB.MarginUnits
Case Is = "Inches"
T = CInt(Decimal.Parse(DB.TopMargin) * 1440)
B = CInt(Decimal.Parse(DB.BottomMargin) * 1440)
L = CInt(Decimal.Parse(DB.LeftMargin) * 1440)
R = CInt(Decimal.Parse(DB.RightMargin) * 1440)
Case Is = "Centimeters"
T = CInt((Decimal.Parse(DB.TopMargin) * 1440) / 2.54)
B = CInt((Decimal.Parse(DB.BottomMargin) * 1440) / 2.54)
L = CInt((Decimal.Parse(DB.LeftMargin) * 1440) / 2.54)
R = CInt((Decimal.Parse(DB.RightMargin) * 1440) / 2.54)
End Select
pd.DefaultPageSettings.Margins.Top = T / Declarations.AnInch
pd.DefaultPageSettings.Margins.Bottom = B / Declarations.AnInch
pd.DefaultPageSettings.Margins.Left = L / Declarations.AnInch
pd.DefaultPageSettings.Margins.Right = R / Declarations.AnInch

DB.Dispose()
PrinterhDC = CreateDC(pd.DefaultPageSettings.PrinterSettings.GetHdevnames.ToString, pd.DefaultPageSettings.PrinterSettings.PrinterName, 0, 0)
If pd.DefaultPageSettings.Landscape Then
LineWidth = (pd.DefaultPageSettings.PaperSize.Height - (pd.DefaultPageSettings.Margins.Right + pd.DefaultPageSettings.Margins.Left) * Declarations.AnInch)
Else
LineWidth = (pd.DefaultPageSettings.PaperSize.Width - (pd.DefaultPageSettings.Margins.Right + pd.DefaultPageSettings.Margins.Left) * Declarations.AnInch)
End If
s = SendMessage(RTF.Handle, EM_SETTARGETDEVICE, PrinterhDC, LineWidth)
SetMargins = LineWidth
End Function
End Module


How can I actually get this to work? Thanks!

Jason

Continue reading...
 
Back
Top