I have a Panel as a container this panel has a picture on it as a background, within the container panel, I have another panel where I gonna put some information in labels, that information will change in time, what I want is a transition when a new info is about to show, fade out the information panel with the old info and then fade in the same panel with the new info. At the time of the fade out the information panel I will be able to see the backgroud image of the container panel. Both panels have BorderStyle=FixedSingle, also the info panel has a backcolor color.
Now my question is: is there any way to fade in/out the information panel and the whole content within too?
I was searching in the web, and I found an approach to this effect working with the panel's backcolor but it doesn't work at all, and besides, the content still there, since they just try to fade the backcolor property:
Timer tm = new Timer();
private void Form1_Shown(object sender, EventArgs e)
{
tm.Interval = 100;
tm.Tick += new EventHandler(timer1_Tick);
tm.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
int aa = 0;
panel2.BackColor = Color.FromArgb(aa, 255, 0, 0);
aa += 10;
if (aa > 255)
tm.Enabled = false;
}
Any help will be appreciated.
I don't believe you can set the opacity of individual controls. The form itself has an opacity, but I don't think you want to fade out the whole control.
You can create custom controls that support opacity...here's an example:
http://www.slimee.com/2009/02/net-transparent-forms-and-controls-with.html
I believe this implementation would apply to child controls within the panel (because it is working on the rectangular area that the control takes up). If I'm wrong, you'd have to handle all of the child control's as part of your over-ridden behavior.
As others have said, getting this to look 'smooth' might be a lot of work. Hopefully someone will have a better answer.
As suggested in other answers, you can not (easily without your own controls) fade in/out a panel.
You can fade the form in or out at start up OR have a modal dialog form that fades in or out.
Fade In
private void FadeIn_Tick(object sender, EventArgs e)
{
this.Opacity += .08;
if (this.Opacity >= 1)
{
FadeIn.Stop();
}
}
Fade Out
private void FadeOut_Tick(object sender, EventArgs e)
{
this.Opacity -= .08; //Decrease opacity
if (this.Opacity <= 0) //While it is not 0
{
FadeOut.Stop(); //Stop!
this.Close(); //Close the form
}
}
Related
I have a panel and a timer which is helping me to open a form in an animated way.
The panel contains a form.
code is
private void timer1_Tick(object sender, EventArgs e)
{
if (panel1.Width < 1004)
{
panel1.Left -= 50;
panel1.Width += 50;
}
else
{
timer1.Enabled = false;
}
}
Code is working perfectly when I click a button to enable the timer with no background image in the MDI Form.
But whenever I put a background image its become slow. Takes longer time than before. Every Timer interval can be understood. Form appears by shaking.
Is there is any solution? What can be done to avoid this.
I have a form with a picturebox docked to fill the whole thing. On this form, I have a panel that is normally invisible and another picturebox; on the panel, I have a label and another panel with a label.
Here is what SHOULD happen when the user hovers over the second picturebox:
The picturebox's image changes and the first panel becomes visible, making the second panel and both labels visible too
The user clicks on the second label
The second label's OnClick handler makes the first label's text change and the second panel becomes invisible
A timer ticks for a few seconds
A code segment in the timer's OnTick handler causes the image in the second picturebox to change and the first panel to become invisible
Here is what DOES happen:
The picturebox's image changes and the first panel becomes visible, making the second panel and both labels visible too
The user clicks on the second label
The second label's OnClick handler sets the first label's text to a new string and sets the second panel's Visible property to false, BUT the second panel stays visible (although you can't interact with it) and the first label's text gets written on top of the old text
A timer ticks for a few seconds
A code segment in the timer's OnTick handler causes the image in the second picturebox to change and the first panel to become invisible
I've tried everything I can think of. I've called Invalidate, Update, and Refresh on every control in the form, I've called Application.DoEvents, I've reset the image in the background PictureBox to itself, nothing. The REALLY weird part is that in step 5, when the front picturebox resets itself and all panels are set invisible, nothing gets left behind - it's just for that brief few seconds between the OnClick handler terminating and the timer's OnTick cleaning up that there are problems. I can edit this for more information if needed, but does anyone have any ideas of what to do?
Edit: It's been pointed out to me that I should probably upload the code for this. Well, that code is a hacked-together mess, but okay. Also: there are some weird extra bits (in the enum types among others), they're for later parts of the project and irrelevant right now.
bool CountingHoverTime = false;
int HoverTime = 0;
int MasterTick = 0;
enum GhostState { Stand, Speak, Pet, Ask };
GhostState curState;
public enum TalkType { HoverGen, Petted, Spont, TimerMsg, Response };
private void Form1_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
TopMost = true;
ControlBox = false;
Text = String.Empty;
WindowState = FormWindowState.Maximized;
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
this.TransparencyKey = Color.Transparent;
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//base.OnPaintBackground(e);
}
private void pictureBox2_MouseHover(object sender, EventArgs e)
{
if(curState == GhostState.Stand)
{
CountingHoverTime = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if((curState != GhostState.Ask) && (curState != GhostState.Stand))
{
MasterTick++;
if(MasterTick > 10)
{
SetToBasic();
}
}
else
{
MasterTick = 0;
}
if (CountingHoverTime)
{
HoverTime++;
if (HoverTime > 4)
{
HoverTime = 0;
curState = GhostState.Ask;
Say("What can I do for you?", TalkType.HoverGen);
}
}
}
public void SetToBasic()
{
curState = GhostState.Stand;
ghostBox.Image = Properties.Resources.stickStand1;
TalkPanel.Visible = false;
}
public void Say(String speak, TalkType type)
{
mainText.Text = speak;
if(type == TalkType.Response || type == TalkType.Spont)
{
curState = GhostState.Speak;
}
else
{
curState = GhostState.Ask;
}
ghostBox.Image = Properties.Resources.stickTalk;
if (type == TalkType.HoverGen)
OptionPanel.Visible = true;
else
OptionPanel.Visible = false;
TalkPanel.Visible = true;
backBox.Image = Properties.Resources.background;
ghostBox.Invalidate();
TalkPanel.Invalidate();
mainText.Invalidate();
backBox.Invalidate();
ghostBox.Update();
TalkPanel.Update();
mainText.Update();
backBox.Update();
Application.DoEvents();
}
private void op1label_Click(object sender, EventArgs e)
{
curState = GhostState.Speak;
OptionPanel.Visible = false;
Say("No can do yet.", TalkType.Response);
}
Edit 2: I've put together a gif visualization of what's happening, thank you HandbagCrab.
I've approached this from a different direction. I think the issue is to do with the picturebox you have docked on the form causing some issues.
To fix it and still keep the transparency, get rid of the backBox. Set the background colour of the form to a colour you're not going to use then set the transparency key to that colour. This will make those areas of the form transparent. Now you just need your hidden panel and your labels and whatever control it is that hosts your stick man image.
I've left the backgrounds of the labels as pink here but you should change them to your background colour so that they're not transparent.
When I run the form I get the transparency still and when clicking on the grey panel (I've used a panel to simulate your stick man) shows the hidden panel with the labels. Clicking label2 updates the text on label1 (the one that contains the text "longish text"), completely replacing the text.
Here it is in use (I've not done a gif as I wanted each step to be clearly visible)
Here's the application when open:
Here it is after clicking the grey box:
Here's the updated text when clicking label2:
I've left the application border style as Sizeable just so you can see where the border lies. I also took the screenshots over a black background so there was no visual clutter.
Here's me right clicking the desktop through the transparent section:
Hi i have create a program capture the select area
when the program start it's make the form top most so always it's top for other program , also i transparent the form this help to capture the select area with out capture the form , i have some controls like button this i want to see always when make the capture
here what code i use transparent and make top most
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = (1000/30);
timer1.Start();
TransparencyKey = Color.LightBlue;
BackColor = Color.LightBlue;
FormBorderStyle = FormBorderStyle.None;
DoubleBuffered = true;
SetStyle(ControlStyles.ResizeRedraw, true);
TopMost = true;
}
you can select the area you want by drag or size the form i draw a rectangle
for border
i use timer so when run the program start to capture until user close the program
the problem its capture also the controls like the button i have in center
this i want to hide , i found solution to hide before make the capture
and show again this work well but it's make flashing all the controls and this make pain in eyes here example when make capture
private void timer1_Tick(object sender, EventArgs e)
{
Opacity = 0.0; // hide form
var image = CaptureDesktopSize(Cam); // capture
pictureBox1.Image = image;
image.Save(#"C:/data/V.png", ImageFormat.Png);
Opacity = 1.0; // show form
}
there is any way to do this to hide the controls i don't want
i working with C#
thank you very much this my first question :) i am new user
here also one screenshot to see what i mean
enter image description here
c# winforms here. I need to draw an invisible rectangle area over a panel and catch his mouse enter/leave events.
My situation (as for some other suggestions you may have):
I have a media player (the panel), on mouse enter event I make visible a little navigation menu (it's located over the panel). I want to hide the nav menu on mouse leave from the panel. This works but unfortunately also entering the nav menu make it invisible. Many thanks.
On mouse leave, simply see if the current Cursor.Position is contained by your rectangle. For example, using a panel and a label:
public Form1()
{
InitializeComponent();
panel1.MouseEnter += panel1_MouseEnter;
panel1.MouseLeave += common_MouseLeave;
label1.MouseLeave += common_MouseLeave;
}
private void panel1_MouseEnter(object sender, EventArgs e)
{
label1.Visible = true;
}
private void common_MouseLeave(object sender, EventArgs e)
{
Rectangle rc = panel1.RectangleToScreen(panel1.ClientRectangle);
if (!rc.Contains(Cursor.Position))
{
label1.Visible = false;
}
}
As long as the mouse is over a specific control, we show some form. When the mouse leaves the control, we hide the control after a small timeout. This is standard hover behavior.
However, when a control (for example a Treeview) has a scrollbar, and the mouse is ON or OVER the scrollbar, the events don't fire ...
If we could get a reference to the scrollbar control, this would solve our problem, as we would add the same listener events to the scrollbar. However, the scrollbar isn't accessible as far as I know ...
How can we solve this problem ?
The scrollbar is in the tree view's non-client area. When the mouse moves there, it starts generating non-client messages like WM_NCMOUSEMOVE and WM_NCMOUSELEAVE. You would have to sub-class the TreeView and override WndProc() to detect these message.
This doesn't really solve your problem though, you'll still have a hard time with edge cases. A low-tech approach with a Timer always works:
private Form frmPopup;
private void treeView1_MouseEnter(object sender, EventArgs e) {
timer1.Enabled = true;
if (frmPopup == null) {
frmPopup = new Form2();
frmPopup.StartPosition = FormStartPosition.Manual;
frmPopup.Location = PointToScreen(new Point(treeView1.Right + 20, treeView1.Top));
frmPopup.FormClosed += (o, ea) => frmPopup = null;
frmPopup.Show();
}
}
private void timer1_Tick(object sender, EventArgs e) {
Rectangle rc = treeView1.RectangleToScreen(new Rectangle(0, 0, treeView1.Width, treeView1.Height));
if (!rc.Contains(Control.MousePosition)) {
timer1.Enabled = false;
if (frmPopup != null) frmPopup.Close();
}
}
I think there are several different ways to do this, but the key is your desire to have a timeout on the action. I think a combination of two techniques might work:
Put the control on a panel, docked to fill, and use the MouseEnter of the panel to turn on your behavior -- this will include the control's scrollbar. You can use the MouseLeave event of the panel as well, but you'll have to check the cursor's position to ensure it hasn't moved into the contained control. This method is mostly reliable, but moving the mouse quickly can confuse it.
If you combine this with a timer that starts when your shown/hidden control is shown and check the cursor position periodically. This will work, but your timeout before hiding the control won't necessarily be consistent (because the timer starts when they enter the control). You could stop/start the timer on mousemoves in the control to alleviate this somewhat.
I put together a project of the different methods I tried here: http://lovethedot.s3.amazonaws.com/100609StackoverflowScrollbarQuestion.zip
By docking the control you want to track in the panel, it essentially wraps it and you'll get MouseEnter at the very edge of the tracked control:
private void panel1_MouseEnter(object sender, EventArgs e)
{
this.Text = "in";
}
private void panel1_MouseLeave(object sender, EventArgs e)
{
if (!new Rectangle(new Point(0, 0), panel1.Size).Contains(panel1.PointToClient(Control.MousePosition)))
this.Text = "out";
}
You're tracking entry into the panel surrounding the control, and exit from that panel provided the cursor isn't inside the tracked control.
To get a better "leave" experience, it's combined with a Timer that checks to see where the cursor is as well:
private void listBox3_MouseEnter(object sender, EventArgs e)
{
button1.Visible = true;
visibleTimer.Stop();
visibleTimer.Start();
}
void visibleTimer_Tick(object sender, EventArgs e)
{
if (!new Rectangle(new Point(0, 0), listBox3.Size).Contains(listBox3.PointToClient(Control.MousePosition)))
{
visibleTimer.Stop();
button1.Visible = false;
}
}