C# Picturebox leaves a white trail when moved - c#

I'm using a timer to move a PictureBox inside a form. The picturebox leaves a white trail behind it which disappears when the picturebox stops. Is there any way to get rid of the white trail? I've tried this.Invalidate(); which removed the trail but caused the whole form to flimmer until the picturebox stopped.
If anyone could help me solve this problem I would be grateful!

It's generally much more difficult to do animations in WinForms than in WPF, because WinForms was not designed to support animations.
Have a look at the open source library Dot Net Transitions that provides some animation functionality to WinForms
The Transitions library lets you create animated transitions of any properties of user-interface elements for .NET. It provides an easy way to perform UI animations in .NET in a similar way to the Core Animation library for Apple and the iPhone.
http://code.google.com/p/dot-net-transitions/

Related

Transparent picturebox over another transparent picturebox c#

I'm having this problem. I would like for the frog's background to be transparent so the log underneath can be seen, but instead, the background picturebox's color is shown.
problem
I googled the problem and got the suggestion to change the Parent of the picturebox, which i tried, but this happened:
it works, but only in the log's picturebox area
I'm clearly doing something wrong. Can somebody help?
WinForms does not support true alpha-transparency. "Transparent" controls re-render only the parent form's background behind themselves - controls cannot be truely layered and blended in the Z-axis.
So what you're after really isn't possible.
However, implementing a game using WinForms controls and GDI (System.Drawing) is a bad idea - performance is terrible (in part due to Microsoft's crippling of GDI since Windows Vista by removing hardware acceleration of many operations) and there are endless issues with inconsistent double-buffering and DPI-scaling.
I suggest you rewrite your game's rendering layer in Unity, Direct2D or SDL.

How to create a drawing in a window in WPF

I've decided to learn a bit of WPF and I've created an application with the Mahapps Metro library and it interacts with a SQLite database (Unrelated but a bit of background).
I'd like to draw an object, let's say a triangle, in a new window.
I've seen this - Click - but the drawing of the shape needs to be visible to the user. So the user will see the line being drawn from point A to B to C to A. The image will "reload" after a few seconds i.e. Clearing the window/canvas and redrawing the triangle.
Are they any libraries out there that might make this easier or does WPF have something else I can use to achieve this?
Also, the redrawing of the triangle will be in a separate thread running a loop. Something tells me this isn't going to be very efficient. Is there a better way initiate a "redraw"?
My "answer" are some helpful searches and a few results that might help you get to the next step of deciding on a design that works for you.
Yes, WPF does have facilities to help you achieve some animation in drawing lines.
I searched for "wpf animate line drawing" and some interesting links for your research are:
How do you animate a line on a canvas in C#?
generating animated line
Drawing line "slowly" programmatically with wpf
Hopefully this gets you going in a good direction. Best of luck with your project.

Embed button control into existing Direct3D application

I would like to overlay own content above a Direct3D v9 game (made by a third party).
Overlay Interactive Button
Specifically, I would like to overlay a clickable button control, like Steam does, for instance, though I'm attempting a much simpler interface.
Ideally, I would be able to overlay a WPF button or a Windows Form button or whole UserControl, but if that is not possible then creating a functioning button from primitives would suffice as well.
Text Overlay Working with SharpDX
I have a working sample of overlaying text based on the work of Justin Stenning
Namely with the help of SharpDX.Direct3D9.Font DrawText method
How to Overlay a Button or UserControl with SharpDX
I looked through relevant samples in https://github.com/sharpdx/SharpDX-Samples but was not able to find a way to include or draw native controls with SharpDX
Is it possible?
If not, are there any samples of drawing a clickable button from scratch?
Please bear in mind I'm a novice in the DirectX world :)
Your biggest problem isn't going to be getting the controls to render on top of a DX scene; it's going to be getting them to respond to input afterwards. You can probably rig up a way to get the visuals copied to a DX surface, but I have no idea how you would capture input, translate it, and deliver it back to the WPF components. If it's even possible, it's almost certainly more trouble than it's worth.
There are a couple game-oriented Xaml solutions out there that you may want to check out. WPF for Games is a partial C++ implementation of WPF based on Direct3D, and there's also the proprietary Noesis GUI.

Game Development - Avoiding Flickers

I am developing a tetris like game in winforms(C# and .netframework 2.0).
The winform has an background image and a picture-box which move down(new Location is assigned) at an interval of 500ms.
The problem is when picture-box moves down the background image of form flickers at the point where the picture-box was earlier located. If i don't use any background image, then there is no flickering.
Is there any graphics accelerator or any kind of solution using which the flickering problem can be solved.
The term used for this is Double Buffering. The idea is simple - start with 2 panes but only display one. Draw to a hidden pane, and quickly swap the hidden and visible panes. Rinse and repeat for every transition (animation).
Luckily, you dont have to deal with the nuts and bolts of this in .NET, controls do it for you. This SO question will help you: How to double buffer .NET controls on a form?
Set DoubleBuffered = true on your controls. This should help prevent flickering.
For documentation on the DoubleBuffered property, see here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx
If you are developing a game in Windows Forms you really ought to be overriding OnPaint and implementing painting of the sprites on every frame, as opposed to moving heavyweight PictureBox controls.
You will find you may get the flicker regardless of DoubleBuffer if you use the approach you mentioned. However, with all drawing done in OnPaint, then DoubleBuffer begins to work.
I did a quick search and found this interesting article on creating a game-loop in Windows Forms using OnPaint override. In particular take a look at GameStateDrawer which does the rendering directly to graphics context.

Avoiding game loop in XNA 4.0

I developing a simple 3D model viewer on XNA 4.0. Is there a way to avoid a infinite game loop with Draw and Update functions? I need render 3D graphics, but without infinite rendering of scene. I mean I need redraw the scene only then it really changed.
I suggest taking a look at how they're doing it in their samples:
App Hub - WinForms Series 1
App Hub - WinForms Series 2
Why exactly do you want this?
If you use a winforms application you have more control on your update and draw loop, but you also could render the scene to a texture first, and then stop rendering at all.
You are going to have to roll your own multi-threaded loop; if you stall either Update or Draw the other will get stalled too. Another consideration is that if your window gets obscured somehow (under non-DWM environments; a window overlapping it for instance) stalling the Draw will leave areas of your window unpainted.
If you want to go ahead with this; what you will do is start a Thread the first time Update gets called and subsequently use a ManualResetEvent to synchronise the two.
A far more robust option is to render your entire scene to a RenderTarget2D when the scene is updated; if the scene has not been altered since the last Update or Draw call simply render the RenderTarget2D instead of the whole scene. This is still an infinite loop, but it tends toward your requirement (even though it does not meet it).
This sort of scenario would be best solved by using WinForms or WPF as the host for your rendering system. I have worked with such systems before and I found using SlimDX with WPF Interop was my preferred solution. Using this approach allows for the addition of nice UI features provided by WPF, overlain on a 3D model rendered to a surface.

Categories

Resources