Disable Background for popup like asp.net ajax

2 posts, 0 answers
  1. Won
    Won avatar
    15 posts
    Member since:
    Sep 2010

    Posted 05 Oct 2010 Link to this post

    Is it possible that making background look dark and not being able to click when there is a popup window from grid like asp.net ajax does?
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 05 Oct 2010 Link to this post

    Hello Won,

    Sorry, but transparency (opacity) is not supported in Winforms (for anything other than for the form themselves), but you could "fake" this behavior by doing the following:
    1 Creating a control that will have a PictureBox where the snapshot of your running form will be.
    2. Add that control to your form docked
    3. Call a ShowMessageBox method (from within your control)
    4 This will Take a snapshot of your form and attach it to the picturebox, and using a timer will create a fading efect.
    5. Show the message box
    6. Close the messagebox and revert to original

    And following, i have created an application to test this ( i was meaning to for a while now, and now i had some free time to play around with some things)
    using System.Windows.Forms;
     
    namespace TestDarkenMessageBox
    {
        using System.Drawing;
        using System.Drawing.Imaging;
        using Telerik.WinControls;
        using Telerik.WinControls.UI;
     
        public partial class Form1 : Form
        {
            private RadDarkenControl darkenControl;
     
            public Form1()
            {
                InitializeComponent();
                this.Load += new System.EventHandler(Form1_Load);
            }
     
            void button_Click(object sender, System.EventArgs e)
            {
                darkenControl.ShowMessageBox("BOO!");
            }
     
            void Form1_Load(object sender, System.EventArgs e)
            {
                var button = new RadButton();
                button.Text = "Show MessageBox";
                button.Click += new System.EventHandler(button_Click);
                this.Controls.Add(button);
     
                button = new RadButton();
                button.Text = "Test";
                button.Click += new System.EventHandler(button_Click);
                button.Dock = DockStyle.Bottom;
                this.Controls.Add(button);
     
                button = new RadButton();
                button.Text = "Test2";
                button.Click += new System.EventHandler(button_Click);
                button.Dock = DockStyle.Bottom;
                this.Controls.Add(button);
     
                darkenControl = new RadDarkenControl(this);
                darkenControl.Size = this.Size;
                darkenControl.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
                this.Controls.Add(darkenControl);
            }
        }
     
        public class RadDarkenControl : RadControl
        {
            private Form parent;
     
            private PictureBox darkenedForm = new PictureBox();
     
            private int minimumBrightness = -30;
     
            private int brightness = 0;
     
            public RadDarkenControl(Form parentForm)
            {
                InitializeComponent();
                this.parent = parentForm;
                this.Visible = false;
                this.Controls.Add(darkenedForm);
                darkenedForm.Dock = DockStyle.Fill;
            }
     
            private void ShowControl()
            {
                brightness = 0;
     
                this.Visible = true;
     
                var formSnapshot = this.CaptureControl(parent);
                darkenedForm.Image = AdjustBrightness(formSnapshot, brightness);
                darkenedForm.BringToFront();
                this.BringToFront();
                var timer = new Timer();
                timer.Tick += new System.EventHandler(timer_Tick);
                timer.Interval = 20;
                timer.Start();
            }
     
            void timer_Tick(object sender, System.EventArgs e)
            {
                if (brightness > minimumBrightness)
                {
                    darkenedForm.Image = AdjustBrightness((Bitmap)darkenedForm.Image, brightness);
                    brightness -= 5;
                }
            }
     
            private Bitmap CaptureControl(Form form)
            {
                var borderWidth = (parent.Width - parent.ClientSize.Width) / 2;
                var titlebarHeight = parent.Height - parent.ClientSize.Height - borderWidth;
     
                Graphics myGraphics = this.CreateGraphics();
                Size s = form.Size;
                var memoryImage = new Bitmap(s.Width - borderWidth, s.Height - titlebarHeight, myGraphics);
                Graphics memoryGraphics = Graphics.FromImage(memoryImage);
                memoryGraphics.CopyFromScreen(
                    form.Location.X + borderWidth, form.Location.Y + titlebarHeight, 0, 0, s);
     
                return memoryImage;
            }
     
            private void HideControl()
            {
                this.Visible = false;
            }
     
            public void ShowMessageBox(string text)
            {
                this.ShowControl();
                MessageBox.Show(text);
                this.HideControl();
            }
     
            public static Bitmap AdjustBrightness(Bitmap Image, int Value)
            {
                System.Drawing.Bitmap TempBitmap = Image;
                float FinalValue = (float)Value / 255.0f;
                System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
                System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
                float[][] FloatColorMatrix ={
                         new float[] {1, 0, 0, 0, 0},
                         new float[] {0, 1, 0, 0, 0},
                         new float[] {0, 0, 1, 0, 0},
                         new float[] {0, 0, 0, 1, 0},
                         new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
                     };
     
                System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatrix);
                System.Drawing.Imaging.ImageAttributes Attributes = new System.Drawing.Imaging.ImageAttributes();
                Attributes.SetColorMatrix(NewColorMatrix);
                NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes);
                Attributes.Dispose();
                NewGraphics.Dispose();
                return NewBitmap;
            }
     
            public static Bitmap AdjustContrast(Bitmap Image, float Value)
            {
                Value = (100.0f + Value) / 100.0f;
                Value *= Value;
                System.Drawing.Bitmap TempBitmap = Image;
                System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
                System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
                NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
                NewGraphics.Dispose();
     
                for (int x = 0; x < NewBitmap.Width; ++x)
                {
                    for (int y = 0; y < NewBitmap.Height; ++y)
                    {
                        Color Pixel = NewBitmap.GetPixel(x, y);
                        float Red = Pixel.R / 255.0f;
                        float Green = Pixel.G / 255.0f;
                        float Blue = Pixel.B / 255.0f;
                        Red = (((Red - 0.5f) * Value) + 0.5f) * 255.0f;
                        Green = (((Green - 0.5f) * Value) + 0.5f) * 255.0f;
                        Blue = (((Blue - 0.5f) * Value) + 0.5f) * 255.0f;
                        NewBitmap.SetPixel(x, y, Color.FromArgb(Clamp((int)Red, 255, 0), Clamp((int)Green, 255, 0), Clamp((int)Blue, 255, 0)));
                    }
                }
     
                return NewBitmap;
            }
     
            public static Bitmap AdjustGamma(Bitmap Image, float Value)
            {
                System.Drawing.Bitmap TempBitmap = Image;
                System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
                System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
                NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
                NewGraphics.Dispose();
     
                int[] RedRamp = new int[256];
                int[] GreenRamp = new int[256];
                int[] BlueRamp = new int[256];
                for (int x = 0; x < 256; ++x)
                {
                    RedRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
                    GreenRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
                    BlueRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
                }
     
                for (int x = 0; x < NewBitmap.Width; ++x)
                {
                    for (int y = 0; y < NewBitmap.Height; ++y)
                    {
                        Color Pixel = NewBitmap.GetPixel(x, y);
                        int Red = RedRamp[Pixel.R];
                        int Green = GreenRamp[Pixel.G];
                        int Blue = BlueRamp[Pixel.B];
                        NewBitmap.SetPixel(x, y, Color.FromArgb(Red, Green, Blue));
                    }
                }
     
                return NewBitmap;
            }
     
            private static int Clamp(int Value, int Max, int Min)
            {
                Value = Value > Max ? Max : Value;
                Value = Value < Min ? Min : Value;
                return Value;
            }
     
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
     
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
     
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
                this.SuspendLayout();
                //
                // RadDarkenControl
                //
                this.ClientSize = new System.Drawing.Size(298, 279);
                this.Name = "RadDarkenControl";
                this.Text = "RadDarkenControl";
                this.ThemeName = "ControlDefault";
                ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
                this.ResumeLayout(false);
            }
        }
    }

    *Update, if you want a proof of concept for displaying a form from this you could just create a new method like this:
    public void ShowSomeForm(Form form)
    {
        this.ShowControl();
        form.FormClosing += new FormClosingEventHandler(form_FormClosing);
        form.Show();
    }
     
    void form_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.HideControl();
    }
    This will open the new form, NOT as a modal dialog, and because there is a picturebox on top of the form you cannot click on any of the controls inside the main form while this dialog is open.

    It's just a basic example, but if you really want to, it should get you started.

    Have fun!

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top