Skip to content

TPrintDialogDemo 项目分析

项目信息

文件结构

TPrintDialogDemo/
├── 1.bmp
├── Extractor_Icon.ico
├── Project1.py
├── Project1.xml
├── Unit1.py
├── Unit1.pydfm
├── Unit1.sct
├── Unit1.sfm

主程序文件: Project1.py

from glcl import *
from Unit1 import *

def main():
    Application.Initialize()
    Application.Title = 'Project1'
    Application.MainFormOnTaskbar = True
    Application.Icon.LoadFromFile(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Extractor_Icon.ico"))
    MainForm = Form1(Application)
    MainForm.Show()
    FreeConsole()
    Application.Run()
    MainForm.Free()

if __name__ == '__main__':
    main()

单元文件: Unit1.py

import os
from glcl import *

class Form1(Form):

    def __init__(self, owner):
        self.Button1 = Button(self)
        self.FontDialog1 = FontDialog(self)
        self.PrintDialog1 = PrintDialog(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.Button1.OnClick = self.Button1Click

        # 创建一个按钮用于打开打印对话框
        self.print_button = Button(self)
        self.print_button.Parent = self     # 设置父控件为窗体
        self.print_button.Caption = "Print"  # 设置按钮文本
        self.print_button.SetBounds(150, 80, 100, 30)  # 设置按钮的位置和大小

        # 为按钮设置点击事件处理程序
        self.print_button.OnClick = self.open_print_dialog

    def open_print_dialog(self, Sender):
        # 创建打印对话框
        print_dialog = PrintDialog(self)

        # 显示打印对话框并检查用户是否点击了“确定”
        if print_dialog.Execute():
            # 用户选择了打印机并点击“确定”,可以在这里执行打印操作
            # 例如,打印一条消息
            Application.PrintText("Print job started with selected settings.", 10, 10, self.FontDialog1.Font)

            # 这里可以添加更多的打印逻辑,例如:
            # 使用指定的打印机和设置来发送实际的文档进行打印

    def Button1Click(self, Sender):
        # Bitmap构造函数的两个参数,宽和高,可以写0,因为是从文件加载
        bitmap = Bitmap(0, 0)
        # 目前打印的图像格式只能是bmp
        bitmap.LoadFromFile('1.bmp')
        # 第4个参数 缩放比例 从1到100,1打印的图像最大,100打印的图像最小
        Application.PrintImage(bitmap, 50, 50, 1)

设计文件: Unit1.sct

def Button1Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 175
  ClientWidth = 300
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object Button1: TButton
    Left = 16
    Top = 136
    Width = 75
    Height = 25
    Caption = #25171#21360#22270#20687
    TabOrder = 0
    OnClick = Button1Click
  end
  object PrintDialog1: TPrintDialog
    Left = 16
    Top = 88
  end
  object FontDialog1: TFontDialog
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -15
    Font.Name = 'Segoe UI'
    Font.Style = []
    Left = 16
    Top = 56
  end
end

其他文件

  • 1.bmp
  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

对话框组件演示

使用的组件

  • Form1 (TForm)
  • Button1 (TButton)
  • PrintDialog1 (TPrintDialog)
  • FontDialog1 (TFontDialog)

技术特点

  • 包含4个GUI组件
  • 定义2个事件处理

窗体属性

  • caption: #25171#21360#22270#20687
  • height: 175
  • width: 300

代码分析

导入的模块: - import os - from glcl import *

定义的类: - Form1

定义的方法: - init - open_print_dialog - Button1Click

事件绑定: - self.Button1.OnClick = self.Button1Click - self.print_button.OnClick = self.open_print_dialog