我使用了微软知识库的在NT系统下定义页面大小的代码,但里面没有写到关于如何设置纸张方向其中核心代码如下:Public Function SelectForm(FormName As String, ByVal MyhWnd As Long) _
    As Integer
Dim nSize As Long           ' Size of DEVMODE
Dim pDevMode As DEVMODE
Dim PrinterHandle As Long   ' Handle to printer
Dim hPrtDC As Long          ' Handle to Printer DC
Dim PrinterName As String
Dim aDevMode() As Byte      ' Working DEVMODE
Dim FormSize As SIZELPrinterName = Printer.DeviceName  ' Current printer
hPrtDC = Printer.hdc              ' hDC for current Printer
SelectForm = FORM_NOT_SELECTED    ' Set for failure unless reset in code.' Get a handle to the printer.
If OpenPrinter(PrinterName, PrinterHandle, 0&) Then
    ' Retrieve the size of the DEVMODE.
    nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, 0&, _
            0&, 0&)
    ' Reserve memory for the actual size of the DEVMODE.
    ReDim aDevMode(1 To nSize)    ' Fill the DEVMODE from the printer.
    nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _
            aDevMode(1), 0&, DM_OUT_BUFFER)
    ' Copy the Public (predefined) portion of the DEVMODE.
    Call CopyMemory(pDevMode, aDevMode(1), Len(pDevMode))
    
    ' If FormName is "MyCustomForm", we must make sure it exists
    ' before using it. Otherwise, it came from our EnumForms list,
    ' and we do not need to check first. Note that we could have
    ' passed in a Flag instead of checking for a literal name.
    If FormName = "MyCustomForm" Then
        ' Use form "MyCustomForm", adding it if necessary.
        ' Set the desired size of the form needed.
        With FormSize   ' Given in thousandths of millimeters
            .cx = 214000   ' width
            .cy = 216000   ' height
        End With
        If GetFormName(PrinterHandle, FormSize, FormName) = 0 Then
            ' Form not found - Either of the next 2 lines will work.
            'FormName = AddNewForm(PrinterHandle, FormSize, "MyCustomForm")
            AddNewForm PrinterHandle, FormSize, "MyCustomForm"
            If GetFormName(PrinterHandle, FormSize, FormName) = 0 Then
                ClosePrinter (PrinterHandle)
                SelectForm = FORM_NOT_SELECTED   ' Selection Failed!
                Exit Function
            Else
                SelectForm = FORM_ADDED  ' Form Added, Selection succeeded!
            End If
        End If
    End If
    
    ' Change the appropriate member in the DevMode.
    ' In this case, you want to change the form name.
    pDevMode.dmFormName = FormName & Chr(0)  ' Must be NULL terminated!   pDevMode.dmOrientation = 2    ' Set the dmFields bit flag to indicate what you are changing.
    pDevMode.dmFields = DM_FORMNAME    ' Copy your changes back, then update DEVMODE.
    Call CopyMemory(aDevMode(1), pDevMode, Len(pDevMode))
    nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _
            aDevMode(1), aDevMode(1), DM_IN_BUFFER Or DM_OUT_BUFFER)    nSize = ResetDC(hPrtDC, aDevMode(1))   ' Reset the DEVMODE for the DC.    ' Close the handle when you are finished with it.
    ClosePrinter (PrinterHandle)
    ' Selection Succeeded! But was Form Added?
    If SelectForm <> FORM_ADDED Then SelectForm = FORM_SELECTED  
Else
    SelectForm = FORM_NOT_SELECTED   ' Selection Failed!
End If
End Function
其中红色字是我加上去的,我想这样来定义纸张的方向为横向,虽然通过设置断点知道pDevMode.dmOrientation是返回2,但在打印时返回的是1,即纵向而不是横向。请问该加些什么令方向生效?我不想要网上另外的一种关于预设的那种方法,即dmOrientation = eOrientation 的那种,有一个好的现成无谓再加那么多代码上去,会变得很复杂很累赘的,所以如果非真的要用那种方法不行的话,请教怎么修改上面的代码令其能打印出横向纸呢?谢谢

解决方案 »

  1.   

    上面的红色字给代码格式格式化了,就是那段pDevMode.dmOrientation = 2
      

  2.   

    你的打印机没连接好,导致设置dmOrientation 出错,设置好就可以了。
      

  3.   


    我是用Microsoft XPS Document Writer来打印的啊~难道Microsoft XPS Document Writer不支持横向打印?
      

  4.   

    在打印之前设置Printer.Orientation就可以了
      

  5.   

    NT的话确实没有测试过,不过2000是可以的.
    话又说回来,你不会是用的NT4.0作打印服务器吧?
    如果服务器硬件性能还可以的话,2000server还是不错的.
    对于2003我没有好感,觉得比较罗嗦,性能也不及2000server
      

  6.   


    我意思是NT内核的系统,哈哈~2000、XP等都算了请问你是怎样实现的?是不是基于上面那段代码加的?如果是请指点一下在哪个位置加?谢谢
      

  7.   

    我的程序会生成一个配置文件,在程序运行的时候自动加载.
    其中可以包括用户的一些偏好选项,一些常用设定,也包括打印设置.
    上面的程序中已经列出了所有打印机和支持的纸张类型.只要把用户选择的打印机名字和纸张记录下来就可以.
    我使用了以下的结构来记录一些我的程序中用到的打印设置相关的信息
    Type PrintSetting
       XO As Single
       YO As Single
       XRatio As Single
       YRatio As Single
       Orientation As Long
       ScaleMode As Long
       FontSize As Long
       PaperSize As Long
       PrintName As String * 100
    End Type
    自定义数据结构是可以直接读写到文件的,这个我就跳过.
    每次程序加载后把上次程序关闭时保存的信息再读回上述结构中,然后再设定打印机:
    Private Sub InitPrint()
    Dim Ptr As Printer
    For Each Ptr In Printers
       If Ptr.DeviceName = Trim(PrintSettings.PrintName) Then
          Set Printer = Ptr
          Exit For
       End If
    NextPrinter.Orientation = PrintSettings.Orientation
    Printer.ScaleMode = PrintSettings.ScaleMode        '=6 设定单位为毫米
    Printer.PaperSize = PrintSettings.PaperSize
    Printer.FontSize = PrintSettings.FontSize
    End Sub
      

  8.   

    不行哦~一切按默认的...
    请问你有没有试过在XP下试过?你用的2000系统是SP多少?
      

  9.   

    我的程序并不改变系统对打印机的任何默认设置,我只是把程序的打印偏好用自己的配置文件保存起来,下一次启动的时候自动加载而已.
    2000 server和XP下都可以通过.
      

  10.   

    不好意思,那晚有点事一过忘记了这里...to LS的:那就奇怪了,为什么那么多人都为XP系统下的打印问题头痛,而且微软还特意出了篇相关打印的解决方案代码~...请问LS的VB6是SP多少?