如何让edit框中的文字(文字稍多时)自动缩小字体以填满整个编辑框 请指教

解决方案 »

  1.   

    var
      cvs : TCanvas;
      h : HWND;
    begin
      cvs := TCanvas.Create;
      cvs.Handle := GetDeviceContext(h);
      cvs.Font.Assign(edt.Font);
      edt.ClientWidth := cvs.TextWidth(edt.Text)+2; //边框加两个像素
      cvs.Free;
    end;
      

  2.   

    理论上说,就是根据你edit的宽度去/字数,得到每个字的大小。但这个确实不好算。因为字号跟宽度我不知道怎么换算。
      

  3.   

    简单说个算法:可以创建个memo,让memo的初始字体大小、宽度和edit一样,将edit的内容填入memo中,然后判断memo的行数超过一行没,超过则将字体减小一号继续判断。最终将memo的字体大小返还给edit
      

  4.   


    var
      cvs   :   TCanvas;
      h   :   HWND;
      oldTextWidth,NewTextWidth,NewFontHeigh : Integer;
      lf : TLogFont;
      tf : TFont;
    begin
      //某些字体如果不支持太大或太小时
      //设置可能无效  //用TextWidth得到当前字体下需要多少像素
      cvs   :=   TCanvas.Create;
      cvs.Handle := GetDeviceContext(h);
      cvs.Font.Assign(edt.Font);
      oldTextWidth := cvs.TextWidth(edt.Text);
      CloseHandle(cvs.Handle);
      cvs.Free;  //将ClientWidth作为新字体时同样字数需要的像素
      NewTextWidth := edt.ClientWidth-2;  //计算新字体的字体高
      NewFontHeigh := trunc(edt.Font.Height/oldTextWidth*NewTextWidth);  //创建新的逻辑字体
      tf := TFont.Create;
      tf.Assign(edt.Font);
      GetObject(tf.Handle, sizeof(lf), @lf);
      lf.lfHeight := NewFontHeigh;
      tf.Handle := CreateFontIndirect(lf);  //编辑框使用新字体
      edt.Font.Assign(tf);  tf.Free;
    end;
      

  5.   

    最终使用  清风拂面师兄 思路 做如下代码:
                    Memo1.Width   :=edit1.Width; 
                    Memo1.Font   :=edit1.Font; 
                    Memo1.text:=   (c   +   '-'   +   name2);                 While   Memo1.Lines.Count> 1   do 
                      begin 
                        memo1.Font.size   :=   memo1.Font.size-1; 
                      end;                 edit1.Font.size   :=   memo1.Font.size; 
                    edit1.Text   :=   Memo1.text; 但获取的字号小了一号,可能是字数刚好凑到那里了吧