拖动控件时只显示虚框

        因为之前在写拖动控件的时候,一直是让该控件处理显示状态。如果是容器控件有使用背景图或拖动的控件本身有背景,在拖动时会占用比较多的系统资源,在界面上甚至会出现闪烁的问题。后来有网友介绍一种方法,大致的思路是,开始拖动(MouseDown)控件前时把这个控件隐藏掉,在拖动过程中(MouseMove)通容器控件(Form或Panel之类的控件)的Paint事件描绘一个虚框,到了目标位置后,释放鼠标时(MouseUp)再把控件显示出来。(简单实现的代码如下:)

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

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.button1.MouseDown += new MouseEventHandler(button1_MouseDown);
            this.button1.MouseUp += new MouseEventHandler(button1_MouseUp);
            this.button1.MouseMove += new MouseEventHandler(button1_MouseMove);
        }

        protected int x, y;
        protected bool movingFlag = false;
        void button1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;

            movingFlag = true;
            x = e.X;
            y = e.Y;

        }

        void button1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;

            movingFlag = false;
            this.button1.Visible = true;
        }

        void button1_MouseMove(object sender, MouseEventArgs e)
        {
            if (movingFlag == false) return;

            this.button1.Visible = false;
            this.button1.Location = new Point(button1.Location.X + (e.X - x), button1.Location.Y + (e.Y - y));
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (movingFlag == false) return;
            Graphics g = e.Graphics;
            Pen p = new Pen(SystemColors.Highlight, 1);
            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

            g.DrawRectangle(p, button1.Location.X, button1.Location.Y, button1.Width, button1.Height);
        }
  /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(12, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(162, 56);
            this.button1.TabIndex = 0;
            this.button1.Text = "&Drop Me!";
            this.button1.UseVisualStyleBackColor = true;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(528, 460);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值