博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义MessageBox
阅读量:6926 次
发布时间:2019-06-27

本文共 3447 字,大约阅读时间需要 11 分钟。

其实就是一个Form遮挡另一个Form。

 

The convenience of having custom controls is that they can be used as normal controls. Where convenient, we adhered to this philosophy throughout the project; e.g. the message box, the picture button and the sliding list are real controls, ready to be reuse in external projects.

Custom Image Button

The button ImageButton inherits from Control and allows you to display an image with the behaviors of a button. To customize the control we perform the override of these methods:

protected override void OnPaintBackground(PaintEventArgs e)

{
//Do nothing
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (isPressed)
e.Graphics.DrawImage(imagePressed, 0, 0);
else
e.Graphics.DrawImage(image, 0, 0);
}

To change the image of the button when it is pressed, we override OnMouseDown and OnMouseUp event methods and we set a Boolean defining which image to draw:

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)

{
isPressed = true;
this.Invalidate();
base.OnMouseDown(e);
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
isPressed = false;
this.Invalidate();
base.OnMouseUp(e);
}

Finally, in order to use this custom control within a form, we can use the following:

ImageButton customButton = new ImageButton(bitmap, bitmapPressed, transparentColor);

customButton.Size = new Size(200,50);
customButton.Location = newPoint(0,0);
customButton.Click += new EventHandler(customButton_Click);

Custom MessageBox, AlphaBlend and Image Trasparency

The MessageBox is a full screen form exploiting alpha blending and image transparency. The alpha blending is obtained from a PInvoke call to the AlphaBlend API, which is available starting from Windows CE version 5.0.

[DllImport("coredll.dll")]

extern publicstatic Int32 AlphaBlend(IntPtr hdcDest, Int32 xDest, Int32 yDest, Int32 cxDest, Int32 cyDest, IntPtr hdcSrc, Int32 xSrc, Int32 ySrc, Int32 cxSrc, Int32 cySrc, BlendFunction blendFunction);

The function is wrapped by the managed method DrawApha , which can be called as follows:

Drawing .DrawAlpha(e.Graphics, background, 180, 0, 0);

NOTE: the image “background ” will be drawn with a transparency of 70% (=180/255).

As for the ImageButton , the customized message box has OnPaintBackground and OnPaint methods overridden, drawing a picture completely black semitransparent. After painting the background image, the solid background image of the message box is drawn using the technique of image transparency offered by the Compact Framework. First you must create an instance of the class ImageAttributes:

transparency = new ImageAttributes

Then you have to decide what color to make transparent, in this case we are using the Black :

transparency.SetColorKey(transparentColor, transparentColor);

NOTE: in .NET Framework, you can select a range of colors ranging from the first and the second parameter of the SetColorKey function. In the .NET Compact Framework instaed this is not possible, and the two parameters need to be equal.

Finally you can draw the image using the following overload method of Graphics.DrawImage:

e.Graphics.DrawImage(background, rectangle, 0, 0, background.Width, background.Height, GraphicsUnit.Pixel, transparency);

In the case of our application, the final result is a background image with the edges rounded, while the rest of the screen is darker.

 

     本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/819917,如需转载请自行联系原作者

你可能感兴趣的文章
iOS内存暴增问题追查与使用陷阱
查看>>
linux grep命令 (学习备份)
查看>>
139.00.007 Git学习-Cheat Sheet
查看>>
2017-11-06-构建之法:现代软件工程-阅读笔记
查看>>
vue 进行 gzip压缩和服务器如何开启gzip(转)
查看>>
组合模式-虚有其表的模式
查看>>
java8 peek
查看>>
Python实现字符串反转的几种方法
查看>>
目前国际上所用云计算平台IaaS、PaaS、SaaS简介
查看>>
模式识别之预测---一元线性回归
查看>>
响应式ie8兼容问题
查看>>
[16]CSS3 边框图片效果
查看>>
mysql group_concat方法用法
查看>>
[六省联考2017]摧毁“树状图”
查看>>
利用自然数的标准分解证明可数集合的所有有限子集形成的集合是可数集
查看>>
Excel中SEARCH和FIND函数的区别
查看>>
js继承综合
查看>>
[转译]5种方法提高你网站的登录体验
查看>>
关于Grunt
查看>>
linux基础名词
查看>>