---
title: "c# 拖动控件闪烁"
author: "starnc"
date: "2012-11-06T14:41:28Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2012-11-06T14:42:31Z"
id: 1717489
url: "https://starnc.iteye.com/blog/1717489"
---

最近几天在做画线框的工作，类似于截屏之后修改截屏大小的那种，做完之后发现拖拉线框的时候，线框不停的闪烁，再

 

试了双缓冲技术仍然无果的情况的下，果断觉得是鼠标定位的问题，遂做了如下修改，果断不闪了。\\(^o^)/

 

代码修改如下：

 

在pnl\_MouseMove(object sender, MouseEventArgs e)事件里

 

e.Location.X 改成 Cursor.Position.X            e.Location.Y 改成 Cursor.Position.Y

 

如果你也像我一样一直闪烁，不妨试试这个改动吧，也许会有效果。

 

 

 

## 注：MousEventArgs.Location 和 Cursor.Position的区别

**MouseEventArgs.Position**is the mouse cursor position*relative to the control during the event*,**at the time of the event**.

**MouseEventArgs.Position**就是说当控件的事件发生的时候，鼠标在控件上的位置。

**Cursor.Position**is the**current**mouse cursor position*relative to the desktop*.

**Cursor.Position**就是当前鼠标在桌面上的位置，不需要在控件发生时取到，适合在任何时候获得鼠标的位置