码农的笔记

Delphi虽好,但已不流行; 博客真好,可以做笔记

博客园 首页 新随笔 联系 订阅 管理
  117 随笔 :: 0 文章 :: 3 评论 :: 61603 阅读

开发环境DelphiXe11;

这个代码不适用于Delphi7,一般不适用于Delphi2009之前的版本;

这个图片:12个字(汉字)换行;2数字和2字母认为等于1汉字;

这个换行不大行,全是小写的字母占占用的位置比较少,如果要效果好的,请自己写;

 

 

--Unit-- 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
unit Unit1;
 
interface
 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Math;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
function f_GetLineBreakPosition(const AStr: string;
  ACutoffCount: DWord): Integer;
var
  vStrlen, vCount_A, vCount_B, i, j, vChineseCharacterCount, vSubInt, vSubInt2,
  vTempInt_A, vTempInt_B: Integer;
  vDouble: Double;
  vChineseCharacterBool: Boolean;
begin
  //只是粗略计算,不要太较真
  Result := 0;
  if ACutoffCount < 4 then //最少四个字
    Exit;
  if AStr = '' then
    Exit;
 
  vStrlen := Length(AStr);
 
  if vStrlen <= ACutoffCount then //ACutoffCount截断的汉字位置  只能显示两行,第一行多少个汉字截断(两个数字或者连个数字或者字符默认为一个汉字)
    Exit;
  vCount_A := 0;
  vCount_B := 0;
  for i := 1 to vStrlen do
  begin
    if Ord(Astr[i]) < 256 then
      inc(vCount_A) //字符个数
    else
      inc(vCount_B); //汉字个数
 
 
    j := i + 1;
    if j <= vStrlen then
    begin
      if Ord(Astr[j]) < 256 then //小于256都认为非汉字
        vChineseCharacterBool := False
      else
        vChineseCharacterBool := True;
      vTempInt_B := vCount_B * 2;
      vTempInt_A := vCount_A;
      if vChineseCharacterBool then//下一个是汉字
      begin
        vTempInt_B := (vCount_B + 1) * 2;
      end
      else
      begin
        vTempInt_A := vCount_A + 1;
      end;
 
      vDouble := (vTempInt_A + vTempInt_B) / 2.0;
      vSubInt := Trunc(vDouble);
      vSubInt2 := Ceil(vDouble);
      if vSubInt = vSubInt2 then //没有小数
      begin
        if vSubInt = ACutoffCount then
        begin
          Result := j;
          Exit;
        end;
      end
      else
      begin
        if vChineseCharacterBool  then
        begin
          if (vSubInt <= ACutoffCount) and (ACutoffCount < vSubInt2) then
          begin
            Result := j;
            Exit;
          end;
        end
        else
        begin
          if ACutoffCount = vSubInt then
          begin
            Result := j;
            Exit;
          end;
        end;
      end;
    end
    else
      Result := i;
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  vStr1, vStr2: string;
begin
  Memo1.Clear;
  i :=f_GetLineBreakPosition(Edit1.Text, StrToIntDef(Edit2.Text, 10));
  if i = 0 then
    Memo1.Text := Edit1.Text
  else
  begin
    vStr1 := Copy(Edit1.Text,1, i);
    vStr2 := Copy(Edit1.Text,i + 1, 10000);
    Memo1.Lines.Add(vstr1);
    Memo1.Lines.Add(vstr2);
  end;
end;
 
end.

 

--Form-- 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 389
  ClientWidth = 449
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 15
  object Button1: TButton
    Left = 151
    Top = 37
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 16
    Top = 8
    Width = 337
    Height = 23
    TabOrder = 1
    Text = #25216#26415#30340#22797#27963#33410#22307#35806#33410#24674'0123456789'
  end
  object Memo1: TMemo
    Left = 16
    Top = 80
    Width = 401
    Height = 281
    Lines.Strings = (
      'Memo1')
    ScrollBars = ssBoth
    TabOrder = 2
  end
  object Edit2: TEdit
    Left = 16
    Top = 37
    Width = 57
    Height = 23
    TabOrder = 3
    Text = '12'
  end
end

  

posted on   码农的笔记  阅读(77)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示