When shutting down a Windows XP system, it displays a modal dialog box while the background fades to a grayscale. I will like to achieve the same effect in any of the programming languages in the tag list. Can anyone help?
This is pretty easy to do with Winforms. You need a borderless maximized window with a gray background whose Opacity you change with a timer. When the fade is done, you can display a dialog that is borderless and uses the TransparencyKey to make its background transparent. Here's a sample main form that implements this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.BackColor = Color.FromArgb(50, 50, 50);
this.Opacity = 0;
fadeTimer = new Timer { Interval = 15, Enabled = true };
fadeTimer.Tick += new EventHandler(fadeTimer_Tick);
}
void fadeTimer_Tick(object sender, EventArgs e) {
this.Opacity += 0.02;
if (this.Opacity >= 0.70) {
fadeTimer.Enabled = false;
// Fade done, display the overlay
using (var overlay = new Form2()) {
overlay.ShowDialog(this);
this.Close();
}
}
}
Timer fadeTimer;
}
And the dialog:
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
FormBorderStyle = FormBorderStyle.None;
this.TransparencyKey = this.BackColor = Color.Fuchsia;
this.StartPosition = FormStartPosition.Manual;
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
this.Location = new Point((this.Owner.Width - this.Width) / 2, (this.Owner.Height - this.Height) / 2);
}
private void button1_Click(object sender, EventArgs e) {
this.DialogResult = DialogResult.OK;
}
}
Related
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:
I want to show a inherited text box control on mouse over on the form. But text is not displayed. Below is my code.
private ChartCalloutBox m_calloutbox = null;
public Form2()
{
InitializeComponent();
this.MouseMove += Form2_MouseMove;
}
void Form2_MouseMove(object sender, MouseEventArgs e)
{
if (m_calloutbox == null)
{
m_calloutbox = new ChartCalloutBox();
}
m_calloutbox.Location = e.Location;
m_calloutbox.Show();
}
internal class ChartCalloutBox : TextBox
{
public ChartCalloutBox()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.Location = new System.Drawing.Point(350, 170);
this.ClientSize = new System.Drawing.Size(130, 40);
this.Size = new System.Drawing.Size(130, 40);
this.BackColor = System.Drawing.Color.Black;
this.ForeColor = System.Drawing.Color.Brown;
this.Name = "CalloutBox";
this.Text = "Callout Rect";
this.ResumeLayout(false);
//
}
}
Any one help on this how to show the text box on mouse over. and the text box place should be change based on the mouse position.
Thanks,
Bharathi.
Add your control to controls collection.
Code should be like
void Form2_MouseMove(object sender, MouseEventArgs e)
{
if (m_calloutbox == null)
{
m_calloutbox = new ChartCalloutBox();
this.Controls.Add(m_calloutbox);
}
m_calloutbox.Location = e.Location;
}
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.
suppose i have have two sdi form and when apps run then a sdi form show and from there i am showing another sdi form but the problem is i can drga any where the second sdi form which i do not want. in case of MDI form the mdi child form can not be drag out of mdi form boundary.so in my case i want to simulate the same thing. i want no other sdi form can not be drag out form my main sdi form's boundary. so just guide me how to do this.
i could guess that i have to work with form drag event and from there i have to check form top and left but need more suggestion.
private void Form1_Move(object sender, EventArgs e)
{
this.Location = defaultLocation;
}
i try to do it this way but it is not working.
public partial class Form2 : Form
{
Form1 _parent = null;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 parent)
{
InitializeComponent();
_parent = parent;
}
private void Form2_Move(object sender, EventArgs e)
{
if((this.Location.X+this.Width) > _parent.Width)
{
this.Location = new System.Drawing.Point(_parent.ClientRectangle.Width-this.Width,this.Location.Y);
}
if ((this.Location.Y + this.Height) > _parent.Height)
{
this.Location = new System.Drawing.Point(_parent.ClientRectangle.Height - this.Height, this.Location.X);
}
if (this.Location.Y < 0)
{
this.Location = new System.Drawing.Point(this.Location.X);
}
if (this.Location.X < 0)
{
this.Location = new System.Drawing.Point(this.Location.Y);
}
}
}
please guide me where i made the mistake.
thanks
UPDATE
private void Form2_Move(object sender, EventArgs e)
{
int left = this.Left;
int top = this.Top;
if (this.Left < _parent.Left)
{
left = _parent.Left;
}
if (this.Right > _parent.Right)
{
left = _parent.Right - this.Width;
}
if (this.Top < _parent.Top)
{
top = _parent.Top;
}
if (this.Bottom > _parent.Bottom)
{
top = _parent.Bottom - this.Height;
}
this.Location = new Point(left, top);
}
I recomend to you follow the #ProgrammerV5 recomendation. But if you realy need to control the form movement, please see the use of Cursor.Clip property
here are some information: http://www.codeproject.com/Tips/375046/The-Cursor-Clip-Property
Also you may need to do a MouseCapture.
I'm writing a transparent WinForms app and I want to hide the app from showing in Task Manager's applications tab. I'm OK with the fact that it will show in Processes (in fact it should).
If I set:
this.ShowInTaskbar = false;
it only hides from taskbar.
Full code i have i have a timer made from labels
public Form1()
{
InitializeComponent();
this.BackColor = Color.LimeGreen;
this.TransparencyKey = Color.LimeGreen;
Timer time = new Timer();
time.Interval = 1000;
time.Tick += new EventHandler(time_Tick);
time.Start();
this.ShowInTaskbar = false;
}
void time_Tick(object sender, EventArgs e)
{
label1_hour.Text = DateTime.Now.Hour.ToString() ;
label_minute.Text = DateTime.Now.Minute.ToString();
label_second.Text = DateTime.Now.Second.ToString();
}
Try something like this
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;
}
protected override CreateParams CreateParams {
get {
var cp = base.CreateParams;
cp.ExStyle |= 0x80; // Turn on WS_EX_TOOLWINDOW
return cp;
}
}
}
Simply setting the form property FormBorderStyle to FixedToolWindow worked for me. On Win 10 it removes it from "Apps" in Task Manager and puts it in "Background processes"...which the OP specified (and was what I wanted also.)
In addition, it removes the form from showing in "Windows Key + Tab" listing of windows...which is what I wanted as well.