|
|||||||
| TX Text Control .NET Please use this forum for TX Text Control .NET support only. If you need support for a different product, please use the appropriate forum. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Autosize & Twips --> Pixel question
Hi All,
I have a simple question. I have a textcontrol contained within a panel but would like to have an autogrow behavior. Therefore, I caught the Changed event on the textcontrol. Calculate the height of the text via TextBounds, convert them to pixels and grow the panel as necessary. However, this does not grow the box to the correct size. I suspect it might be my conversion from twips to pixels....which I got from the msdn site. Can someone give me a suggestion? Thanks The code is the following: --------------- private void _editor_Changed(object sender, EventArgs e) { if ((Editor.Text.Length == 0) || (Editor.Lines.Count < 1)) return; TXTextControl.Line l = Editor.Lines[Editor.Lines.Count]; TXTextControl.Line lh = Editor.Lines[Editor.InputPosition.Line]; if (l == null) return; int h = ImageUtils.TwipsToPixels(l.TextBounds.Bottom, false); int dh = ImageUtils.TwipsToPixels(lh.TextBounds.Height, false); if (h >= (Editor.Height - Editor.Margin.Top - Editor.Margin.Bottom - 1)) this.Size = new Size(this.Width, this.Height + dh); } -------- private static uint WU_LOGPIXELSX = 88; private static uint WU_LOGPIXELSY = 90; private static uint TwipsPerInch = 1440; public static int TwipsToPixels(int twips, bool isHorizontal) { int pixelPerInch; System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(IntPtr.Zero); IntPtr dc = g.GetHdc(); uint intDC = (uint)dc.ToInt32(); if (isHorizontal) pixelPerInch = (int)GetDeviceCaps(intDC, WU_LOGPIXELSX); else pixelPerInch = (int)GetDeviceCaps(intDC, WU_LOGPIXELSY); g.ReleaseHdc(dc); return (int)Math.Ceiling((double)twips * pixelPerInch / TwipsPerInch); } |
|
#2
|
|||
|
|||
|
Re: Autosize & Twips --> Pixel question
We have a similar behaviour and have solved it like this.
We subclass the TX Textcontrol into a new class. This class publishes a OnContentsResized event with a parameter object containint the new size of the contents. Consumers of this class (like your panel) can then adapt to size changes. We allways have the same width of the textboxes to the event is triggered only on a change in height. The subclass keeps a private field with the y-position of the last object in the Textcontrol. On every change, we compare with the new position and if it differs the event is thrown. Twips conversion in our solution is done using the class below which is pretty much the same as yours: Code:
class TwipsConverter
{
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
private static extern int GetDeviceCaps(IntPtr hDc, int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
private const int WU_LOGPIXELSX = 88;
private const int WU_LOGPIXELSY = 90;
/// <summary>
/// Converts twipts to pixels. Twip is twentieth of a point
/// </summary>
/// <param name="iTwips"></param>
/// <param name="bHorizontal"></param>
/// <returns></returns>
public static int ConvertTwipsToPixels(int iTwips, bool bHorizontal)
{
IntPtr hDC;
int iPixelsPerInch;
int iTwipsPerInch = 1440;
hDC = GetDC(IntPtr.Zero);
if (bHorizontal)
iPixelsPerInch = GetDeviceCaps(hDC, WU_LOGPIXELSX);
else
iPixelsPerInch = GetDeviceCaps(hDC, WU_LOGPIXELSY);
hDC = ReleaseDC(IntPtr.Zero, hDC);
return (int)(((float)iTwips / (float)iTwipsPerInch) * (float)iPixelsPerInch);
}
}
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|