나는 사용자 정의한 패널이 있습니다. 텍스트를 표시하는 데 사용합니다. 하지만 때로는 텍스트가 너무 길어서 다음 줄로 넘어갑니다. 모든 텍스트를 표시하도록 패널의 크기를 자동으로 조정할 수있는 방법이 있습니까?패널에서 텍스트의 높이 찾기
저는 C#과 Visual Studio 2008 및 컴팩트 프레임 워크를 사용하고 있습니다.
(참고 :.. HintBox 패널에서 상속 내 자신의 클래스입니다 그래서 필요에 수정할 수 있습니다)
public void DataItemClicked(ShipmentData shipmentData)
{
// Setup the HintBox
if (_dataItemHintBox == null)
_dataItemHintBox = HintBox.GetHintBox(ShipmentForm.AsAnObjectThatCanOwn(),
_dataShipSelectedPoint,
new Size(135, 50), shipmentData.LongDesc,
Color.LightSteelBlue);
_dataItemHintBox.Location = new Point(_dataShipSelectedPoint.X - 100,
_dataShipSelectedPoint.Y - 50);
_dataItemHintBox.MessageText = shipmentData.LongDesc;
// It would be nice to set the size right here
_dataItemHintBox.Size = _dataItemHintBox.MethodToResizeTheHeightToShowTheWholeString()
_dataItemHintBox.Show();
}
여기
내가의 크기를 조정 걸려 코드입니다
그의 코드 예제가 필자가 필요로하는 것과 가장 비슷하기 때문에 (그 코드가 작동하는 것처럼 보입니다) Will Marcouiller에게 답을 알려 드리겠습니다. 그러나 이것은 내가 사용할 것으로 생각됩니다.
public static class CFMeasureString
{
private struct Rect
{
public readonly int Left, Top, Right, Bottom;
public Rect(Rectangle r)
{
this.Left = r.Left;
this.Top = r.Top;
this.Bottom = r.Bottom;
this.Right = r.Right;
}
}
[DllImport("coredll.dll")]
static extern int DrawText(IntPtr hdc, string lpStr, int nCount,
ref Rect lpRect, int wFormat);
private const int DT_CALCRECT = 0x00000400;
private const int DT_WORDBREAK = 0x00000010;
private const int DT_EDITCONTROL = 0x00002000;
static public Size MeasureString(this Graphics gr, string text, Rectangle rect,
bool textboxControl)
{
Rect bounds = new Rect(rect);
IntPtr hdc = gr.GetHdc();
int flags = DT_CALCRECT | DT_WORDBREAK;
if (textboxControl) flags |= DT_EDITCONTROL;
DrawText(hdc, text, text.Length, ref bounds, flags);
gr.ReleaseHdc(hdc);
return new Size(bounds.Right - bounds.Left, bounds.Bottom - bounds.Top +
(textboxControl ? 6 : 0));
}
}
텍스트를 그리기 위해 os 수준 호출을 사용합니다. P-Invoking을 사용하면 필요한 기능을 얻을 수 있습니다 (다중 줄 바꿈). 이 메서드는 여백을 포함하지 않습니다. 텍스트가 차지하는 실제 공간.
이 코드를 작성하지 않았습니다. 나는 http://www.mobilepractices.com/2007/12/multi-line-graphicsmeasurestring.html에서 그것을 얻었다. 그 블로그 포스트는 나의 정확한 문제와이 결함을 가지고 있었다. (비록 내가 그것을 확장 메소드로 만들기 위해 약간의 비틀기를 만들었지 만)
+1 Compact Framework 질문은 전체 프레임 워크 자체와 다른 해결책을 가져올 수 있습니다. –