c# - Drawn objects not drawing in correct position -
i trying make paint program except have major problem, want draw square click mouse, except shape not drawing in right place, here code:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace paint_program { public partial class form1 : form { int mousex; int mousey; public form1() { initializecomponent(); } private void form1_mouseclick(object sender, mouseeventargs e) { mousex = cursor.position.x; mousey = cursor.position.y; system.drawing.solidbrush mybrush = new system.drawing.solidbrush(system.drawing.color.red); system.drawing.graphics formgraphics; formgraphics = this.creategraphics(); formgraphics.fillrectangle(mybrush, new rectangle(mousex, mousey, 20, 20)); mybrush.dispose(); formgraphics.dispose(); } private void form1_load(object sender, eventargs e) { } private void rectangleshape1_click(object sender, eventargs e) { } private void test_click(object sender, eventargs e) { //testing console.writeline("x:" + mousex); console.writeline("y: " + mousey); } } }
i know how solve problem, , happy if 1 me :d
in code using coursor.position
property provides cursor position in screen coordinates:
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position(v=vs.110).aspx
what interested in coordinates of cursor relative window. in event handler can access using mouseeventargs e
method parameter new rectangle(e.x, e.y, 20, 20)
below modified version of form1_mouseclick
event handler assume works expected:
private void form1_mouseclick(object sender, mouseeventargs e) { mousex = cursor.position.x; mousey = cursor.position.y; system.drawing.solidbrush mybrush = new system.drawing.solidbrush(system.drawing.color.red); system.drawing.graphics formgraphics; formgraphics = this.creategraphics(); formgraphics.fillrectangle(mybrush, new rectangle(e.x, e.y, 20, 20)); mybrush.dispose(); formgraphics.dispose(); }
Comments
Post a Comment