Intercepting the Windows Print Dialog

bjwade62

Well-known member
Joined
May 25, 2006
Messages
50
Programming Experience
3-5
I'm looking for a way to intercept the Windows Print Dialog no matter what program calls it. I want to create a dialog that asks for a project number first and then calls the Windows Print Dialog.

I want to create a billing log that will record a project number every time any program attempts to print. The first step is to intercept the Windows Print Dialog. Can anyone help?

Thanks.
 
Try smoke and mirrors... Create a form that you call when someone tries to print, capture your data, display the print dialog, save the settings to global variables and close the form. You have captured your data and it still looks like a standard print dialog (cause it is) it's just on a seperate form...
 
WMI may or may not do what is necessary for you to monitor all printjobs and allow you to assign project id. Try this code to get the printjob started events, the app must be in Release build configuration mode to work. You also need Add Reference to the System.Management.dll (.Net).
VB.NET:
Imports system.management
Public Class Form2
 
    WithEvents watcher As ManagementEventWatcher
 
    Private Sub Form2_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) _
    Handles Me.FormClosed
        watcher.Stop()
    End Sub
 
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
        Dim q As New WqlEventQuery("__InstanceCreationEvent")
        q.WithinInterval = New TimeSpan(0, 0, 2)
        q.Condition = "TargetInstance ISA 'Win32_PrintJob'"
        watcher = New ManagementEventWatcher(q)
        watcher.Start()
    End Sub
 
    Private Sub watcher_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) _
    Handles watcher.EventArrived
        Dim printjob As ManagementBaseObject = e.NewEvent.Item("TargetInstance")
        Dim sb As New System.Text.StringBuilder
        sb.AppendLine("This printjob just started:")
        For Each p As PropertyData In printjob.Properties
            sb.AppendFormat("{0}: {1}", p.Name, p.Value)
            sb.AppendLine()
        Next
        MsgBox(sb.ToString)
 
    End Sub
End Class
Perhaps you also only want to use finished printjobs, in that case use the __InstanceDeletionEvent class instead.

Adding this code in EventArrived handler will allow you to Pause and Resume the printjob if that should be necessary, but you have to pause it right away else you'll be too late and the printjob would have finished already. I can't find a way to cancel a single printjob with WMI. There may not be a need for this since the above code will catch all printjobs and allow you to assign them anyway.
VB.NET:
        Dim q As New WqlObjectQuery("SELECT * FROM Win32_PrintJob WHERE JobId=" & printjob("JobId"))
        Dim search As New ManagementObjectSearcher(q)
        For Each mo As ManagementObject In search.Get()
            mo.InvokeMethod("Pause", Nothing, Nothing)
            'assign some id with a dialog here.
            mo.InvokeMethod("Resume", Nothing, Nothing)
            Exit For
        Next
 
Back
Top