在表单控件之上创建图形 [英] Creating graphics on top of form controls

查看:52
本文介绍了在表单控件之上创建图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含加载/等待组件的表单,在等待期间,我希望将整个表单及其所有内容都变暗,并在其上方显示一个指示器.使用下面的代码,我可以轻松创建具有所需透明度的矩形,但是创建图形显示在所有控件的下方,而不是上方.有没有办法覆盖表格上的所有内容?

谢谢.

Hi, i have created a form that has a loading / waiting component to it and during the waiting period i wish to dim the entire form with all of its contents and display an indicator over the top of that. Using the code below i can easily create a rectangle with required transparency however the create graphic appears below all controls and not over the top. Is there a way to cover everything on the form?

Thanks.

<身体>
私有 无效 btnDraw_Click( 对象 发件人,EventArgs e)
{
int x = 15;
int y = 15;
int width = 360;
int height = 140;
图形g = this .CreateGraphics( );
solidBrushbrush = new SolidBrush(Color.FromArgb(120,0,0,0 ));
g.FillRectangle(画笔,x,y,宽度,高度);
        private void btnDraw_Click(object sender, EventArgs e)  
        {  
            int x = 15;  
            int y = 15;  
            int width = 360;  
            int height = 140;  
 
            Graphics g = this.CreateGraphics();  
            SolidBrush brush = new SolidBrush(Color.FromArgb(120, 0, 0, 0));  
            g.FillRectangle(brush, x, y, width, height);  
              
        } 

推荐答案

如以下代码所示,使用PictureBox而不是Rectangle来完成您的任务.

使用System;使用System.Collections.Generic;使用System.ComponentModel;使用System.Data;使用System.绘图;
使用System.Text;
使用System.Windows.Forms;
命名空间WinApp.Dim
{
公用部分类Form1:Form
{ public Form1()
{{InitializeComponent();
}

私有void cmdClick_Click(对象发件人,EventArgs e)
pictureRect.Visible = true;
pictureRect.BringToFront();

_timer.Enabled = true; ();
_dateTime = DateTime.Now;
}

void _timer_Tick(对象发送者,EventArgs e) > = 7)
{R
pictureRect.SendToBack();
pictureRect.Visible = false;
_timer.Stop();
_timer.Tick-= _timer_Tick
private DateTime _dateTime = new DateTime();

private #region Design
private System.ComponentModel.IContainer组件= null; ///清理所有正在使用的资源.否则为false. Dispose();
}
base.Dispose(dispose);
区域Windows窗体设计器生成的代码




>////Designer支持的必需方法-请勿使用代码编辑器修改此方法的内容> {{br> this.cmdClick = new System.Windows.Forms.Button();
this.pictureRect = new System.Windows.Forms.PictureBox(); this.pictureRect)).BeginInit();
this.SuspendLayout(); ing.Point(113,116);
this.cmdClick.Name ="cmdClick";
this.cmdClick.Size = new System.Drawing.Size(75,23);
this.cmdClick .TabIndex = 0;
this.cmdClick.Text ="Click";
this.cmdClick.UseVisualStyleBackColor = true;
this.cmdClick.Click + = new System.EventHandler().
////
//pictureRect
////
this.pictureRect.Location = new System.Drawing.Point(12,12);
Rectalize =图片=>. ;
this.pictureRect.Size = new System.Drawing.Size(268,249);
this.pictureRect.TabIndex = 1;
this.pictureRect.TabStop = false; .Visible = false;
////
//Form1
//this.AutoScaleDimensions =新的System.Drawing.SizeF(6F,13F);此=. Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292,273);
this.Controls.Add(this.cmdClick);
this.Controls.Add( this.PictureRect);
this.Name ="Form1";
the this.Text ="Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureRect) ;
this.ResumeLayout(false);

#endregion

私有System.Windows.Forms.Button cmdClick;
私有System.Windows. Forms.PictureBox pictureRect;
#endregion Design

}
}


You can use PictureBox instead of Rectangle to acheive ur task as in this code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinApp.Dim
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmdClick_Click(object sender, EventArgs e)
        {
            pictureRect.BackColor = Color.Wheat;
            pictureRect.Visible = true;
            pictureRect.BringToFront();

            _timer.Enabled = true;
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Start();
            _dateTime = DateTime.Now;
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.Subtract(_dateTime).Seconds >= 7)
            {
                pictureRect.SendToBack();
                pictureRect.Visible = false;
                _timer.Stop();
                _timer.Tick -= _timer_Tick;
            }
        }

        private Timer _timer = new Timer();
        private DateTime _dateTime = new DateTime();

        #region Design
        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);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.cmdClick = new System.Windows.Forms.Button();
            this.pictureRect = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureRect)).BeginInit();
            this.SuspendLayout();
            //
            // cmdClick
            //
            this.cmdClick.Location = new System.Drawing.Point(113, 116);
            this.cmdClick.Name = "cmdClick";
            this.cmdClick.Size = new System.Drawing.Size(75, 23);
            this.cmdClick.TabIndex = 0;
            this.cmdClick.Text = "Click";
            this.cmdClick.UseVisualStyleBackColor = true;
            this.cmdClick.Click += new System.EventHandler(this.cmdClick_Click);
            //
            // pictureRect
            //
            this.pictureRect.Location = new System.Drawing.Point(12, 12);
            this.pictureRect.Name = "pictureRect";
            this.pictureRect.Size = new System.Drawing.Size(268, 249);
            this.pictureRect.TabIndex = 1;
            this.pictureRect.TabStop = false;
            this.pictureRect.Visible = false;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.cmdClick);
            this.Controls.Add(this.pictureRect);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.pictureRect)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button cmdClick;
        private System.Windows.Forms.PictureBox pictureRect;
        #endregion Design

    }
}



这篇关于在表单控件之上创建图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆