How to Cloud Forms in C#

Asked

Viewed 96 times

0

I would like to know how I can cloud a (main) form, after calling another form or Messagebox.show(), to force the user to pay attention only to the active form or message.

inserir a descrição da imagem aqui

I have the following code inside a button:

    private void KpbRecibo_Click(object sender, EventArgs e)
    {
        kpbRecibo.Enabled = false;
        using (FrmRecibos frmRecibos = new FrmRecibos())
        {
            _ = frmRecibos.ShowDialog();
        }
        kpbRecibo.Enabled = true;
    }

I checked several posts spread over the internet and saw some who talked about it, but whenever I tested the codes I received an error message.

Code tested:

        using (FrmRecibos frmRecibos = new FrmRecibos())
        {
            frmRecibos.Owner = this;

            BlurEffect myBlur = new BlurEffect();
            myBlur.Radius = 5; 
            this.Effect = myBlur;


            frmRecibos.ShowDialog();
        }

I’m using :

using System.Windows.Media.Effects;

Together with the reference: Presentationcore.

But I always get a line error:

this.Effect = myBlur;

I would like to know if there is any way you can help me with this problem. I would like to thank you all.

P.S.: As I said before, I found several posts and articles on the subject, but, are not very enlightening, because I believe that the explanations are for those who have a very high level of knowledge, which is clearly not my case.

  • Forms I know well, but this link should help: https://stackoverflow.com/questions/34947504/how-to-show-a-pop-up-message-with-dark-background

  • In the image you have already gone through this question (https://stackoverflow.com/questions/17248748/c-sharp-dialog-form-with-blur-background) of the OS right? The answer in her didn’t help you?

  • Using System.Windows.Media.Effects you achieve the same result more reliably and simply using only 3 lines. The problem is precisely what I had said earlier, the line I mentioned just above simply presents an error. I’ve seen that this method works, I just don’t know why my code is showing error.

1 answer

0

You can use another form that opens behind the message, follow an example I use in my projects:

public partial class FormShadow : Form
{
    //Form Pai onde está o controle a ser exibido
    public Form FormPai { get; set; }


    /// <summary>
    /// Form de destaque de controles e exibição de dicas
    /// </summary>
    /// <param name="_parent">Form que será escurecido</param>
    /// <param name="_control">Controle que será destacado</param>
    /// <param name="_message">Mensagem de dica</param>
    /// <param name="_opacity">Transparencia do Form, 0=Invisivel, 1=Visivel, Recomendado 0.5</param>
    public FormShadow(Form _parent, double _opacity = 0.5)
    {
        InitializeComponent();
        //this.Parent = _parent.Parent;
        //Define a chave de transparencia do form
        this.TransparencyKey = Color.Magenta;

        //Define a cor do sombreamento do form
        this.AllowTransparency = true;
        this.BackColor = Color.Black;

        //Atribuição dos valores passados por parametro
        this.FormPai = _parent;
        this.Opacity = _opacity;

        //Define visual do form
        this.StartPosition = FormStartPosition.Manual;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.ShowInTaskbar = false;

        if (this.FormPai != null)
        {
            this.Size = this.FormPai.Size;
            this.Bounds = this.FormPai.Bounds;
            this.Location = FormPai.PointToScreen(Point.Empty);
        }
        else
        {
            var rect = Screen.GetBounds(Point.Empty);
            this.Size = rect.Size;
            this.Bounds = rect;
            this.Location = Point.Empty;

        }
        //Fechar com ESC
        this.KeyPreview = true;
        this.KeyDown += FormHighlightControl_KeyDown;
    }

    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        if (System.Diagnostics.Debugger.IsAttached) //Se em modo debug, não mostra em full screen pq atrapalha acessar o visual studio
        {
            this.Size = new Size(50, 50);
        }
    }

    public static FormShadow Show(Form _parent, double _opacity = 0.5)
    {
        FormShadow instancia = new FormShadow(_parent, _opacity);
        instancia.Show();
        return instancia;
    }

    public static DialogResult ShowMessageBox(Form _parent, string title, string message, MessageBoxButtons btns = MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.Information, MessageBoxDefaultButton dbtn = MessageBoxDefaultButton.Button1, double _opacity = 0.5)
    {
        DialogResult d;
        using (FormShadow instancia = new FormShadow(_parent, _opacity))
        {
            instancia.Show();
            d = MessageBox.Show(new Form() { TopMost = true }, message, title, btns, icon);
            instancia.Close();
        }
        return d;
    }

    //Fechar com ESC
    void FormHighlightControl_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Escape)
        {
            this.Close();
        }
    }
}

Then it’s easy to show a Messagebox:

private void button1_Click(object sender, EventArgs e)
{
    FormShadow.ShowMessageBox(this, "Teste de mensagem", "Mensagem de texto");
}

Upshot:

inserir a descrição da imagem aqui

https://github.com/rovannlinhalis/WindowsFormsShadow

Note the override of the Onshown Method, it is set so that when running in the visual studio, it shows only one block of 50x50, because the form can disturb access to the visual while debugging.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.