各位师兄 小弟不知道如何让edit框中的文字(文字稍多时)自动缩小字体以填满整个编辑框 请指教
如何让edit框中的文字(文字稍多时)自动缩小字体以填满整个编辑框 请指教
[解决办法]
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;
[解决办法]
理论上说,就是根据你edit的宽度去/字数,得到每个字的大小。但这个确实不好算。因为字号跟宽度我不知道怎么换算。
[解决办法]
简单说个算法:可以创建个memo,让memo的初始字体大小、宽度和edit一样,将edit的内容填入memo中,然后判断memo的行数超过一行没,超过则将字体减小一号继续判断。最终将memo的字体大小返还给edit
[解决办法]
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;