在C#中使用GetCharWidth32(BOOL GetCharWidth32(
  HDC hdc,          // handle to DC
  UINT iFirstChar,  // first character in range
  UINT iLastChar,   // last character in range
  LPINT lpBuffer    // buffer for widths
);),怎样修改GetCharWidth32的HDC的字体信息比如GetCharWidth32(e.Graphics.GetHdc(), 0, 255, buffer);
只能获得系统默认字体宽度。

解决方案 »

  1.   

    [DllImport("gdi32.dll", EntryPoint="GetCharWidth32")]
    public static extern int GetCharWidth32 (
    int hdc,
    int iFirstChar,
    int iLastChar,
    ref int lpBuffer
    );
      

  2.   

    怎样修改GetCharWidth32的HDC的字体信息,现在取回来的都是系统默认字体字号的信息。
      

  3.   

    GDI+已经提供了测量字符串尺寸的方法
    见Graphics.MeasureString
      

  4.   

    如果用GDI的GetCharWidth32函数,那得先用SelectObject设置当前的Font
      

  5.   


    Font font = new Font("宋体", 10);
    System.IntPtr aaaa = SelectObject(e.Graphics.GetHdc(), font.ToHfont());
    int[] arrint= new int[256];
    GetCharWidth(e.Graphics.GetHdc(), 0, 255, arrint);
    这个不对,有什么错误吗?
      

  6.   

    Font font = new Font("宋体", 10);
    System.IntPtr aaaa = SelectObject(e.Graphics.GetHdc(), font.ToHfont());
    int width;
    GetCharWidth(e.Graphics.GetHdc(), 0, 255, ref width);
    DeleteObject(...不过你的意图是什么,为什么把第二三个参数设置为0和255?
    此外为什么非得用GDI的函数?上个回复里已经说过GDI+提供了这种功能,见Graphics.MeasureString方法