I am working on a little school project and I have to use Visual Studio C++/CLI with Windows Forms Application.
Since I am new to Windows Forms I am having some difficulties.
What I would like to achieve:
Press a button
A PictureBox will be created at starting position
A timer will move the PictureBoxto some given position
Pressing the button again will spawn a NEW PictureBox with a different name which will begin to move in the same direction as the first rectangle
and so on.
Note: These pictureboxes have to have a different background color which must be randomly chosen out of 3 colors.
What I need to know is....
I know how to move a picturebox, but how do I dynamically create one with a custom name and color after a button press?
Thanks in advance!
Because you mention this is a school project, here is some high-level advice:
You can draw on a form by either subscribing to the Paint event (https://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint%28v=vs.110%29.aspx) or overriding the OnPaint method (https://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint(v=vs.110).aspx). Take a look at the Graphics object in the EventArgs.
When the timer elapses, make adjustments to local variables (keeping track of the desired position of the Rectangle) and then call Invalidate() (https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invalidate(v=vs.110).aspx) to force the form to be redrawn.
I hope this helps you get pointed in the right direction.
Related
I need to implement a Snakes & Ladders board game for windows phone (8 or 8.1). There will be the classic snakes and ladders board map, but the graphics might need to change (frozen ladders if it is snowing outside, muddy spots on some of the tiles if it is raining etc)
I need a simple and easy way which will allow me to visualize the player moving through the tiles and ladders graphics etc changing. I have seen that in XAML grid if I set an image as background and then add controls in the grid (eg a button for each tile so I can visualize the player moving on each tile), the background image gets overwritten.
So, any ideas?
Ended up doing this via having a button represent the Player and changing the button's margin to move it through the board (represented by a background image). Was not a clean way but the job got done pretty well.
As for the board-graphics changing, i just had 4 different images with the board state and loaded which one i needed in each case. Also not clean, but again got the job done.
I think I must be missing something obvious, but I'm unable to find this after several hours of searching. Is there no way to use a PictureBox or other control to contain an image with partial transparent/alpha-blended pixels, and place that over another image and have the blending be based on the image under it?
For example, this produces the results I want:
Place a panel on a form.
Add an OnPaint handler.
In the OnPaint handler draw 1 PNG, then draw another PNG over it, using Graphics.DrawImage for both.
This does not:
Place a PictureBox on a form and set it to a PNG.
Place another PictureBox on the form and set it to a PNG.
Place the 2nd picture box over the first.
...even if the 2nd picture box is just empty and has a background color of Transparent, it still covers the picture below it.
I've read this stems from all winform controls being windows, so by nature they aren't transparent.
...but even the 15 year old platform I'm migrating from, Borland's VCL, had several windowless controls, so it's hard to imaging winforms doesn't at least have some easy solution?
My first example above is one answer, true, but that adds a lot of work when you can only use one big panel and draw all of your "controls" inside of it. Much nicer if you can have separate controls with separate mouse events/etc. Even if not an image control, and a control I have to draw myself, that would be fine, as long as I can just put one image in each control. In VCL they called this a "paint box", just a rectangle area you could place on a form and draw whatever you want on it. Has it's own mouse events, Bounds, etc. If you don't draw anything in it, it is like it's not even there (100% transparent) other than the fact it still gets mouse events, so can be used as a "hot spot" or "target" as well.
The PictureBox control supports transparency well, just set its BackColor property to Transparent. Which will make the pixels of its Parent visible as the background.
The rub is that the designer won't let you make the 2nd picture box a child of the 1st one. All you need is a wee bit of code in the constructor to re-parent it. And give it a new Location since that is relative from the parent. Like this:
public Form1() {
InitializeComponent();
pictureBox1.Controls.Add(pictureBox2);
pictureBox2.Location = new Point(0, 0);
pictureBox2.BackColor = Color.Transparent;
}
Don't hesitate to use OnPaint() btw.
Sorry, I just found this... once I decided to Google for "winforms transparent panel" instead of the searches I was doing before, the TransPictureBox example show seems to do exactly what I need:
Transparency Problem by Overlapped PictureBox's at C#
Looks like there are 2 parts to it:
Set WS_EX_TRANSPARENT for the window style
Override the "draw background" method (or optionally could probably make the control style Opaque).
How i can draw big graphics on form ? I generate graphic map on function MyForm_Paint()
But if its take a long time, graphic don't render, just white background with red cross
If you want to draw directly on the form, there are many tutorials and examples:
Code: Drawing Graphics on a Windows Form (Visual C#)
Techtopia: Drawing Graphics in C Sharp
Graphics in Visual C# .NET
Codeguru: Getting Graphics to stay on a Form (C#)
If you are just trying to set a form to use a particular image, you might consider placing a PictureBox and setting its .Image property to that of your generated graphic:
pictureBox1.Image = myBitmap;
There is also the form's .BackgroundImage property:
form1.BackgroundImage = myBitmap;
The white background with red cross means that the requested resource is unavailable or not in a recognized format.
Do you need to generate a new graphic every time the form is redrawn? If so, then the Paint event is fine, but that may be why things are taking a long time; i.e. lots of redraws whenever the form is invalidated. If the map doesn't need to change then one of the above suggestions would probably be better.
If redrawing the graphic is the intention, then it would be necessary to discuss how you are generating the graphic in order to diagnose the problem. Because of the red "X" problem you are having, it's possible the graphic is not in the correct format, so it may help to post some of that code for further assistance.
My aim is to get fine control "animation" when it is mouse-over-ed. For example, I have a "map" of controls (game map that represent different type of terrain), each of them is an image with trees/rocks/hills on the green grass or water (lake or see) image of blue/cyan color. When user point any image with mouse it should get shiny: either get more bright background or get a shiny border.
It is hard to say what exactly I want to have (either background change or border), I would like to try each of them and see what is the most appropriate for me.
I am going to have a custom control (MapTile) that will represent a map tile. I know how to catch MouseEnter/MouseLeave events, but not sure how to change control style and if it is a good idea to work with control style in CodeBehind, probably there are better XAML-based solutions.
Could you please help with a solution that provide few goals:
Goal1: Add highlighted border around the control (it will be squares/rectangles, or circles; use what is easier) on mouse enter, remove border on move leave;
Goal2: Change some properties of my CustomControl (for example, background color).
Thank you very much!
1. How to han
You might find it easiest to get hold of Expression Blend and use it to create a custom template for your control.
The Learn Expression Blend page would be a good place to start. Look for tutorials on customising buttons and this is the same sort of thing that you want to do.
You need to use an attached behavior on your control. You don't need to learn Blend for this.
Check this one as an example, but you can search the site for Mouse Over for other examples.
http://gallery.expression.microsoft.com/en-us/MouseOver3D
I have an app with a bunch of controls in it and I want to place a set of cross hairs on top of it. My first attack used a PictureBox and ran into this problem. The solution that fellow proposes, seems a bit... verbose for what I need.
Is there a simple way draw on top of my form? Note that I don't even need the drawing to be part of a control as it doesn't need to do anything but just be there.
This eventually worked. I had to play some games though because most of the controls I wanted to draw on were not where it expected them to be.
Also, it ran into issues when controls were moved; it failed to redraw and stuff moved with the underlying control. This was fixed by forcing invalidation from the move event for anything that might move.
Does a PictureBox with a transparent image have the same problem as a Panel with BackColor set to Transparent? I'm thinking you could have a PictureBox with the crosshair image in it and move that around, instead of drawing it yourself...