Announcement

Collapse
No announcement yet.

Anzahl der Zeichen im sichtbaren Bereich einer TextBox ermitteln

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Anzahl der Zeichen im sichtbaren Bereich einer TextBox ermitteln

    Hallo,<br>

    ich möchte eine Komponente programmieren. Dafür muß ich wissen, wieviel <br>
    Zeichen in den sichtbaren Bereich einer Textbox passen.<br>
    ich verwende die Funktion "GetTextExtentExPoint".<br>
    Sie gibt aber immer nur "false" zurück.<br>
    GetLastError bringt die Fehlernummer 126 = Modul nicht gefunden.<br>
    Was mache ich falsch ? <br>
    <br><br>

    <PRE>
    [DllImport("gdi32.dll" ,CharSet = CharSet.Auto )]
    public extern static long GetTextExtentExPoint
    (

    IntPtr hdc, // handle to DC
    [MarshalAs (UnmanagedType.LPTStr)]
    string lpszStr, // character string
    int cchString, // number of characters
    int nMaxExtent, // maximum width of formatted string
    ref IntPtr lpnFit, // maximum number of characters
    ref IntPtr alpDx, // array of partial string widths
    ref IntPtr lpSize // string dimensions
    );
    [DllImport("Kernel32.dll")]
    public extern static uint GetLastError();

    public struct strSize
    {
    public long lx;
    public long ly;
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    int nFit = 0;
    IntPtr lpnFit = Marshal.AllocHGlobal(Marshal.SizeOf(nFit));

    strSize mySize;
    mySize.lx = 0;
    mySize.ly = 0;

    IntPtr lpSize = Marshal.AllocHGlobal(Marshal.SizeOf(mySize));

    IntPtr alpDx = IntPtr.Zero;

    textBox1.Text = "aaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbb";

    Graphics g = textBox1.CreateGraphics();
    IntPtr hdc = g.GetHdc();

    long nResult = GetTextExtentExPoint(hdc, textBox1.Text,textBox1.Text.Length,
    80,ref lpnFit,ref alpDx,ref lpSize );
    uint nTest = GetLastError();

    }
    </PRE>

  • #2
    allo,<br><br>
    nach einiger Forschungsarbeit bin ich selbst auf die richtige <br>
    Lösung gekommen. Einige Übergabeparameter für GetTextExtentExPoint <br>
    hatten den falschen Datentyp. Gar nicht so einfach, immer daran <br>
    zu denken, daß jeder Datentyp unter C# ein Objekt ist und damit <br>
    quasi als Zeiger übergeben werden kann <br><br>

    <PRE>

    [DllImport("gdi32.dll" ,CharSet = CharSet.Unicode )]
    public extern static uint GetTextExtentExPoint
    (
    IntPtr hdc, // handle to DC
    [MarshalAs (UnmanagedType.LPTStr)]
    string lpszStr, // character string
    int cchString, // number of characters
    int nMaxExtent, // maximum width of formatted string
    ref int lpnFit, // maximum number of characters
    IntPtr alpDx, // array of partial string widths
    ref stSize sSize
    );



    [StructLayout(LayoutKind.Sequential)]
    public struct stSize
    {
    public int lx;
    public int ly;
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    //damit ein Verweistyp daraus wird;
    int lpnFit = new int();
    IntPtr alpDx = IntPtr.Zero;

    stSize mySize;
    mySize.lx = 0;
    mySize.ly = 0;

    textBox1.Text = "ABCDEFGHIJKLMNOPQRSTUVW";

    Graphics g = textBox1.CreateGraphics();
    IntPtr DC = new IntPtr();
    DC = g.GetHdc();

    uint nResult = GetTextExtentExPoint(DC, textBox1.Text, textBox1.Text.Length,
    80,ref lpnFit,alpDx, ref mySize );
    int n1 = mySize.lx ;
    int n2 = mySize.ly ;
    g.ReleaseHdc(DC);

    }
    </PRE&gt

    Comment

    Working...
    X