C#中如何实现自定义字体显示,我想显示可以设置宽高比例的字体,就比如瘦高字体,矮胖子体

wwei466 2008-09-09 05:06:26
如题


我使用CreateFont API创建字体后,在OnPaint事件中使用Font.FromHfont(IntPtr)获得字体,g.DrawString()显示,可是不管如何设置字体的宽和高,显示出的字体还是宽=高。好像宽度设置不管用,只有高度设置有用。


请问问题出在哪里?或有什么其他方法实现我的初衷?


热切盼望回复!!!谢谢。
...全文
1107 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwei466 2008-10-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 h_w_king 的回复:]
思路. 将字转发为GraphicsPath, 变形该GraphicsPath. 再将该GraphicsPath画出来.

C# codeGraphics g = this.CreateGraphics();
g.SmoothingMode = SmoothingMode.HighQuality;

GraphicsPath path = new GraphicsPath();
StringFormat strformat = new StringFormat();
strformat.Alignment = StringAlignment.Center;
strformat.LineAlignm…
[/Quote]

我也是这么想的,但是实现起来比较麻烦。字体较多,而且还要实现事件的响应,也就是说还要找出它的位置来。
不过在c#中也只能是这么做了!
wwei466 2008-09-10
  • 打赏
  • 举报
回复
我看那些mfc的代码,都有个selectobject方法,在c#中直接传给font就可以了吧。

还是因为font类型是c#的,它的宽高比值是不可变得,所以Api传过来的字体应用到Font上还是系统的宽高比值。

是不是应该使用 别的类型定义字符串的字体?但是c#好像还不行吧。
wwei466 2008-09-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hbxtlhx 的回复:]
目前好像只能通过Api来建立字体了。C#本身的类库没有这样的实现。
[/Quote]

那该如何做呢?
北京的雾霾天 2008-09-10
  • 打赏
  • 举报
回复
目前好像只能通过Api来建立字体了。C#本身的类库没有这样的实现。
wwei466 2008-09-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 TengGaoqing 的回复:]
当你去设置一个字体的大小的时候,你要去设置的是这个字体的size,它是一个PointF类型的参数,你要给它一个这样的值。
[/Quote]

size是float类型的吧,怎么会是pointf类型的
TengGaoqing 2008-09-10
  • 打赏
  • 举报
回复
当你去设置一个字体的大小的时候,你要去设置的是这个字体的size,它是一个PointF类型的参数,你要给它一个这样的值。

hanyu0528 2008-09-10
  • 打赏
  • 举报
回复
不懂,帮顶啊~!!!
wwei466 2008-09-10
  • 打赏
  • 举报
回复
大家都说说看啊,顶着有分。
h_w_king 2008-09-10
  • 打赏
  • 举报
回复
思路. 将字转发为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);
wwei466 2008-09-10
  • 打赏
  • 举报
回复
没有人会吗,一起讨论讨论也行啊。
wwei466 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 flysun0311 的回复:]
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)

[/Quote]

谢谢回复,忘了说了,我说的是winform程序,不是web的。
不过还是谢谢。
wwei466 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 atlasroben 的回复:]
你是通过Font.Size 属性设置的高宽吗?
[/Quote]

-----Font.Size只能设置大小,不能设置宽高。
—.—
flysun0311 2008-09-09
  • 打赏
  • 举报
回复
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));//设置字体的大小
}
}

}



你认真调试一下,你会发现这个不仅仅是你想要的东西.
atlasroben 2008-09-09
  • 打赏
  • 举报
回复
你是通过Font.Size 属性设置的高宽吗?
wwei466 2008-09-09
  • 打赏
  • 举报
回复
自己鼎个先!
请大家尽快回复啊。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧