Effect To Show Panel in C#.net - c#

I Want Use a Panel in a Windows Form in C#.net. I Set Visible Property of this Control to false And When I Click on a Button, Panel is showed. I Want Show a Panel by some Effect.
Please Help me for this

You are leaving us guessing about what kind of effect you are looking for. I'll just arbitrarily pick a collapse and expand effect. It takes a Timer, you implement the effect in a Tick event handler. Here's an example, it requires a Panel, Timer and Button:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timer1.Interval = 16;
timer1.Tick += new EventHandler(timer1_Tick);
panel1.BackColor = Color.Aqua;
mWidth = panel1.Width;
}
int mDir = 0;
int mWidth;
void timer1_Tick(object sender, EventArgs e) {
int width = panel1.Width + mDir;
if (width >= mWidth) {
width = mWidth;
timer1.Enabled = false;
}
else if (width < Math.Abs(mDir)) {
width = 0;
timer1.Enabled = false;
panel1.Visible = false;
}
panel1.Width = width;
}
private void button1_Click(object sender, EventArgs e) {
mDir = panel1.Visible ? -5 : 5;
panel1.Visible = true;
timer1.Enabled = true;
}
}

The only effect I can think of is to expand the panel by using a timer and change the size of the panel step-by-step.
I would recommend you to use WPF instead of Winforms that is very good at doing this kind of stuff. You can animate all properties of the control like location, size, alpha. Please, check these articles on WPF animation
WPF Animation overview
Walkthroughs: Create a Custom Animated Button

Related

Is there a way to toggle/fix a menu while scrolling?

I want to build a Windows Forms App that has a menu (several labels) on it's left side which is toggled. On the right side there should be some columns i can scroll through. Jst like Excel with it's fixed rownumbers.
Is there a way to do this? Preferably an easy one.
I think you can use two panels to make the form like the picture you provided.
The following code is a code example and you can refer to it.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ScrollBar hScrollBar1 = new HScrollBar();
private void Form1_Load(object sender, EventArgs e)
{
panel1.BorderStyle = BorderStyle.FixedSingle;
panel1.Dock = DockStyle.Left;
panel2.BorderStyle = BorderStyle.FixedSingle;
panel2.Dock = DockStyle.Fill;
hScrollBar1.Dock = DockStyle.Bottom;
hScrollBar1.Scroll += new ScrollEventHandler(hScroller_Scroll);
panel2.Controls.Add(hScrollBar1);
panel2.HorizontalScroll.Visible = false;
panel2.HorizontalScroll.Enabled = true;
}
private void hScroller_Scroll(object sender, ScrollEventArgs e)
{
panel2.HorizontalScroll.Value = e.NewValue;
}
}
The specific result:

How to make a borderless form draggable on a custom title bar? [duplicate]

This question already has answers here:
How to move and resize a form without a border?
(7 answers)
Closed 7 years ago.
I have created a border-less form using c# but could make the custom title bar movable so I search the internet and found this code:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
m.Result = (IntPtr)(HT_CAPTION);
}
private const int WM_NCHITTEST = 0x84;
private const int HT_CLIENT = 0x1;
private const int HT_CAPTION = 0x2;
After applying this code can click and drag my form in every inch of the form except for the title bar .
This is a good example of the movable title bar.
This is a full example
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Custom_Title_Bar
{
public partial class MainForm : Form
{
private PictureBox title = new PictureBox(); // create a PictureBox
private Label minimise = new Label(); // this doesn't even have to be a label!
private Label maximise = new Label(); // this will simulate our this.maximise box
private Label close = new Label(); // simulates the this.close box
private bool drag = false; // determine if we should be moving the form
private Point startPoint = new Point(0, 0); // also for the moving
public MainForm()
{
this.FormBorderStyle = FormBorderStyle.None;// get rid of the standard title bar
this.title.Location = this.Location; // assign the location to the form location
this.title.Width = this.Width; // make it the same width as the form
this.title.Height = 50; // give it a default height (you may want it taller/shorter)
this.title.BackColor = Color.Black; // give it a default colour (or load an image)
this.Controls.Add(this.title); // add it to the form's controls, so it gets displayed
// if you have an image to display, you can load it, instead of assigning a bg colour
// this.title.Image = new Bitmap(System.Environment.CurrentDirectory + "\\title.jpg");
// if you displayed an image, alter the SizeMode to get it to display as you want it to
// examples:
// this.title.SizeMode = PictureBoxSizeMode.StretchImage;
// this.title.SizeMode = PictureBoxSizeMode.CenterImage;
// this.title.SizeMode = PictureBoxSizeMode.Zoom;
// etc
// you may want to use PictureBoxes and display images
// or use buttons, there are many alternatives. This is a mere example.
this.minimise.Text = "Minimise"; // Doesn't have to be
this.minimise.Location = new Point(this.Location.X + 5, this.Location.Y + 5); // give it a default location
this.minimise.ForeColor = Color.Red; // Give it a colour that will make it stand out
// this is why I didn't use an image, just to keep things simple:
this.minimise.BackColor = Color.Black; // make it the same as the PictureBox
this.Controls.Add(this.minimise); // add it to the form's controls
this.minimise.BringToFront(); // bring it to the front, to display it above the picture box
this.maximise.Text = "Maximise";
// remember to make sure it's far enough away so as not to overlap our minimise option
this.maximise.Location = new Point(this.Location.X + 60, this.Location.Y + 5);
this.maximise.ForeColor = Color.Red;
this.maximise.BackColor = Color.Black; // remember, we want it to match the background
this.maximise.Width = 50;
this.Controls.Add(this.maximise); // add it to the form
this.maximise.BringToFront();
this.close.Text = "Close";
this.close.Location = new Point(this.Location.X + 120, this.Location.Y + 5);
this.close.ForeColor = Color.Red;
this.close.BackColor = Color.Black;
this.close.Width = 37; // this is just to make it fit nicely
this.Controls.Add(this.close);
this.close.BringToFront();
// now we need to add some functionality. First off, let's give those labels
// MouseHover and MouseLeave events, so they change colour
// Since they're all going to change to the same colour, we can give them the same
// event handler, which saves time of writing out all those extra functions
this.minimise.MouseEnter += new EventHandler(Control_MouseEnter);
this.maximise.MouseEnter += new EventHandler(Control_MouseEnter);
this.close.MouseEnter += new EventHandler(Control_MouseEnter);
// and we need to do the same for MouseLeave events, to change it back
this.minimise.MouseLeave += new EventHandler(Control_MouseLeave);
this.maximise.MouseLeave += new EventHandler(Control_MouseLeave);
this.close.MouseLeave += new EventHandler(Control_MouseLeave);
// and lastly, for these controls, we need to add some functionality
this.minimise.MouseClick += new MouseEventHandler(Control_MouseClick);
this.maximise.MouseClick += new MouseEventHandler(Control_MouseClick);
this.close.MouseClick += new MouseEventHandler(Control_MouseClick);
// finally, wouldn't it be nice to get some moveability on this control?
this.title.MouseDown += new MouseEventHandler(Title_MouseDown);
this.title.MouseUp += new MouseEventHandler(Title_MouseUp);
this.title.MouseMove += new MouseEventHandler(Title_MouseMove);
}
private void Control_MouseEnter(object sender, EventArgs e)
{
if (sender.Equals(this.close))
this.close.ForeColor = Color.White;
else if (sender.Equals(this.maximise))
this.maximise.ForeColor = Color.White;
else // it's the minimize label
this.minimise.ForeColor = Color.White;
}
private void Control_MouseLeave(object sender, EventArgs e)
{
// return them to their default colors
if (sender.Equals(this.close))
this.close.ForeColor = Color.Red;
else if (sender.Equals(this.maximise))
this.maximise.ForeColor = Color.Red;
else // it's the minimise label
this.minimise.ForeColor = Color.Red;
}
private void Control_MouseClick(object sender, MouseEventArgs e)
{
if (sender.Equals(this.close))
this.Close(); // close the form
else if (sender.Equals(this.maximise))
{
// maximise is more interesting. We need to give it different functionality,
// depending on the window state (Maximise/Restore)
if (this.maximise.Text == "Maximise")
{
this.WindowState = FormWindowState.Maximized; // maximise the form
this.maximise.Text = "Restore"; // change the text
this.title.Width = this.Width; // stretch the title bar
}
else // we need to restore
{
this.WindowState = FormWindowState.Normal;
this.maximise.Text = "Maximise";
}
}
else // it's the minimise label
this.WindowState = FormWindowState.Minimized; // minimise the form
}
void Title_MouseUp(object sender, MouseEventArgs e)
{
this.drag = false;
}
void Title_MouseDown(object sender, MouseEventArgs e)
{
this.startPoint = e.Location;
this.drag = true;
}
void Title_MouseMove(object sender, MouseEventArgs e)
{
if (this.drag)
{
// if we should be dragging it, we need to figure out some movement
Point p1 = new Point(e.X, e.Y);
Point p2 = this.PointToScreen(p1);
Point p3 = new Point(p2.X - this.startPoint.X,
p2.Y - this.startPoint.Y);
this.Location = p3;
}
}
} // end of the class
} // end of the namespace
If you want you can extract just the moving code and integrate it with your code, the movable Title code is just in the following Event Handlers
Title_MouseUp
Title_MouseDown
Title_MouseMove
Here is the original article for this code, you can read it for more explanation about the code
http://www.dreamincode.net/forums/topic/64981-designing-a-custom-title-bar/
The link to original article is broken

How to make sidebar?

I am using C# programming language.
I have created a new borderless Windows Form in Visual Studio 2012. I have two panels. First panel (panel1) is docked at top and second panel (panel2) is docked at left and its Visible status is false. I also added a button (buttonMenu) in panel1 and docked it on left. What I am trying to achieve is that on buttonMenu click, panel2 slides from left to its original location (left dock) and when buttonMenu clicked again slides away.
I have tried to set the following code:
private void buttonMenu_Click(object sender, EventArgs e)
{
panel2.Visible = !panel2.Visible;
}
And it works but i am trying to make it slide with an animation.
The easier way to do it would be to create a timer and in its Tick event change the panel width like so
private Timer timer = new Timer();
bool closing = false;
int desiredWidth = 300
private void ConfigureTimer()
{
timer.Tick += timer_Tick;
timer.Interval = 16;
}
private void timer_Tick(object sender, EventArgs e){
{
if(closing)
{
panel2.Width-=15;
if(panel2.Width < 0)
{
panel2.Width = 0;
timer.Stop();
}
}else{
panel2.Width+=15;
if(panel2.Width >= desiredWidth)
{
panel2.Width = desiredWidth;
timer.Stop();
}
}
}
private void SwitchPanelState()
{
closing = !closing;
timer.Start();
}
But if you want to make you life easy and more object oriented, you should create an Control with the specified behavior and just add it in your form

MouseLeave detection not working with ImageForm

I've got a smaller image in my form. When the user hovers over the image it brings up a larger view of the image (that follows the mouse, but stays a certain distance from the mouse). In order to do this I am generating a Form with no FormBorderStyles when the cursor hovers the image.
The problem I'm running into is that the first form doesn't seem to detect any longer that the mouse is hovering or leaving the PictureBox once the form activates. The Form also doesn't follow the cursor.
Here is the slimmed down version of what I've got:
C#
bool formOpen = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseHover += pictureBox1_MouseHover;
pictureBox1.MouseLeave +=pictureBox1_MouseLeave;
}
void pictureBox1_MouseHover(object sender, EventArgs e)
{
if (formOpen == false)
{
Form form = new Form();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.BackColor = Color.Orchid;
//Show the form
form.Show();
form.Name = "imageForm";
this.AddOwnedForm(form);
//Set event handler for when the mouse leaves the image area.
//form.MouseLeave += form_MouseLeave;
//Set the location of the form and size
form.BackColor = Color.Black;
form.Dock = DockStyle.Fill;
form.Size = new Size(30, 30);
form.BackgroundImageLayout = ImageLayout.Center;
this.TopMost = true;
formOpen = true;
form.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
}
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
//MessageBox.Show("Worked");
}
}
The MouseLeave (and other events) was not recognized because the opening of the popup window and especially making it topmost=true took away the focus from the original form and its PictureBox.
It also didn't move because not code for moving was provided..
Here are a few changes that will make the form move:
You need a reference to it at the form1 level
you need to move it in the MouseMove event
Note that Hover is a once-only type of event. It fires only once until you leave the control.. (Note: Setsu has switched from Hover to Enter. This works fine, but lacks the short delay before showing the 2nd Form. If you want that back you can either switch back to Hover or you can fake the hover delay by a Timer, which is what I often do.)
// class level variable
Form form;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseEnter += pictureBox1_MouseEnter;
pictureBox1.MouseLeave +=pictureBox1_MouseLeave;
pictureBox1.MouseMove += pictureBox1_MouseMove; // here we move the form..
}
// .. with a little offset. The exact numbers depend on the cursor shape
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if ((form != null) && form.Visible)
{
form.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
}
}
void pictureBox1_MouseEnter(object sender, EventArgs e)
{
// we create it only once. Could also be done at startup!
if (form == null)
{
form = new Form();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//form.BackColor = Color.Orchid;
form.Name = "imageForm";
this.AddOwnedForm(form);
form.BackColor = Color.Black;
form.Dock = DockStyle.Fill;
form.Size = new Size(30, 30);
form.BackgroundImageLayout = ImageLayout.Center;
//this.TopMost = true; // wrong! this will steal the focus!!
form.ShowInTaskbar = false;
}
// later we only show and update it..
form.Show();
form.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
// we want the Focus to be on the main form!
Focus();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
if (form!= null) form.Hide();
}
MouseHover = Occurs when the mouse pointer rests on the control. (msdn)
Try MouseMove instead.

How to gradually resize a picturebox?

I have been trying to code this for a while and after a couple of weeks of searching the web for an answer I decided to ask
All I want to do is gradually resize pictureBox1 to a set limit from a variable starter value when the mouse hovers over it, the furthest I got was using a forloop which made it instantly change size. I would like it to also change height and width at the same time (pictureBox1 will be a square and i just want it to be a bigger square with a bit of smooth movement)
Also I need it to gradually change back to the original size once the mouse moves off of pictureBox1.
I have been toying about with a couple of solutions found on websites but none seem to work properly, also you might need to know that I have two forms involved in this code; Form1 and frmMenu and because of a mass amount of errors I commented out the bottom two methods.
I do not get any errors but it just doesn't work.
public partial class frmMenu : Form
{
//private int size = 100;
public Timer timer1;
public frmMenu()
{
InitializeComponent();
pictureBox1.MouseEnter += new EventHandler(pictureBox1_MouseEnter);
//pictureBox1.MouseLeave += new EventHandler(pictureBox1_MouseLeave);
}
private string frmMenu_Load
{
set
{
timer1.Interval = 1;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
//for (int i = 140; i > size; size++)
//{
//}
{
timer1.Interval = 1;
}
timer1.Enabled = true;
if (pictureBox1.Height <= 140)
{
pictureBox1.Size = new Size(pictureBox1.Size.Width, pictureBox1.Size.Height + 1);
}
else
{
timer1.Enabled = false;
}
}
// private void pictureBox1_MouseLeave(object sender, EventArgs e)
// {
// if (size > 100)
// for (int i = size; i > 100; i--)
// {
// size = i;
// }
// pictureBox1.Height = pictureBox1.Width = size;
// }
// private void pictureBox1_Click(object sender, EventArgs e)
// {
// var Form1 = new Form1();
// Form1.Show();
// var Menu = new frmMenu();
// Menu.Close();
// }
}
This is my first time asking so sorry if I haven't given enough information ^.^
Try this code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
bool mouseHover;
int width;
int height;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 25;
timer1.Tick += timer1_Tick;
width = pictureBox1.Width;
height = pictureBox1.Height;
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
if (mouseHover)
{
pictureBox1.Width += (pictureBox1.Width < 100) ? 5 : 0;
pictureBox1.Height += (pictureBox1.Height < 100) ? 5 : 0;
}
else
{
pictureBox1.Width += (pictureBox1.Width > width) ? -5 : 0;
pictureBox1.Height += (pictureBox1.Height > height) ? -5 : 0;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
mouseHover = true;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
mouseHover = false;
}
}
}
You can adjust the interval to how you like it, but increasing at 5 pixels horizontally/vertically every 25 milliseconds is pretty smooth. You need to set the initial height and width so you can go back to that size after the mouse leaves the picture box. I use the null coalescing operator so you don't have to stop the timer. As long as the condition on the left side of the ? is true, it will evaluate to the value on the left side of the :. When the condition is false, it evaluates to the right side of the :.

Categories

Resources