This is in regards to C# coding, im quite new to this programming language, and do not know much about any other as well, but what i would like to achieve is to have a picture box as the background, and have another picture box over lapping it but the transparent part has to show the picture box behind. I have been able to have a transparent picture box, but the thing is it only shows the form's back color rather than the picture box behind it. can anyone help with this?
IN other words a picture box over a picture box, but able to see through the first picture box where it is clear, and see the picture box behind.
Thanks in advance.
Go to Project -> Add user control.
Give that User Control the BackGroundImage.
Drag your picturebox onto the the usercontrol. Make the PictureBox's Backcolor transparent.
Build the project.
In the designer, you should be able to drag your new usercontrol onto the form.
This will do what you want.
for background, you can use Graphics. build a paint event for your form visually:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(Image.FromFile("address of image"), 0, 0, this.Width, this.Height);
}
it will color the form and you don't need picturebox for background.
Related
Is it possible to detect a click on a picturebox that is not visible
I have tried this :
private void PictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("teste");
}
But even if the picture box has no image and that background color is transparent, it hides the elements behind it.
I explain myself a little better;
I do a tic-tac-toe and I have the image of the cross that is not visible when the game is started; as soon as I press on a delimited area (where I want to place the cross), I want it to become visible
How can I do that?
Thank you.
I would use the PictureBox with an ImageList containing 2 images: the first is the background of your game field and the second is the cross.
In your Click Event I would toggle the pictures on click then.
How can i move scrollbars of picturebox when a button is pressed?
I have flowLayoutPanel (Dock: none, AutoSize: false, AutoScroll: true) and i placed on it a PictureBox (Dock: none, SizeMode: AutoSize). I loaded a large image (9000x6315px) into PictureBox so scrollbars are visible and allow me to scroll map. But.. only with mouse. How can i scroll a PictureBox using a code, when a button is pressed?
Problem is BETTER visible in this video on youtube (duration 3mins) and will let you better understand what i mean:
https://youtu.be/3Haqzsyn_zE
In Embarcadero Rad Studio i could write something like this:
ScrollBox1->HorzScrollBar->Position=500;
ScrollBox1->VertScrollBar->Position=500;
Is it possible in VS?
Thank you!
Add a temporary button to your form, with this code (inserting the name of your FlowLayoutPanel):
Console.WriteLine(flowLayoutPanel1.AutoScrollPosition.ToString());
Scroll your zoomed map to the position you want it to be in and click the button.
Example output:
{X=-146,Y=-164}
Whatever those values are, you want to store the opposite of them. Repeat the process and write down all the positions you need.
Now you can set the AutoScrollPosition of your FlowLayoutPanel to any of those points, and it will scroll there. For instance, if that was the desired point for your "B1" button:
private void B1_Click(object sender, EventArgs e)
{
Point pt = new Point(146, 164);
flowLayoutPanel1.AutoScrollPosition = pt;
}
I'm trying to create an effect so when the user hovers the mouse over a picturebox, the button fades to a hoverimage, and when they leave, it fades back to the original. I'm using pictureboxes as buttons in a program. I'm doing this because all the buttons will be pictures with no button textures, so I didn't see the point in using a button. Just so you can visualize it, here is the original image:
And the image to fade to:
I could still change these images a bit, but thats the general idea.
How would I go about creating this fading effect? I'm picturing something using timers and opacity settings, but I don't know how any of that stuff could help me solve this.
E: Heres a bit of code I have. It changes from image to image when I hover, but its not a fade, and it looks very choppy.
private void pictureBox3_MouseEnter(object sender, EventArgs e)
{
pictureBox3.Image = pictureBox37.Image;
}
private void pictureBox3_MouseLeave(object sender, EventArgs e)
{
pictureBox3.Image = pictureBox38.Image;
}
pictureBox37 and pictureBox38 are invisible reference pictureboxes with the images I need.
I don't think there's any WinForms support for this kind of animation, so you'd have to do this manually. If you have more than one of these buttons, I recommend creating your own UserControl with this functionality. I can give you some pointers how to do this:
Your UserControl will have two Image properties, one for the normal and one for the hover image
You'll have to override the OnPaint method to do your own custom painting, blending these two images onto your Graphics according to a position property. This is a float where 0 indicates showing the normal image and 1 the hover image. Any value in between means a blend of these two images. I found some good blending code here.
Then you'll need a Timer to update this position dynamically and redraw the button (by calling Invalidate). I recommend using a Windows.Forms.Timer, which has the advantage that its Tick event is always executed on the main Thread, so you don't have to use Invoke to modify your control.
And last but not least you override the OnMouseEnter and OnMouseLeave methods, to set this all into motion. These methods could set a positionChange property that indicates in which direction position is changing.
Hope this helps ...
I've got a UserControl which I created myself, it is for display like PictureBox, but it displays my datas about something. So if no target thing to display things about it, it hasn't got any image to show. If this happens (always when I put my custom control on the form), the control can't be seen (only if I select it) because of no image to show. I want to do something like what the PictureBox does, in the editor when it hasn't got image to disaplay, it has a line border to "tell us" "here is a picture box on the form", but when I run the program and the PictureBox with no image, it hasn't got that border.
Image.
How can I detect this for my custom control?
You may use the property DesignMode to determine if your control is in design mode to draw the Rectangle around it, otherwise at runtime, the rectangle will not be drawn.
public class CustomControl : UserControl
{
protected override void OnPaint(PaintEventArgs e)
{
if (DesignMode){
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(0,0,ClientSize.Width-1, ClientSize.Height-1));
}
base.OnPaint(e);
}
}
Here's what I have in VERY simple easy to grasp terms.
My form background is Blue.
I created a gradient image from white to the Blue from the form background. This is to give the form a nice gradient look. I added a picturebox to my Form and set this image as the Image.
I added a picturebox with a Logo on top of the gradient Picturebox, but it's 'grabbing' the Form background color and not respecting the transparent background image I wanted it to grab.
So:
Blue Form -> Huge pictureBox with gradient -> Small picturebox thats supposed to respect the gradient.
Help please!
I think this might be as the PictureBox is not a control container. So this implies that when you drag the button picture box onto the main picture box, it is not actually a child of the picturebox, but rather of the form.
You would notice that if you were to do he same with a panel (set the form blue, panel background image, and place the button picture box control on the panel) it would show transparent to the panel control.
Why not rather set the Form BackgroundImage, avoid the Huge Picture Box, and set the small picture box on the form itself.