Do I need to send ESC commands through a windows driver? - vb.net

Can anyone help me.
Do I need to send printer commands when printing through a windows driver?
I am writing a windows forms application which prints out information on a label printer. This information contains an image which has a title and a barcode on it. I can create the image ok, but when I try to send it to the printer it prints completely wrong!

The Esc commands are normally handled through the Windows Driver. Here's rough example on how to print an image. You initialize PrintDocument, then use the Print method to start printing. Windows then calls the PrintPage handler, which draws on e.Graphics. e.Graphics is effectively the printer page.
Dim pd As PrintDocument
AddHandler pd.PrintPage, AddressOf printPage
' assign pd properties
pd.Print
Sub printPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
' assign img, set rectangle, determine number of pages, etc.
e.Graphics.DrawImage(img, rectangle) ' essentially draws on the printer page
e.HasMorePages = ...
end sub

Related

visual studio form Printing

I'm trying to print the form in visual studio when you press the P key but when its working but it keeps degrading the quality of the image/form and i don't know how to resize the form size in the printing.
this is the form i want to print
this is the degraded quality after i print the image
Private Sub IDPrint_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = "p" OrElse e.KeyChar = "P" Then
PrintDialog1.ShowDialog()
PrintForm1.PrinterSettings = PrintDialog1.PrinterSettings
PrintForm1.Print()
End If
End Sub
Power Pack Printform control is simply a bitmap copy of the screen. Since printers probably have a higher pixel resolution than monitors, the image you want to print will be stretched and appear fuzzy.
Considering Power Pack Printform is out of date and limited, I suggest you use the PrintDocument component to print from a Windows Form.
Here's the document you can refer to: How to: Print a Windows Form

VB.Net Print Form to PDF Printer

I am trying (and failing) right now to print a Windows Form as a PDF File in VB.Net
Now I know from doing some research that VB.Net does not have any built in function which could allow me to do this without using a third party application.
My way to get around this, was to attempt to print my Form using a PDF Printer, since logically that should work no? My issue is getting that to work properly.
This is currently my code for attempting to print:
Private Sub SelectPrinterThenPrint()
Dim PrintersDialog As New PrintDialog()
If PrintersDialog.SHowDialog(Me) = System.Forms.DialogResult.OK Then
Try
p_Document = New PrintDocument()
PrintersDialog.Document = p_Document
AddHandler p_Document.PrintPage, AddressOf HandleOnPrintPage
Catch ex As Exception
End Try
End If
End Sub
Private Sub HandleOnPrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles p_Document.PrintPage
Dim MorePagesPending = False
Dim bmp As New Bitmap(pnlContainer.Width, pnlContainer.Height)
pnlContainer.DrawToBitmap(bmp, pnlContainer.ClientRectangle)
e.Graphics.DrawImage(bmp, New Point(0,0))
If MorePagesPending Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
The SelectPrinterThenPrint() method is called when a button on my Form is clicked. Right now I am having two issues.
1) I don't know how to setup where the file gets saved. The Print Dialog opens, and I get to select my PDF Printer, but it doesn't let me set a filename. So how do I set the filename I want to be printing to?
2) Because I can't tell if the file is actually being saved, I can't tell if I am actually setting up the print properly or not. I am trying to print the entire contents of a panel, which holds all the elements I want on my PDF file. One thing I am unsure about however, is that the form the panel is displayed in, is not the same size as the panel. The Form the panel is in has the same width, but not the same height, with the form being set up for auto scrolling. Will I get the whole Panel to print? Or just the part the is visible in the form at the time of printing?

Click event for dynamically created labels VB.NET

For college, I am creating a gravitational field simulator where two masses are shown along with the magnitude and direction of the gravitational force that they will experience. For the bodies of mass I am using dynamically created ovalshapes, and these ovalshapes each have a label on it showing the mass. These ovalshapes and labels are stored in a listarray. I will need to be able to drag and drop these bodies. For the ovalshapes I am using If OvalShape.ContainsFocus but due to the fact that the labels will be ontop of these ovalshapes I need some sort of way to test whether the mouse is down on the labels. I have tried using an event handler but I get an error "click is not an event of system.collections.listarray" for this code AddHandler labelArray.Click, AddressOf Me.labelArray_ClickSo my questions is, Is there a ways to test whether a label belonging to a listarray has been clicked and which label within the array has been clicked on. Thanks in advance
You need to add the event handler to each label in the array
For Each l As Label In labelArray
AddHandler l.Click, AddressOf myClickHandler
Next
Then in the handler function:
Private Sub myClickHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim clickedLabel As Label = DirectCast(sender, Label) ' Cast the sending object into a Label object
' then do whatever you want with the label such as get it's text and show it in a message box:
MessageBox.Show("Label text Clicked was: " & clickedLabel.Text)
End Sub

Setting a windows form's screen position based on another form's current position

I am creating an application with multiple windows forms. The main form is movable, and I want a confirmation window to flash based on where the main form is located.
For example, the main form opens, user drags it 200 points to the left. How do I make sure the confirmation window, upon button-press, opens up exactly to the left of that window?
The built-in properties (center screen, center parent, etc.) don't provide this functionality.
I'm aware of these functions:
Form1.Left += 200
and
Dim frmAccounts as new Form()
Set FrmAccounts.DesktopLocation = new Point(100,100)
but neither of these take into consideration user dragging.
Any ideas?
Thanks for your help.
To keep the buddy glued to the main form, you have to use the main form's LocationChanged event to know when to move it. And you have to position it before it is displayed, that's a bit tricky due the form possibly getting rescaled on a machine with a different DPI setting. Best time to do it is when the buddy's Load event fires, it is rescaled by then. Some sample code:
Public Class Form1
Dim buddy As Form
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If buddy Is Nothing Then
buddy = New Form2
AddHandler buddy.Load, AddressOf MoveBuddy
AddHandler Me.LocationChanged, AddressOf MoveBuddy
AddHandler buddy.FormClosed, Sub() buddy = Nothing
buddy.Show(Me)
End If
End Sub
Private Sub MoveBuddy(sender As Object, e As EventArgs)
buddy.Bounds = New Rectangle(Me.Left - buddy.Width, Me.Top, buddy.Width, buddy.Height)
End Sub
End Class

Printing Photos using VB6 and/or .NET

Does anyone have any code suggestions or samples for printing photos (BMP or TIFF or JPEG), using Visual Basic or .NET framework?
VB6 and .NET handle printing quite differently. These examples are the bare minumum, just to give you an idea of the process.
In VB6 you control the printer step by step:
Private Sub PrintMyPicture()
'Place to store my picture
Dim myPicture As IPictureDisp
'Load the picture into the variable
Set myPicture = LoadPicture("C:\temp\myPictureFile.bmp")
'Draw the picture on the printer, just off the edge of the page
Printer.PaintPicture myPicture, 10, 10
'Done printing!
Printer.EndDoc
End Sub
Lo and behold, your picture will come out of the default printer. the PaintPicture method accepts width, height and a few other parameters to help get the image to fit, and the Printer object gives you all sorts of info about the printer.
In .Net it's kind of the other way around. You start printing, and the printer will raise an event for each page until you tell it to stop. Every page event gives you as graphics object upon which you can draw using all of the standard System.Drawing classes and methods:
'Class variable
Private WithEvents printer As System.Drawing.Printing.PrintDocument
' Kick off the printing process. This will cause the printer_PrintPage event chain to start firing.
Public Sub PrintMyPicture()
'Set up the printer
printer.PrinterSettings.PrinterName = "MyPrinterNameInPrintersAndFaxes"
printer.Print()
End Sub
'This event will keep firing while e.HasMorePages = True.
Private Sub printer_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printer.PrintPage
'Load the picture
Dim myPicture As System.Drawing.Image = System.Drawing.Image.FromFile("C:\temp\myPictureFile.bmp")
'Print the Image. 'e' is the Print events that the printer provides. In e is a graphics object on hwich you can draw.
'10, 10 is the position to print the picture.
e.Graphics.DrawImage(myPicture, 10, 10)
'Clean up
myPicture.Dispose()
myPicture = Nothing
'Tell the printer that there are no more pages to print. This will cause the document to be finalised and come out of the printer.
e.HasMorePages = False
End Sub
Again, there are a lot more parameters in DrawImage and the PrintDocument objects.