combobox中下拉後的內容不夠寬 |
答題得分者是:hagar
|
computer20002
一般會員 發表:8 回覆:16 積分:4 註冊:2002-11-12 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
computer20002
一般會員 發表:8 回覆:16 積分:4 註冊:2002-11-12 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Try the hint? e.g.
http://www.vclxx.org/DELPHI/D32FREE/SKCMBHNT.ZIP
|
computer20002
一般會員 發表:8 回覆:16 積分:4 註冊:2002-11-12 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
參考:
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 發送簡訊給我 |
|
tonylong
一般會員 發表:6 回覆:3 積分:1 註冊:2002-11-26 發送簡訊給我 |
===================引 用 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;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); } |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |