自定义MessageBox

来源:互联网 发布:软件项目进度计划表 编辑:程序博客网 时间:2024/05/25 01:35

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

 

The convenience of having custom controls is that they can be usedas normal controls. Where convenient, we adhered to this philosophythroughout the project; e.g. the message box, the picture button andthe sliding list are real controls, ready to be reuse in externalprojects.

Custom Image Button

The button ImageButtoninherits from Control and allows you to display an image with thebehaviors of a button. To customize the control we perform the overrideof 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, Int32yDest, Int32 cxDest, Int32 cyDest, IntPtr hdcSrc, Int32 xSrc, Int32ySrc, 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 methodsoverridden, drawing a picture completely black semitransparent. Afterpainting the background image, the solid background image of themessage box is drawn using the technique of image transparency offeredby the Compact Framework. First you must create an instance of theclass 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 thefirst and the second parameter of the SetColorKey function. In the .NETCompact Framework instaed this is not possible, and the two parametersneed 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);

Inthe case of our application, the final result is a background imagewith the edges rounded, while the rest of the screen is darker.

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 12306取消三次后怎么办 外国人护照过期了怎么办 行李丢飞机上怎么办 学生票没次数了怎么办 去美国行李超重怎么办 12306密码找回失败怎么办 12306注册身份重复怎么办 12306身份信息重复怎么办 必修课没有选上怎么办 大学错过了选课怎么办 大学忘记选课了怎么办 火车网上购票儿童票怎么办 售票厅看见小偷怎么办 没赶上火车火车票怎么办 重庆到韩国签证怎么办 重庆去韩国签证怎么办 重庆办韩国签证怎么办 故宫网上预定后怎么办 坐游轮如果晕船怎么办 听听力反应慢怎么办? 毕业证照片丢了怎么办 离线网盘有违规内容怎么办 手机不能向下拉怎么办 cad运行不了插件怎么办 ai中缺少文字怎么办 电脑被格式化了怎么办 迷你世界地图下载失败怎么办 网页上广告太多怎么办 网页打开广告太多怎么办 PS界面图标小怎么办 百度地图反应慢怎么办 汽车导航不播报怎么办 wps菜单栏隐藏了怎么办 手机导航声音小怎么办 手机导航不好用怎么办 手机导航箭头不准怎么办 手机处于离线状态怎么办 穷人让人看不起怎么办 非洲人口过多怎么办啊 鼻子上长白头怎么办 高铁查到违禁品怎么办