Go Back   TX Text Control Community > TX Text Control Discussion and Support Forums > TX Text Control .NET

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.

Reply
 
Thread Tools Display Modes
  #1  
Old November 14, 2008
xyzabc1010 xyzabc1010 is offline
Registered User
 
Join Date: Jul 2007
Posts: 5
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);
}
Reply With Quote
  #2  
Old November 19, 2008
ealsmyr ealsmyr is offline
Registered User
 
Join Date: Aug 2007
Location: Stockholm, Sweden
Age: 33
Posts: 44
Send a message via MSN to ealsmyr Send a message via Skype™ to ealsmyr
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);
        }
    }
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



All times are GMT +1. The time now is 20:31:39.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
© 1991 - 2010 The Imaging Source Europe GmbH