是否可以在窗体中拖动控件时做得像Windows一样,在按下鼠标拖动的时候只显示控件的虚框(原控件位置不变)(效果见下图),待确定位置松开鼠标后,原控件再移动目标位置。
解决方案 »
- ComboBox DropDownStyle 设置成simple回车会清空文本?
- IIS 浏览页面出现“/Web”应用程序中的服务器错误”。
- 打印机的问题 有点急 能帮下忙不
- 100分求解无法连接SQL数据库问题
- 讨论一下在C#下使用union的一些问题
- C#怎样实现webBrowser设置代理访问
- C#中如何设置打印预览的比例?
- 如何用FindControl取出DataGrid的HeaderTemplate中的控件
- 有志于自己做出点东西,又不满足于上班,希望自己创业,又没有合作团队的南京程序员进来!!!!!(其它地方也可以)!!!!
- 菜鸟问题 ,如果我想连结另一台机子上的数据库 连接字符串应如何写
- 怎样可以使编程后所生成的文件不可以删除?
- 那里有用户自定义控件下载???
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;
}
}