1、IntPtr代表指针变量,是c#的基本类型。
2、BLENDFUNCTION是我定义的结构体,用于对应UpdateLayeredWindow这个API函数的BLENDFUNCTION类型参数:定义如下,UpdateLayeredWindow函数下面有说。
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
3、SelectObject是API函数,这里把其作为常函数定义在Win32API这个类里面,作用是将对象选进DC。具体怎么在c#里面引用API函数,可以参考MSDN
[DllImport("gdi32.dll", ExactSpelling = true)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
4、UpdateLayeredWindow也是API函数,定义在Win32API这个类里面,作用是把设置窗口的透明度或透明方式(Alapha还是透明指定颜色),下面是这个函数在c#里面引用的方法。
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
5、createParams.ExStyle |= 0x80000;是为了把窗口设为Layer窗口,一定要设置这个,不然窗口无法透明。其实这是API里面的CreateWindowEx函数创建窗口时用的扩展属性,可以参考MSDN。
可以的,不过一定要使用API来做。先把窗口设为Layer窗口,在form中重写CreateParams属性,如下:
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= Win32API.0x80000;
return createParams;
}
}
再用UpdateLayeredWindow设置一下要重绘的透明图片,范例如下,用到的API和结构要自己查MSDN哦:
IntPtr screenDc = Win32API.GetDC(IntPtr.Zero);
IntPtr memDc = Win32API.CreateCompatibleDC(screenDc);
IntPtr hBitmap = IntPtr.Zero;
IntPtr hOldBitmap = IntPtr.Zero;
try
{
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
hOldBitmap = Win32API.SelectObject(memDc, hBitmap);
Win32API.Size newSize = new Win32API.Size(bitmap.Width, bitmap.Height);
Win32API.Point sourceLocation = new Win32API.Point(0, 0);
Win32API.Point newLocation = new Win32API.Point(this.Left, this.Top);
Win32API.BLENDFUNCTION blend = new Win32API.BLENDFUNCTION();
blend.BlendOp = 0x00;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = 0x00;
Win32API.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize,
memDc, ref sourceLocation, 0, ref blend, 0x02);
}
finally
{
Win32API.ReleaseDC(IntPtr.Zero, screenDc);
if (hBitmap != IntPtr.Zero)
{
Win32API.SelectObject(memDc, hOldBitmap);
Win32API.DeleteObject(hBitmap);
}
Win32API.DeleteDC(memDc);
}
关于您上次回答的“[C#] GDI+能实现PNG格式背景图片的透明”~
1.IntPtr代表指针变量,是c#的基本类型
2.BLENDFUNCTION是我定义的结构体,用于对应UpdateLayeredWindow这个API函数的BLENDFUNCTION类型参数:定义如下,UpdateLayeredWindow函数下面有说。
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
3.SelectObject是API函数,我这里把它作为常函数定义在Win32API这个类里面,作用是将对象选进DC。我将它的c#引用方法也给你吧,具体怎么在c#里面引用API函数,可以参考MSDN
[DllImport("gdi32.dll", ExactSpelling = true)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
4.UpdateLayeredWindow也是API函数,定义在Win32API这个类里面,作用是把设置窗口的透明度或透明方式(Alapha还是透明指定颜色),下面是这个函数在c#里面引用的方法。
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
5.createParams.ExStyle |= Win32API.0x80000这句写错了,
应该是:createParams.ExStyle |= 0x80000;是为了把窗口设为Layer窗口,一定要设置这个,不然窗口无法透明。其实这是API里面的CreateWindowEx函数创建窗口时用的扩展属性,可以参考MSDN。
1.wpf
2.只能用那个Layer了
#19143125090#
C#想利用GDI+将文字转成图片,效果如下图,文字一定要粘住边,该怎么实现? - ******
#张窦# 我记得label里有个方法可以测量字符串所占像素宽度的 int size = new Label().CreateGraphics().MeasureString(text, font); 参数text是文本,font是文本格式,至于字符串所占像素高度的话好解决,跟font里的字体大小成比例的.用GDI+写完字符串后,根据高度和宽度再画个正方形.
#19143125090#
c#里面怎么使用gdi的库? - ******
#张窦# 不知道你说的GDI算不算GDI+.这样说吧======调用GDI+ 很简单,System.Drawing命名空间就是为GDI+而设计的,用Graphics类进行Draw/Fill函数,就是调用GDI+======调用GDI NET中绘图多是GDI+的,但也有托管下的GDI函数,比如TextRender类就是GDI的,(因为GDI+的DrawString绘图不精准,因此NET 2.0引入了GDI的TextRender) 也可以直接调用Windows API中的GDI,用DllImport链接gdi32.dll,调用里面的函数就可以了.
#19143125090#
C# WINDOWSFORM 如何生成表格图片呢? - ******
#张窦# 使用GDI+可以实现:一种方式是直接截取表格内容区域生成图片,另外一种通过GDI+来完成绘制表格数据生成图片!
#19143125090#
C# GDI+绘图 - ******
#张窦# 你可以将绘制的图片赋给picturebox控件,然后直接对picturebox控件进行操作就可以了.例子: Bitmap bm = new Bitmap(60,30); Graphics g= Graphics.FromImage(bm);g.Clear(Color.FromArgb(192,192,255));g.DrawString("要绘制的文字", ...
#19143125090#
关于C#GDI绘制表格 - ******
#张窦# 计算每个格的位置,直接DrawString,可以指定字体,颜色,对齐方式等
#19143125090#
C#Bitmap是干什么用的?能帮我解释下吗? - ******
#张窦# c#.net Bitmap类的基本使用方法 http://download.csdn.net/source/1002902 Bitmap 类 更新:2007 年 11 月 封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成.Bitmap 是用于处理由像素数据定义的图像的对象.命名空间: System....
#19143125090#
(C#)GDI绘制的图如何保存成图片 - ******
#张窦# 绘制到Bitmap上Bitmap bit=new Bitmap(200,200,PixelFormat.Format32bppArgb);//设置长宽 using (var g=Graphics.FromImage(bit)) { //这里绘图 g.Flush(); } bit.Save("0.png");
#19143125090#
C#中,GDI+的一点提问,关于image.save() - ******
#张窦# 将此图像以指定的格式保存到指定的流中. 名称说明 Image.Save (String)将该 Image 保存到指定的文件或流. Image.Save (Stream, ImageFormat)将此图像以指定的格式保存到指定的流中. 由 .NET Compact Framework 支持. Image...
#19143125090#
C#的GDI画图问题 - ******
#张窦# 没有清空画布 就会保留上次的绘画记录加上这一句 里面忘了有没有参数了 如果有 可以添加清空后的颜色sIma.Clear();
#19143125090#
C# GDI+ 画图问题 - ******
#张窦# Graphics g = this.CreateGraphic();即可 this即为你的窗体类