Skip to content

THeaderControlDemo 项目分析

项目信息

文件结构

THeaderControlDemo/
├── 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.HeaderControl1 = HeaderControl(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.Button1.OnClick = self.Button1Click

        # 创建 THeaderControl 控件用于显示标题
        self.headerControl = HeaderControl(self)
        self.headerControl.SetBounds(10, 10, 360, 30)  # 设置控件的位置和大小
        self.headerControl.Parent = self  # 将控件设置为窗体的子控件

        # 初始化列
        self.init_columns()

        # 创建按钮用于添加新列
        self.btnAddColumn = Button(self)
        self.btnAddColumn.SetBounds(10, 50, 150, 30)  # 设置按钮的位置和大小
        self.btnAddColumn.Caption = "添加列"  # 设置按钮文本
        self.btnAddColumn.OnClick = self.add_column  # 绑定点击事件
        self.btnAddColumn.Parent = self  # 将按钮设置为窗体的子控件

    # 初始化列的方法
    def init_columns(self):
        # 创建并添加第一个标题
        header_item1 = self.headerControl.Sections.Add()  # 添加新列
        header_item1.Text = "列 1"  # 设置列的标题
        header_item1.Width = 100  # 设置列的宽度

        # 创建并添加第二个标题
        header_item2 = self.headerControl.Sections.Add()  # 添加新列
        header_item2.Text = "列 2"  # 设置列的标题
        header_item2.Width = 100  # 设置列的宽度

        # 创建并添加第三个标题
        header_item3 = self.headerControl.Sections.Add()  # 添加新列
        header_item3.Text = "列 3"  # 设置列的标题
        header_item3.Width = 100  # 设置列的宽度

    # 添加新列的方法
    def add_column(self, Sender):
        # 获取当前列数量
        column_count = self.headerControl.Sections.Count
        # 创建新列
        new_column = self.headerControl.Sections.Add()
        new_column.Text = f"列 {column_count + 1}"  # 设置

    def Button1Click(self, Sender):
        # 获取当前列数量
        column_count = self.HeaderControl1.Sections.Count
        # 创建新列
        new_column = self.HeaderControl1.Sections.Add()
        new_column.Text = f"列 {column_count + 1}"  # 设置

设计文件: Unit1.sct

def Button1Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 205
  ClientWidth = 382
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object HeaderControl1: THeaderControl
    Left = 0
    Top = 172
    Width = 382
    Height = 33
    Align = alBottom
    Sections = <
      item
        ImageIndex = -1
        Text = #21015'1'
        Width = 50
      end
      item
        ImageIndex = -1
        Text = #21015'2'
        Width = 50
      end
      item
        ImageIndex = -1
        Text = #21015'3'
        Width = 50
      end
      item
        ImageIndex = -1
        Text = #21015'4'
        Width = 50
      end>
  end
  object Button1: TButton
    Left = 296
    Top = 136
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

  • Form1 (TForm)
  • HeaderControl1 (THeaderControl)
  • Button1 (TButton)

技术特点

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

窗体属性

  • caption: Button1
  • height: 205
  • width: 382

代码分析

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

定义的类: - Form1

定义的方法: - init - init_columns - add_column - Button1Click

事件绑定: - self.Button1.OnClick = self.Button1Click - self.btnAddColumn.OnClick = self.add_column # 绑定点击事件