![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
C#中拖动无标题栏的窗体时,窗体在不停的闪烁怎么办?
我在测试网上贴出的实现无标题栏窗口拖动代码的时候发现窗体在拖动的时候不停的闪烁,这个问题大家也有吗?是怎么解决的?我的代码如下:usingSystem;usingSyst...
我在测试网上贴出的实现无标题栏窗口拖动代码的时候发现窗体在拖动的时候不停的闪烁,这个问题大家也有吗?是怎么解决的? 我的代码如下:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Contact {
public partial class FrmMain : Form {
private Point relativePosition = new Point {
X = 0, Y = 0
};
public FrmMain() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
Application.Exit();
}
private void FrmMain_MouseDown(object sender, MouseEventArgs e) {
if(e.Button == MouseButtons.Left) {
this.relativePosition.X = this.Left - e.X;
this.relativePosition.Y = this.Top - e.Y;
}
}
private void FrmMain_MouseUp(object sender, MouseEventArgs e) {
if(e.Button == MouseButtons.Left) {
this.Location = new Point(
this.relativePosition.X + e.X, this.relativePosition.Y + e.Y);
}
}
private void FrmMain_MouseMove(object sender, MouseEventArgs e) {
if(e.Button == MouseButtons.Left) {
this.Location = new Point(
this.relativePosition.X + e.X, this.relativePosition.Y + e.Y);
}
}
}
} 展开
using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Contact {
public partial class FrmMain : Form {
private Point relativePosition = new Point {
X = 0, Y = 0
};
public FrmMain() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
Application.Exit();
}
private void FrmMain_MouseDown(object sender, MouseEventArgs e) {
if(e.Button == MouseButtons.Left) {
this.relativePosition.X = this.Left - e.X;
this.relativePosition.Y = this.Top - e.Y;
}
}
private void FrmMain_MouseUp(object sender, MouseEventArgs e) {
if(e.Button == MouseButtons.Left) {
this.Location = new Point(
this.relativePosition.X + e.X, this.relativePosition.Y + e.Y);
}
}
private void FrmMain_MouseMove(object sender, MouseEventArgs e) {
if(e.Button == MouseButtons.Left) {
this.Location = new Point(
this.relativePosition.X + e.X, this.relativePosition.Y + e.Y);
}
}
}
} 展开
4个回答
展开全部
闪是因为你的代码有问题,改成这样就行了
private Point start = Point.Empty;
private void FrmMain_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
start = MousePosition;
}
}
private void FrmMain_MouseUp(object sender, MouseEventArgs e)
{
start = Point.Empty;
}
private void FrmMain_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && start != Point.Empty)
{
this.Left += MousePosition.X - start.X;
this.Top += MousePosition.Y - start.Y;
start = MousePosition;
}
}
private Point start = Point.Empty;
private void FrmMain_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
start = MousePosition;
}
}
private void FrmMain_MouseUp(object sender, MouseEventArgs e)
{
start = Point.Empty;
}
private void FrmMain_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && start != Point.Empty)
{
this.Left += MousePosition.X - start.X;
this.Top += MousePosition.Y - start.Y;
start = MousePosition;
}
}
展开全部
原因很简单:你的显卡不是很强大。因为拖动窗体的时候,要重置窗体的坐标,会触发OnPaint事件,需要重绘窗口和所有控件,需要大量的图像计算。显卡GPU不好的话会卡的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参考资料: http://hi.baidu.com/xiaohuaduo8/blog/item/ebc943afd8d4b6f3faed50cb.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
小花朵8
受教了,谢谢
受教了,谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询