自定义字体的gdi+方法

自定义字体的gdi+方法:

调用API方法,使用API画。

 

  1. [System.Runtime.InteropServices.DllImport("gdi32")]
  2.         private static extern IntPtr CreateFont    (int H, int W, int E, 
  3.                 int O, int FW, int I, int u, int S, int C, int OP, int CP, int Q, int PAF, string F);
  4.         [System.Runtime.InteropServices.DllImport("user32")]
  5.         private static extern IntPtr GetDC(IntPtr hwnd);
  6.         [System.Runtime.InteropServices.DllImport("gdi32")]
  7.         private static extern IntPtr BeginPath(IntPtr hdc);
  8.         [System.Runtime.InteropServices.DllImport("gdi32")]
  9.         private static extern IntPtr EndPath(IntPtr hdc);
  10.         [System.Runtime.InteropServices.DllImport("gdi32")]
  11.         private static extern IntPtr PathToRegion(IntPtr hdc);
  12.         [System.Runtime.InteropServices.DllImport("gdi32")]
  13.         private static extern int SetBkMode(IntPtr hdc, int nBkMode);
  14.         [System.Runtime.InteropServices.DllImport("gdi32")]
  15.         private static extern int SetFMode(IntPtr hdc, int nBkMode);
  16.         [System.Runtime.InteropServices.DllImport("gdi32")]
  17.         private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
  18.         [System.Runtime.InteropServices.DllImport("gdi32")]
  19.         private static extern int TextOut(IntPtr hdc, int x, int y, string lpString, int nCount);
  20.         [System.Runtime.InteropServices.DllImport("gdi32")]
  21.         private static extern int TextOut( int x, int y, string lpString, int nCount);
  22.         [System.Runtime.InteropServices.DllImport("user32")]
  23.         private static extern IntPtr SetWindowRgn(IntPtr hwnd, IntPtr hRgn, bool bRedraw);
  24.         //
  25.         [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  26.         static extern uint SetTextColor(IntPtr hdc, int crColor);
  27.         [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  28.         static extern bool GetTextExtentPoint(IntPtr hdc, string lpString,
  29.             int cbString, ref Size lpSize);
  30.         const int FW_HEAVY = 900;
  31.         const int ANSI_CHARSET = 0;
  32.         const int OUT_DEFAULT_PRECIS = 0;
  33.         const int CLIP_DEFAULT_PRECIS = 0;
  34.         const int DEFAULT_QUALITY = 0;
  35.         const int DEFAULT_PITCH = 0;
  36.         const int FF_SWISS = 32;
  37.         const int TRANSPARENT = 4;

使用:

  1.             this.ForeColor = Color.Red;
  2.             IntPtr dc = e.Graphics.GetHdc();
  3.             IntPtr m_Font = CreateFont(20, (int)14, 0, 0, FW_HEAVY, 0, 0,
  4.              0, 134, OUT_DEFAULT_PRECIS,
  5.              CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  6.              DEFAULT_PITCH | FF_SWISS, "宋体");
  7.             //创建字体
  8.             
  9.             //SetBkMode(dc,ColorTranslator.ToWin32(Color.White));
  10.             //设置背景为透明模式,这是必须有的
  11.             Size MeasureSize = new Size(0, 0);
  12.             SetTextColor(dc, ColorTranslator.ToWin32(Color.Red));
  13.             IntPtr m_OldFont = SelectObject(dc, m_Font);
  14.             string strE = "c#";
  15.             string strC = "我的新字体";
  16.             int x=0,y=this.ClientSize.Height / 2;
  17.             
  18.             SelectObject(dc, m_Font);
  19.             TextOut(dc, x,y , strE, strE.Length);
  20.             GetTextExtentPoint(dc, strE, strE.ToString().Length, ref MeasureSize);
  21.             x += MeasureSize.Width;
  22.             //if (x + MeasureSize.Width > ClientSize.Width)
  23.             //{
  24.             //    y += MeasureSize.Height;
  25.             //    x = 0;
  26.             //}
  27.             //m_Font = CreateFont(18, 25, -0, 0, FW_HEAVY, 0, 0,
  28.             // 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
  29.             // CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  30.             // DEFAULT_PITCH | FF_SWISS, "宋体");
  31.             SelectObject(dc, m_Font);
  32.             TextOut(dc, x, y, strC, strC.Length * 2);
  33.             e.Graphics.ReleaseHdc();
  34.             x = -0;
  35.             e.Graphics.DrawLine(Pens.Black, new Point(x, y), new Point(this.ClientSize.Width, y));
  36.             e.Graphics.DrawString("我的jiu字体", Font, new SolidBrush(Color.Blue), new PointF(x, y + MeasureSize.Height));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值