全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2736
推到 Plurk!
推到 Facebook!

combobox中下拉後的內容不夠寬

答題得分者是:hagar
computer20002
一般會員


發表:8
回覆:16
積分:4
註冊:2002-11-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-01-06 11:12:55 IP:61.221.xxx.xxx 未訂閱
請教各位先進 combobox下拉的寬度不夠寬 因為內容資料比較長,有沒有什麼函數或指令可以讓它變寬 請各位幫幫忙,謝謝!
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-01-06 11:29:19 IP:147.8.xxx.xxx 未訂閱
引言:請教各位先進 combobox下拉的寬度不夠寬 因為內容資料比較長,有沒有什麼函數或指令可以讓它變寬 請各位幫幫忙,謝謝!
set DropDownCount for number of items in the drop down. For 寬, I think the standard combobox dropdown width is same as the width of the combobox itself.
computer20002
一般會員


發表:8
回覆:16
積分:4
註冊:2002-11-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-01-06 11:53:07 IP:61.221.xxx.xxx 未訂閱
因為內容的文字過長,不是要items 如 combobox.with=100 但是 Length(combobox.items.strings[0])>combobox.with 在下拉時 過長的內容就會被截掉 又因為combobox元件的width=下拉時list的width 不知道這個問題有沒有解 不好意思我沒表達好我的問題
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-01-06 12:10:58 IP:147.8.xxx.xxx 未訂閱
computer20002
一般會員


發表:8
回覆:16
積分:4
註冊:2002-11-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-01-06 14:41:45 IP:61.221.xxx.xxx 未訂閱
謝謝您 william!
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-01-18 07:43:24 IP:202.39.xxx.xxx 未訂閱
參考:
procedure SetComboDropDownWidth(ComboBox: TComboBox; Width: Longint = -1); 
var 
  I, TextLen: Longint; 
begin 
  if Width < ComboBox.Width then 
  begin 
    ComboBox.Canvas.Handle := GetDC(ComboBox.Handle); 
    try 
      for I := 0 to ComboBox.Items.Count -1 do 
      begin 
        TextLen := ComboBox.Canvas.TextWidth(ComboBox.Items[I]); 
        if TextLen > Width then 
          Width := TextLen; 
      end;           (* Standard ComboBox drawing is Rect.Left   2, 
         adding the extra spacing offsets this *) 
      Inc(Width, 10); 
    finally 
      ReleaseDC(ComboBox.Handle, ComboBox.Canvas.Handle); 
    end; 
  end; 
  SendMessage(ComboBox.Handle, CB_SETDROPPEDWIDTH, Width, 0); 
end; 
-- 將進酒 杯莫停 與君歌一曲 請君為我傾耳聽
computer20002
一般會員


發表:8
回覆:16
積分:4
註冊:2002-11-12

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-01-20 09:17:35 IP:61.221.xxx.xxx 未訂閱
hagar先進: 謝謝!
tonylong
一般會員


發表:6
回覆:3
積分:1
註冊:2002-11-26

發送簡訊給我
#8 引用回覆 回覆 發表時間:2007-04-30 15:36:54 IP:59.116.xxx.xxx 訂閱

===================引 用 hagar 文 章===================
參考:
 procedure SetComboDropDownWidth(ComboBox: TComboBox; Width: Longint = -1);  var    I, TextLen: Longint;  begin    if Width < ComboBox.Width then    begin      ComboBox.Canvas.Handle := GetDC(ComboBox.Handle);      try        for I := 0 to ComboBox.Items.Count -1 do        begin          TextLen := ComboBox.Canvas.TextWidth(ComboBox.Items[I]);          if TextLen > Width then            Width := TextLen;        end;         (* Standard ComboBox drawing is Rect.Left   2,           adding the extra spacing offsets this *)        Inc(Width, 10);      finally        ReleaseDC(ComboBox.Handle, ComboBox.Canvas.Handle);      end;    end;    SendMessage(ComboBox.Handle, CB_SETDROPPEDWIDTH, Width, 0);  end;  
-- 將進酒 杯莫停 與君歌一曲 請君為我傾耳聽


hagar版主的程式碼,我在使用時常會發生無效的形況,以下是我在網路上搜尋得的程式碼,目前使用上沒有問題,借花獻佛供大家參考。

delphi code

procedure SetComboDropDownWidth(ComboBox: TComboBox; Width: Integer = -1);
var
I, TextLen: Longint;
lf: LOGFONT;
f: HFONT;
begin
if Width < ComboBox.Width then begin
FillChar(lf,SizeOf(lf),0);
StrPCopy(lf.lfFaceName, ComboBox.Font.Name);
lf.lfHeight := ComboBox.Font.Height;
lf.lfWeight := FW_NORMAL;
if fsBold in ComboBox.Font.Style then
lf.lfWeight := lf.lfWeight or FW_BOLD;

f := CreateFontIndirect(lf);
if (f <> 0) then
begin
try
ComboBox.Canvas.Handle := GetDC(ComboBox.Handle);
SelectObject(ComboBox.Canvas.Handle,f);
try
for I := 0 to ComboBox.Items.Count -1 do begin
TextLen := ComboBox.Canvas.TextWidth(ComboBox.Items[I]);
if TextLen > Width then
Width := TextLen;
end;
(* Standard ComboBox drawing is Rect.Left 2,
adding the extra spacing offsets this *)
Inc(Width, GetSystemMetrics(SM_CYVTHUMB)
GetSystemMetrics(SM_CXVSCROLL));
finally
ReleaseDC(ComboBox.Handle, ComboBox.Canvas.Handle);
end;
finally
DeleteObject(f);
end;
end;
end;
SendMessage(ComboBox.Handle, CB_SETDROPPEDWIDTH, Width, 0);
end;

bcb code

void SetComboDropDownWidth(TComboBox *ComboBox,int Width)
{
int I,TextLen;
LOGFONT lf;
HFONT f;
if(Width < ComboBox->Width)
{
memset(&lf,0,sizeof(lf));
strcpy(lf.lfFaceName, ComboBox->Font->Name.c_str());
lf.lfHeight = ComboBox->Font->Height;
lf.lfWeight = FW_NORMAL;
if (ComboBox->Font->Style.Contains(fsBold))
lf.lfWeight = lf.lfWeight | FW_BOLD;

f = CreateFontIndirect(&lf);
if (f != NULL)
{
try
{
ComboBox->Canvas->Handle = GetDC(ComboBox->Handle);
SelectObject(ComboBox->Canvas->Handle,f);
try
{
for(I=0;IItems->Count;I )
{
TextLen = ComboBox->Canvas->TextWidth(ComboBox->Items->Strings[I]);
if(TextLen > Width)
Width = TextLen;
}
// Standard ComboBox drawing is Rect.Left 2,adding the extra spacing offsets this
Width = GetSystemMetrics(SM_CYVTHUMB) GetSystemMetrics(SM_CXVSCROLL);
}
__finally
{
ReleaseDC(ComboBox->Handle, ComboBox->Canvas->Handle);
}
}
__finally
{
DeleteObject(f);
}
}
}
SendMessage(ComboBox->Handle, CB_SETDROPPEDWIDTH, Width, 0);
}
系統時間:2024-05-07 14:42:30
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!