如题   
    
    
  我使用CreateFont   API创建字体后,在OnPaint事件中使用Font.FromHfont(IntPtr)获得字体,g.DrawString()显示,可是不管如何设置字体的宽和高,显示出的字体还是宽=高。好像宽度设置不管用,只有高度设置有用。   
    
    
  请问问题出在哪里?或有什么其他方法实现我的初衷?   
    
    
  热切盼望回复!!!谢谢。 

解决方案 »

  1.   

    你是通过Font.Size 属性设置的高宽吗?
      

  2.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class Font : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取系统的所有字体名称(方法一)
            //for (int i = 0; i <= System.Drawing.FontFamily.Families.Length - 1; i++)
            //{
            //      this.ddlFont.Items.Add(System.Drawing.FontFamily.Families[i].Name);
            //}        //获取系统的所有字体名称,也可以使用foreach(方法二)        System.Drawing.Text.InstalledFontCollection font;//安装在系统的所有字体,无法继承
            font = new System.Drawing.Text.InstalledFontCollection();
            foreach (System.Drawing.FontFamily family in font.Families)
            {
                this.ddlFont .Items.Add(family.Name);
            }        //获取系统所有颜色(利用枚举获取系统的颜色并且将Dropdownlist的字体颜色改成当前的颜色)
            string[] colorArray = Enum.GetNames(typeof(System.Drawing.KnownColor));
            foreach (string color in colorArray)
            {
                ListItem item = new ListItem(color);
                item.Attributes.Add("style", "color:" + color);
                this.ddlColor .Style.Add("BackColor", color);
                this.ddlColor .Items.Add(item);        }
            //系统字体大小
            string[] sizeArray = Enum.GetNames(typeof(System.Web.UI.WebControls.FontSize));
            this.rblSize .DataSource = sizeArray;
            this.rblSize.SelectedIndex = -1;
            this.rblSize.DataBind();
        }
     
        protected void btnFont_Click(object sender, EventArgs e)
        {
            this.lblString.ForeColor = System.Drawing.Color.FromName(this.ddlColor.SelectedItem.Text);
            this.lblString.Font.Name = this.ddlFont.SelectedItem.Text;
            if (this.rblSize.SelectedIndex > -1)
            {
                this.lblString.Font.Size = FontUnit.Parse(this.rblSize.SelectedItem.Text);//选择字体的大小
            }
            else
            {
                this.lblString.Font.Size = FontUnit.Point(Int32.Parse(this.txtSize.Text));//设置字体的大小
            }
        }
        
    }你认真调试一下,你会发现这个不仅仅是你想要的东西.
      

  3.   


    -----Font.Size只能设置大小,不能设置宽高。
    —.—
      

  4.   


    谢谢回复,忘了说了,我说的是winform程序,不是web的。
    不过还是谢谢。
      

  5.   

    当你去设置一个字体的大小的时候,你要去设置的是这个字体的size,它是一个PointF类型的参数,你要给它一个这样的值。
      

  6.   


    size是float类型的吧,怎么会是pointf类型的
      

  7.   

    目前好像只能通过Api来建立字体了。C#本身的类库没有这样的实现。
      

  8.   

    我看那些mfc的代码,都有个selectobject方法,在c#中直接传给font就可以了吧。还是因为font类型是c#的,它的宽高比值是不可变得,所以Api传过来的字体应用到Font上还是系统的宽高比值。是不是应该使用 别的类型定义字符串的字体?但是c#好像还不行吧。
      

  9.   

    思路. 将字转发为GraphicsPath, 变形该GraphicsPath. 再将该GraphicsPath画出来.
    Graphics g = this.CreateGraphics();
                g.SmoothingMode = SmoothingMode.HighQuality;
                
                GraphicsPath path = new GraphicsPath();
                StringFormat strformat = new StringFormat();
                strformat.Alignment = StringAlignment.Center;
                strformat.LineAlignment = StringAlignment.Center;
                path.AddString("中国", new FontFamily("黑体"), (int)this.Font.Style, 100f, new Point(250,250), strformat);
                
                Matrix m=new Matrix();
                m.Scale(1f,0.2f);   //压扁
                path.Transform(m);
                g.FillPath(new SolidBrush(Color.Red), path);