7、设置字形窗口
[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 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("user32")]
private static extern IntPtr SetWindowRgn(IntPtr hwnd,IntPtr hRgn,bool bRedraw);
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 = 1;
private void Form1_Load(object sender, System.EventArgs e)
{
IntPtr dc=GetDC(this.Handle);
IntPtr m_Font=CreateFont(200, 100, 0, 0, FW_HEAVY, 1, 0,
0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS, "宋体");
//创建字体
BeginPath(dc);
//开始记录窗体轮廓路径
SetBkMode(dc,TRANSPARENT);
//设置背景为透明模式,这是必须有的
IntPtr m_OldFont=SelectObject(dc,m_Font);
TextOut(dc,0, 0, "C#",2);
SelectObject(dc,m_OldFont);
EndPath(dc);
//结束记录窗体轮廓路径
IntPtr m_wndRgn=PathToRegion(dc);
//把所记录的路径转化为窗体轮廓句柄
SetWindowRgn(this.Handle,m_wndRgn,true);
//赋予窗体指定的轮廓形状
}
8、屏幕截图
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern IntPtr CreateDC (
string lpszDriver , // 驱动名称
string lpszDevice , // 设备名称
string lpszOutput , // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
) ;
private void button1_Click(object sender, System.EventArgs e)
{
this.Hide();
IntPtr dc1 = CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ;
//创建显示器的DC
Graphics g1 = Graphics.FromHdc ( dc1 ) ;
//由一个指定设备的句柄创建一个新的Graphics对象
Bitmap MyImage = new Bitmap ( Screen.PrimaryScreen.Bounds.Width , Screen.PrimaryScreen.Bounds.Height , g1 ) ;
//根据屏幕大小创建一个与之相同大小的Bitmap对象
Graphics g2 = Graphics.FromImage ( MyImage ) ;
//获得屏幕的句柄
IntPtr dc3 = g1.GetHdc ( ) ;
//获得位图的句柄
IntPtr dc2 = g2.GetHdc ( ) ;
//把当前屏幕捕获到位图对象中
BitBlt ( dc2 , 0 , 0 , Screen.PrimaryScreen.Bounds.Width , Screen.PrimaryScreen.Bounds.Height , dc3 , 0 , 0 , 13369376 ) ;
//把当前屏幕拷贝到位图中
g1.ReleaseHdc ( dc3 ) ;
//释放屏幕句柄
g2.ReleaseHdc ( dc2 ) ;
//释放位图句柄
MyImage.Save ( "c:\\MyJpeg.jpg" , ImageFormat.Jpeg ) ;
MessageBox.Show ( "已经把当前屏幕保存到C:\\MyJpeg.jpg文件中!" ) ;
this.Show();
}
}
9、图片的透明显示
Bitmap b=new Bitmap("..\\..\\test.bmp");
b.MakeTransparent(Color.White);
this.pictureBox1.Image=b;
本系列文章是作者学习《Visual C#.NET 应用编程150例》(源码)心得笔记,欢迎转载,请注明原文地址,如有疑问,可以通过 [email protected] 联系作者本人。