Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetCharacterPlacement (gdi32)
 
.
Summary

C# Signature:

    [DllImport("gdi32.dll", EntryPoint="GetCharacterPlacementW")]
    static extern uint GetCharacterPlacementW(IntPtr hdc, [MarshalAs(UnmanagedType.LPWStr)] string lpString,
       int nCount, int nMaxExtent, ref GCP_RESULTS lpResults, uint dwFlags);

User-Defined Types:

    [StructLayout(LayoutKind.Sequential)]
    public struct GCP_RESULTS
    {
        public int StructSize;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string OutString;
        public IntPtr Order;
        public IntPtr Dx;
        public IntPtr CaretPos;
        public IntPtr Class;
        public IntPtr Glyphs;
        public int GlyphCount;
        public int MaxFit;
        public uint lStructSize;
        public IntPtr lpOutString;
        public IntPtr lpOrder;
        public IntPtr lpDx;
        public IntPtr lpCaretPos;
        public char* lpClass;
        public IntPtr lpGlyphs;
        public uint nGlyphs;
        public int nMaxFit;
    }

    [Flags]
    public enum GCPFlags : uint
    {
        GCP_DBCS = 0x0001,
        GCP_REORDER = 0x0002,
        GCP_USEKERNING = 0x0008,
        GCP_GLYPHSHAPE = 0x0010,
        GCP_LIGATE = 0x0020,
        GCP_DIACRITIC = 0x0100,
        GCP_KASHIDA = 0x0400,
        GCP_ERROR = 0x8000,
        GCP_JUSTIFY = 0x00010000,
        GCP_CLASSIN = 0x00080000,
        GCP_MAXEXTENT = 0x00100000,
        GCP_JUSTIFYIN = 0x00200000,
        GCP_DISPLAYZWG = 0x00400000,
        GCP_SYMSWAPOFF = 0x00800000,
        GCP_NUMERICOVERRIDE = 0x01000000,
        GCP_NEUTRALOVERRIDE = 0x02000000,
        GCP_NUMERICSLATIN = 0x04000000,
        GCP_NUMERICSLOCAL = 0x08000000,
    }

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    public int GetCaretPostion(string str, int index)
    {
        IntPtr OldFont = SelectObject(this.DC, this.Font);

Please add some!

        int len = str.Length;
        int[] order = new int[len];
        int[] dx = new int[len];
        int[] caret = new int[len];
        byte[] clss = new byte[len];
        short[] glys = new short[len];

Alternative Managed API:

Do you know one? Please contribute it!

        GCHandle ordHnd = GCHandle.Alloc(order, GCHandleType.Pinned);
        GCHandle dxHnd = GCHandle.Alloc(dx, GCHandleType.Pinned);
        GCHandle carHnd = GCHandle.Alloc(caret, GCHandleType.Pinned);
        GCHandle clsHnd = GCHandle.Alloc(clss, GCHandleType.Pinned);
        GCHandle glyHnd = GCHandle.Alloc(glys, GCHandleType.Pinned);

Documentation

        try
        {
        GCP_RESULTS rs = new GCP_RESULTS();
        rs.StructSize = Marshal.SizeOf(typeof(GCP_RESULTS));

        rs.OutString = new String('\0', len + 2);
        rs.Order = ordHnd.AddrOfPinnedObject();
        rs.Dx = dxHnd.AddrOfPinnedObject();
        rs.CaretPos = carHnd.AddrOfPinnedObject();
        rs.Class = clsHnd.AddrOfPinnedObject();
        rs.Glyphs = glyHnd.AddrOfPinnedObject();

        rs.GlyphCount = len;
        rs.MaxFit = 0;

        GCPFlags flg = GCPFlags.GCP_GLYPHSHAPE;
        uint r = GetCharacterPlacement(this.DC, str, len, 0, ref rs, (uint)flg);
        if (r == 0)
            throw new Exception(string.Format("GeCharacterPlacement Error {0}", Marshal.GetLastWin32Error()));
        }
        finally
        {
        ordHnd.Free();
        dxHnd.Free();
        carHnd.Free();
        clsHnd.Free();
        glyHnd.Free();
        }

        SelectObject(this.DC, OldFont);

        return caret[index];
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions