如何设置label字符间距? 是否需要重写?

解决方案 »

  1.   

    找到这段代码 但是不会用:
     [DllImport("gdi32.dll",   CharSet=CharSet.Auto)]  
    public   static   extern   int   SetTextCharacterExtra(IntPtr hdc,int   nCharExtra); Font   f=   new   Font("宋体",20);   
    Brush   b=   new   SolidBrush(Color.Green);   
    String   s="11111";   
        
    IntPtr   hwnd   =   new   IntPtr();   
    hwnd   =   this.Handle;   
    Graphics   g   =   Graphics.FromHwnd(hwnd);   
    IntPtr   hdc   =   g.GetHdc();   
    SetTextCharacterExtra(hdc,40);   
    using(Graphics   gg   =   Graphics.FromHdc(hdc))   
    {   
    gg.DrawString(s,f,b,0,0);   
    }   
    g.ReleaseHdc(hdc);   
    g.Dispose();
      

  2.   

    可以试试Graphics.DrawStringGraphics   g   =   this.CreateGraphics();  
      string   strValue   =   "Hello   World";  
      StringFormat   myformat   =   new   StringFormat();  
      myformat.Alignment   =   StringAlignment.Center;  
      myformat.LineAlignment   =   StringAlignment.Center;  
      myformat.FormatFlags   =   StringFormatFlags.FitBlackBox;  
       
      Font   fnt   =   new   Font(   "宋体",10f   );  
      Rectangle   rect   =   new   Rectangle(   300,   300,   170,   30   );  
       
      SizeF   size   =   g.MeasureString(   strValue,   fnt   );  
      Bitmap   bit   =   new   Bitmap(   (int)(size.Width),   (int)(size.Height)   );  
      Rectangle   rectBase   =   new   Rectangle(   0,   0,   bit.Width,   bit.Height   );  
      Graphics   gImage   =   Graphics.FromImage(   bit   );  
      gImage.DrawString(   strValue,   fnt,  
      Brushes.Black,  
      rectBase,   myformat   );  
      gImage.Save();  
       
      //Stretch   image   to   specific   width  
      rect.X   =   rect.Top   +   (   rect.Height   -   rectBase.Height   )   /   2;  
      rect.Height   =   rectBase.Height;  
      g.DrawImage(   bit,   rect,   rectBase,GraphicsUnit.Pixel   );  
      gImage.Dispose();  
      bit.Dispose();