I wonder how I can code an application that allows the user to handle simple graphic objects like in a vector graphics program. As a starting point I would like to have a program which allows the user to draw some rectangles, select them with the mouse and move them around.
I have some base knowledge in WinForms but it seems that WPF is a better choice for this task (tell me if you think different. I wouldn't mind using a free graphics library for Winforms as alternative).
I think I know hot to draw a rectangle and how to find out which rectange was clicked by the user. But I don't know how to move the rectangle around with the mouse. Can you give me a hint? I had a look into animations where I could move the rectanle around programatically but I am not sure whether this is the right way to implement it for mouse control.
You might want to have a look into PathGeometry. Check this link.
Combined with a Canvas and some controls you can make a pretty nitty editor ;)
Hope this helps.
Related
Hello: I am trying to create an app which will display a moving sphere. App will vary speed and direction. I've tried Adobe Flash but cannot get it smooth. Smoothness is essential in this case. So I am trying C#.
Initially, I can see that this can be implemented by:
1) Creating a PictureBox of a sphere, and using a Timer, change its coordinates. or
2) Using the this.paint function to draw a filled circle, and somehow, with a timer, erasing and redrawing it.
Can someone recommend the best path to take? I'll have a main menu where the user will chose speed/direction/how many etc... and then simply show the "game window" with the moving spheres. Any guidance would be much appreciated.
This is to be displayed on a PC only.
Thanks
-Ed
I just answered a similar question here.
NOTE: Depending on your needs, it is possible to achieve smooth animations under winforms (under certain conditions) though you are responsible for everything. wpf provides an animation framework but wpf is perhaps a milestone harder.
It probably does not matter should you pursue winforms first or WPF. You arguably could learn the basics under winforms then move over to wpf. wpf may require you to learn quite a bit before you can do anything.
Summary
Essentially what this does is to create an offscreen bitmap that we will draw into first. It is the same size as the UserControl. The control's OnPaint calls DrawOffscreen passing in the Graphics that is attached to the offscreen bitmap. Here we loop around just rendering the tiles/sky that are visible and ignoring others so as to improve performance.
Once it's all done we zap the entire offscreen bitmap to the display in one operation. This serves to eliminate:
Flicker
Tearing effects (typically associated with lateral movement)
There is a Timer that is scheduled to update the positions of all the tiles based on the time since the last update. This allows for a more realistic movement and avoids speed-ups and slow-downs under load. Tiles are moved in the OnUpdate method.
If you note in the code for Timer1OnTick I call Invalidate(Bounds); after animating everything. This does not cause an immediate paint rather Windows will queue a paint operation to be done at a later time. Consecutive pending operations will be fused into one. This means that we can be animating positions more frequently than painting during heavy load. Animation mechanic is independent of paint. That's a good thing, you don't want to be waiting for paints to occur. xna does a similar thing
Please refer to my full SO answer complete with sample code
Here are a few hints to get you going:
First you will need to come to a decision about which platform to target: WPF or Winforms.
Then you should know what to move across what; a nice Bitmap or just a circle across an empty background or a Bitmap or a Form with controls on it.
In Winforms both your approaches will work, esp. if you set a circular region see here for an example of that. (The part in the fun comment!)
And yes, a Timer is the way to animate the sphere. Btw, a Panel or even a Label can display an Bitmap just as well as a PictureBox.
For smooth movements make sure to set the Form.Doublebuffered=true, if you move across a Form. If you move across any other control (except a PictureBox or a Label) you will need to subclass it to get access to the DoubleBuffered property!
It is often also a good idea to keep the Location of a moving item in a variable as a PointF and use floats for its speed because this way you can fine grain the speed and Location changes and also the Timer Intervals!
I am trying to design a small application that could draw simple objects like lines,ellipses,triangles, etc... with a mouse using OpenGL. There should be a GUI which user can interact with. I thought of design the UI with C#. But I have problems getting it to work with OpenGL library since I need to get mouse interaction to this.
Most of the available codes write the code and directly produces the output to a window when the program is run.
Is there any one that could give me a tip on how to achieve this?
With OpenGL, all the stuff to draw are outputed each frame. So if you plan to modify coordinates of an object, you have to use variables as coordinate while calling vertex3f(x,y,z). Update the values in your mouse event handler, and next frames, new values will be used.
I've given a task to create a picture puzzle using c#. I have to customize a control to act like a tile. what I have in my mind is to create label to act as a puzzle tile. I have no experience in customizing a label control.. If any one can help me with a tutorial for this task It would be a great help.
I'm using windows forms for this project and not WPF.
I've googled but couldn't find any suitable tutorial. Please help me on this.
Thank you.
below is how each piece should look like in the application! A tile class is used to represent each puzzle piece.
The only way to manipulate the design structure of Windows Form controls is through the GDI (Graphics Device Interface). The device context is accessed through System.Drawing. This will allow you to inherit or override a control and create/alter its appearance. I worked with the GDI for about a month. It is not something that's learned overnight. As a quick alternative, you may consider adding puzzle pictures to a button control and coding it up so that if button A is within xx pixels of button B's left edge then A&B background image = a connected picture. Or something along those lines.
http://www.codeproject.com/Articles/6913/Creating-a-professional-looking-GDI-drawn-custom-c
I want to programatically draw a few simple 2D shapes in a WPF application. Which control should I use for that? I've heard that Canvas can do that but that it's mostly designed to be a container for other controls and not a "drawing" canvas.
Those other controls could be shapes like rectangles, lines, etc., so Canvas may be a good start. If you want to render directly through a function, you could basically use any (user)control as base class, though.
Canvas is meant for drawing, obviously. What is your concern ? if it is Speed, you should have a look on Windows XNA.
Maybe Direct X, Open GL or any library providing access to them could be of some use. Give more details on your needs for an answer.
Can someone point me to a C# open source implementaion with a simple image animations.
e.g. I feed the input image to animator, and the animation code produces a few dozen of images which if displayed sequentially looks like animation.
I am not something extremely fancy - a simple DirectX filter like animations would do.
You would be look for a sprite then? Microsoft has tutorials on this including:
http://msevents.microsoft.com/cui/WebCastEventDetails.aspx?culture=en-US&EventID=1032273446&CountryCode=US
and general graphics in C# here:
http://www.microsoft.com/events/series/msdnvisualcsharp.aspx?tab=webcasts
Hmmm... I'm not sure if this is what you want, but I've have created a library called Transitions that lets you animate most properties of UI controls easily. You could use it to move the position of pictures, transition between pictures or grow and shrink pictures. But I'm not sure that this is exactly the effects you're after? Anyway, if it's any help, my library is here:
http://code.google.com/p/dot-net-transitions/