自定义字体的gdi+方法:
调用API方法,使用API画。
- [System.Runtime.InteropServices.DllImport("gdi32")]
- private static extern IntPtr CreateFont (int H, int W, int E,
- int O, int FW, int I, int u, int S, int C, int OP, int CP, int Q, int PAF, string F);
- [System.Runtime.InteropServices.DllImport("user32")]
- private static extern IntPtr GetDC(IntPtr hwnd);
- [System.Runtime.InteropServices.DllImport("gdi32")]
- private static extern IntPtr BeginPath(IntPtr hdc);
- [System.Runtime.InteropServices.DllImport("gdi32")]
- private static extern IntPtr EndPath(IntPtr hdc);
- [System.Runtime.InteropServices.DllImport("gdi32")]
- private static extern IntPtr PathToRegion(IntPtr hdc);
- [System.Runtime.InteropServices.DllImport("gdi32")]
- private static extern int SetBkMode(IntPtr hdc, int nBkMode);
- [System.Runtime.InteropServices.DllImport("gdi32")]
- private static extern int SetFMode(IntPtr hdc, int nBkMode);
- [System.Runtime.InteropServices.DllImport("gdi32")]
- private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
- [System.Runtime.InteropServices.DllImport("gdi32")]
- private static extern int TextOut(IntPtr hdc, int x, int y, string lpString, int nCount);
- [System.Runtime.InteropServices.DllImport("gdi32")]
- private static extern int TextOut( int x, int y, string lpString, int nCount);
- [System.Runtime.InteropServices.DllImport("user32")]
- private static extern IntPtr SetWindowRgn(IntPtr hwnd, IntPtr hRgn, bool bRedraw);
- //
- [System.Runtime.InteropServices.DllImport("gdi32.dll")]
- static extern uint SetTextColor(IntPtr hdc, int crColor);
- [System.Runtime.InteropServices.DllImport("gdi32.dll")]
- static extern bool GetTextExtentPoint(IntPtr hdc, string lpString,
- int cbString, ref Size lpSize);
- const int FW_HEAVY = 900;
- const int ANSI_CHARSET = 0;
- const int OUT_DEFAULT_PRECIS = 0;
- const int CLIP_DEFAULT_PRECIS = 0;
- const int DEFAULT_QUALITY = 0;
- const int DEFAULT_PITCH = 0;
- const int FF_SWISS = 32;
- const int TRANSPARENT = 4;
使用:
- this.ForeColor = Color.Red;
- IntPtr dc = e.Graphics.GetHdc();
- IntPtr m_Font = CreateFont(20, (int)14, 0, 0, FW_HEAVY, 0, 0,
- 0, 134, OUT_DEFAULT_PRECIS,
- CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
- DEFAULT_PITCH | FF_SWISS, "宋体");
- //创建字体
- //SetBkMode(dc,ColorTranslator.ToWin32(Color.White));
- //设置背景为透明模式,这是必须有的
- Size MeasureSize = new Size(0, 0);
- SetTextColor(dc, ColorTranslator.ToWin32(Color.Red));
- IntPtr m_OldFont = SelectObject(dc, m_Font);
- string strE = "c#";
- string strC = "我的新字体";
- int x=0,y=this.ClientSize.Height / 2;
- SelectObject(dc, m_Font);
- TextOut(dc, x,y , strE, strE.Length);
- GetTextExtentPoint(dc, strE, strE.ToString().Length, ref MeasureSize);
- x += MeasureSize.Width;
- //if (x + MeasureSize.Width > ClientSize.Width)
- //{
- // y += MeasureSize.Height;
- // x = 0;
- //}
- //m_Font = CreateFont(18, 25, -0, 0, FW_HEAVY, 0, 0,
- // 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
- // CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
- // DEFAULT_PITCH | FF_SWISS, "宋体");
- SelectObject(dc, m_Font);
- TextOut(dc, x, y, strC, strC.Length * 2);
- e.Graphics.ReleaseHdc();
- x = -0;
- e.Graphics.DrawLine(Pens.Black, new Point(x, y), new Point(this.ClientSize.Width, y));
- e.Graphics.DrawString("我的jiu字体", Font, new SolidBrush(Color.Blue), new PointF(x, y + MeasureSize.Height));