自定义MessageBox

其实就是一个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.

原文地址:https://www.cnblogs.com/xyzlmn/p/3168446.html