C# 悬停在windows窗体图形中工作不正常
这是我的密码C# 悬停在windows窗体图形中工作不正常,c#,winforms,gdi,C#,Winforms,Gdi,这是我的密码 private bool isMouseOverPin(int x, int y, Pin pin) { int left, top, right, bottom,size; size=5; left = pin.theRectangle.Left-size; right = pin.theRectangle.Right + size; top = pin.theRectan
private bool isMouseOverPin(int x, int y, Pin pin)
{
int left, top, right, bottom,size;
size=5;
left = pin.theRectangle.Left-size;
right = pin.theRectangle.Right + size;
top = pin.theRectangle.Top - size;
bottom = pin.theRectangle.Bottom + size;
if (x >= left && y >= top && x <= right && y <= bottom)
{
return true;
}
else
{
return false;
}
}
private void pinHover()
{
for (int i = 0; i < pins.Count; i++)
{
if (isMouseOverPin(Cursor.Position.X, Cursor.Position.Y, pins[i]) == true)
{
using (Graphics gr=canvas.CreateGraphics())
{
gr.DrawRectangle(10,10,10,10);
}
}
}
}
private void canvas_MouseMove(object sender, MouseEventArgs e)
{
pinHover();
}
private bool isMouseOverPin(int x,int y,Pin-Pin)
{
int左、上、右、下、大小;
尺寸=5;
左=销。矩形。左尺寸;
右=销。直角。右+尺寸;
top=pin.theRectangle.top-尺寸;
底部=销。矩形。底部+尺寸;
如果(x>=left&&y>=top&&x我怀疑您的PIN位置是相对于画布而不是屏幕的
您应该将e.Location从MouseEventArgs传递到pinHover,或者您可以使用PointToClient
e、 g
这将做同样的事情。什么是canvas
?顺便说一句.NET使用gdi+
而不是gdi
@AlvinWong更准确地说,图形类提供了对gdi+的访问,但是.NET也使用gdi[例如:TextRenderer.DrawText()].它可以工作,但当我向下或向上滚动时,它就停止工作了。你能告诉我是什么原因吗reason@user1774204当您滚动面板时?可能当您在面板上移动另一个窗口时…如果我的猜测是正确的,您需要缓冲绘图矩形
,并在绘制
事件中处理它,而不是在鼠标移动
事件,在MouseMove事件中,您将调用Invalidate()。我已经在使用缓冲区(位图)进行绘制!但此问题仍然存在!plzhelp@user1774204我做了一些编辑(尚未测试,因此可能无法编译)。您应该将绘制代码移动到面板上的绘制事件。重新阅读您的评论后,我认为您的意思是,无论滚动,您都希望矩形保持在该位置,在这种情况下,您需要根据滚动值调整矩形位置。我也包括了该内容。如果您还有任何问题,请询问新的问题斯蒂昂。
List<Int32> DrawPinRects;
private void initBuffer()
{
DrawPinRects = new List<Int32>();
}
private void pinHover(Point Position)
{
DrawPinRects.Clear();
for (int i = 0; i < pins.Count; i++)
{
if (isMouseOverPin(Position.X, Position.Y, pins[i]) == true)
{
DrawPinRects.Add(i);
}
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
pinHover(e.Location);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
foreach(Int32 index in DrawPinRects)
{
Int32 y = panel1.VerticalScroll.Value + 10;
e.Graphics.DrawRectangle(Pens.Black, 10, y, 10, 10);
}
}
int left, top, right, bottom,size;
size=5;
left = pin.theRectangle.Left-size;
right = pin.theRectangle.Right + size;
top = pin.theRectangle.Top - size;
bottom = pin.theRectangle.Bottom + size;
if (x >= left && y >= top && x <= right && y <= bottom)
int size = 5;
Rectangle TestRect = pin.theRectangle;
TestRect.Inflate(size,size);
if (TestRect.Contains(x,y))