none
调用GDI textout的问题 RRS feed

  • 问题

  • 调用GDI显示竖排文字的问题,因为字体是OpenType而GDI+好像不能很好的支持这种字体所以用GDI

    谢谢大家!

                IntPtr hdcG = e.Graphics.GetHdc();
                LOGFONT logFont = new LOGFONT();
                logFont.lfHeight = 20;
                logFont.lfWidth = 20;
                logFont.lfEscapement =900;
                logFont.lfOrientation = 900;
                logFont.lfWeight = 20;
                logFont.lfItalic = 0;
                logFont.lfUnderline = 0;
                logFont.lfStrikeOut = 0;
                logFont.lfFaceName = "Mongolian Writing";
                logFont.lfCharSet = 1;
                //logFont.

                Font f = Font.FromLogFont(logFont);

                IntPtr hdcFont = f.ToHfont();

                SelectObject(hdcG, hdcFont);
                string TextV = "I want to print vertical text";
                TextOut(hdcG, 0, 0, TextV, TextV.Length);

    1。 以上代码设置了lfEscapement =900, logFont.lfOrientation = 900可是文字的显示方向为什么不是垂直的,

    2。SelectObject选择字体时一定要LOGFONT->Font->IntPtr这样转换一下吗?不能直接用LOGFONT吗?

    2012年3月12日 20:25

答案

  • 请参见这个例子,如何开发一个竖直显示的Label:

    Extended Vertical Label Control in C# .NET: 
    http://www.codeproject.com/Articles/19774/Extended-Vertical-Label-Control-in-C-NET 

    以下是效果图:


    Neddy Ren[MSFT]
    MSDN Community Support | Feedback to us

    2012年3月14日 8:20

全部回复

  • 请参见这个例子,如何开发一个竖直显示的Label:

    Extended Vertical Label Control in C# .NET: 
    http://www.codeproject.com/Articles/19774/Extended-Vertical-Label-Control-in-C-NET 

    以下是效果图:


    Neddy Ren[MSFT]
    MSDN Community Support | Feedback to us

    2012年3月14日 8:20
  •   谢谢您的回复,

    我看到这个例子最终使用DrawString函数来显示文本,

    而这个就是问题所在,Drawstring不支持OpenType的复杂文本,

    下图为例:这是一段蒙文文本 上面是GDI的输出,下面是GDI+的输出,蒙文是一种复杂文本,字形随字符所处的位置变化

    可以看出GDI的textout支持这种机制,而GDI+的DrawString不支持这种机制,虽然GDI+的坐标变换功能强大,但就是不能用啊,

    所以问题还是:怎么控制GDI的Textout输出的文本的角度啊?

    源代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;


    namespace GDIText
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }


            
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
            public class LOGFONT
            {
                public int lfHeight = 0;
                public int lfWidth = 0;
                public int lfEscapement = 0;
                public int lfOrientation = 0;
                public int lfWeight = 0;
                public byte lfItalic = 0;
                public byte lfUnderline = 0;
                public byte lfStrikeOut = 0;
                public byte lfCharSet = 0;
                public byte lfOutPrecision = 0;
                public byte lfClipPrecision = 0;
                public byte lfQuality = 0;
                public byte lfPitchAndFamily = 0;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
                public string lfFaceName = string.Empty;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct POINT
            {
                public int X;
                public int Y;


                public POINT(int x, int y)
                {
                    this.X = x;
                    this.Y = y;
                }


                public static implicit operator System.Drawing.Point(POINT p)
                {
                    return new System.Drawing.Point(p.X, p.Y);
                }


                public static implicit operator POINT(System.Drawing.Point p)
                {
                    return new POINT(p.X, p.Y);
                }
                public override string ToString()
                {
                    //string s0
                    return "X=" +this.X.ToString() + ",Y=" + this.Y.ToString();
                }
            }
            [DllImport("gdi32.dll")]
            static public extern IntPtr CreateCompatibleDC(IntPtr hDC);
            [DllImport("gdi32.dll", EntryPoint = "BitBlt")]
            public static extern bool BitBlt(IntPtr hdcDest, int nXDest,
             int nYDest, int nWidth, int nHeight, IntPtr hdcSrc,
             int nXSrc, int nYSrc, int dwRop);
            [DllImport("user32.dll", EntryPoint = "GetDC")]
            public static extern IntPtr GetDC(IntPtr hWnd);
            [DllImport("gdi32.dll")]
            static extern bool BeginPath(IntPtr hdc);
            [DllImport("gdi32.dll")]
            static extern bool EndPath(IntPtr hdc);
            [DllImport("gdi32.dll")]
            private static extern IntPtr SetROP2(IntPtr hdc, int fnDrawMode);
            [DllImport("gdi32.dll")]
            private static extern IntPtr SetBkMode(IntPtr hdc, int nBkMode);
            [DllImport("gdi32.dll")]
            static public extern bool DeleteObject(IntPtr hObject);
            [DllImport("gdi32.dll")]
            static public extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
            [DllImport("gdi32.dll")]
            static public extern IntPtr DeleteDC(IntPtr hDC);
            [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
            static extern bool TextOut(IntPtr hdc, int nXStart, int nYStart,
               string lpString, int cbString);
            [DllImport("gdi32.dll")]
            static extern int GetPath(IntPtr hdc, [Out] POINT[] lpPoints,
               [Out] byte[] lpTypes, int nSize);


            
            


            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                LOGFONT logFont = new LOGFONT();
                logFont.lfHeight = 20;
                logFont.lfWidth = 1;
                logFont.lfEscapement =900;
                logFont.lfOrientation = 900;
                logFont.lfWeight = 30;


                logFont.lfFaceName = "Mongolian Writing";
                logFont.lfCharSet = 1;
                logFont.lfOutPrecision = 1;
                logFont.lfPitchAndFamily = 1;


                //Bitmap bmp = new Bitmap(500, 100);
                //Graphics bmpG = Graphics.FromImage(bmp);


                //IntPtr dcBmp = bmp.GetHbitmap();


                IntPtr dc = e.Graphics.GetHdc();
                //SelectObject(dc, dcBmp);
                Font f = Font.FromLogFont(logFont);


                IntPtr hdcFont = f.ToHfont();


                SelectObject(dc, hdcFont);
                string TextV = "ᠭᠠᠯᠢᠭ ᠲᠤ ᠪᠠᠭᠤᠯᠭᠠᠬᠤ";
                SetROP2(dc, 5);
                SetBkMode(dc, 1);
                //BeginPath(dc);
                TextOut(dc, 0, 0, TextV, TextV.Length);
                //EndPath(dc);
                e.Graphics.ReleaseHdc();
                FontFamily ff=new FontFamily("Mongolian Writing");
                Font myFont=new Font(ff,20);
                e.Graphics.DrawString(TextV, myFont, Brushes.Blue, new PointF(0f, 50f));
                //int nCount = GetPath(dc, null, null, 0);
                //POINT[] ptPath = new POINT[nCount];
                //byte[] b = new byte[nCount];




                
                //string strPoints = "";
                //for (int i = 0; i < nCount; i++)
                //{
                //    strPoints += ptPath[i].ToString();
                //    strPoints += "\n";
                //}
                //e.Graphics.DrawString(strPoints, Font, Brushes.Black, new PointF(100.0f, 0.0f));
            }


            private void Form1_Load(object sender, EventArgs e)
            {


            }
        }
    }


    2012年3月14日 14:52