移动控件中的后续问题 [英] Follow-up Question in Movable Controls

查看:37
本文介绍了移动控件中的后续问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!上次我问一个问题:文本框和按钮是否可以在用户控件中移动,而Abhinav S.发布了一个有用的链接.

现在我的文本框和按钮是可移动的,我想知道我使用paint事件创建的行是否也可以移动.

帮帮我,谢谢! :}

解决方案

只有一种.
当您在Paint事件中绘制一条线时,它实际上并没有像控件一样存在":除非您重新绘制它,否则它将在下次绘制该控件的操作失效时消失.如果每次将其绘制在不同的位置,则它似乎会移动.

尝试一下:设置一个计时器,该计时器每次将整数向上移动10,将其环绕100,并将其用作绘制线条的Y坐标.调用控件的Invalidate方法,您将看到行的移动. />
现在,当发生MouseDown时,检查单击是否为现有线(创建它们时必须保留对线坐标的引用).如果是的话,就在直线上指出这一点,不要重新粉刷它.在MouseMove中获取坐标,并弄清楚应该对直线的原始起点和终点进行的更改.这应该为您提供新的起点和终点.用它们重画.

假设您单击了(x,y)处的行.在MouseMove中再次检查坐标,例如(x + 3,y-4).现在,如果原始起点和终点分别是(origStartX,origStartY)和(origEndX,origEndY),它们也将更改为(origStartX + 3,origStartY-4)和(origEndX + 3,origEndY-4).使用这些点画线.

在MouseUp中,您将在最后一次执行相同的重新计算.完成后,更新参考并将原始点的值更改为新点.


Hi! Last time I asked a question whether a textbox and buttons can be movable inside user control and Abhinav S. posted a useful link.

Now that my textbox and buttons are movable, I would like to know if the line I created using paint event can be movable too.

Help me guys, Thanks! :}

解决方案

Only sort of.
When you draw a line in the Paint event, it doesn''t really "exist" in the same way that a control does: it will disappear the next time the control it was drawn in is invalidated, unless you re-draw it. If you draw it in a different position each time, then it will appear to move.

Try it: Set up a timer, which moves an integer up by 10 each time, wrapping at 100 and use that as the Y coordinate at which you draw your line. Call the controls Invalidate method, and you will see the line move.


Since you have already drawn the line, I assume you have handled MouseDown, MouseMove and MouseUp events for the container.

Now, when the MouseDown happens, check if the click is one a existing line (you will have to keep a reference to the line coordinates as an when you create them). If yes, get that very point on the line and do not repaint it. Get the coordinates in the MouseMove and figure out the changes that should happen to the original start and the end points of the line. This should give you the new start and end points. Use them to redraw.

Suppose you have clicked on the line at (x,y). In MouseMove check the cooridnated again, say (x + 3,y-4). Now if the original start and end points were (origStartX,origStartY) and (origEndX, origEndY), they too will change to (origStartX + 3,origStartY - 4) and (origEndX + 3, origEndY - 4). Draw the line using these points.

In MouseUp, you will do the same recalculation for the last time. Once it is done, update the references and change the values for the original points to the new point.


这篇关于移动控件中的后续问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆