How to show only border of winforms window when resizing? - c#

I would like to disable displaying of the content of the window when resizing, is it possible? The problem is that when I'm resizing my window the controls redraw on correct positions but it doesn't look good because it's not done fluently.
EDIT: I would like a code that would manage the following scenario:
I click on the corner of window
Now only the border of window is visible - the middle part is transparent
I set the size of the window by mouse
I release the mouse button and the middle part of the window will appear
EDIT II:
I've got the MDI application and it doesn't support transparency for child windows

An idea is to put all the controls in a panel and set it's visibility to false on the resize event of the form.
Edit: this will make the form transparent while resizing.
private void Form1_ResizeBegin(object sender, EventArgs e)
{
panel1.Visible = false;
Form1.ActiveForm.TransparencyKey = Color.Transparent;
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
panel1.Visible = true;
Form1.ActiveForm.TransparencyKey = Color.Gray; // or whatever color your form was
}

Related

How to set a transparent winform, into a panel transparent panel, to show as background what's behind?

I'm coding a program which has a principal form with a pictureBox that i use as a common background image, i also use a panel, who has the pictureBox as a Parent, and I use that panel to join my other winforms in it, lets call this winforms "A" , my problem is that i set the A winforms as Transparent, so i can see the pictureBox that is behind the panel, now, my problem resides that when i join the A winforms, they show as transparent, but with the issue that the don't show the pictureBox, they show what's behind the program.
This is the PictureBox that i want to show behind the A winforms
This is what I'm talking about, as you cab see, the program is showing me my own desktop wallpaper
Please I need your help, as you can realise, as you can realise, my english is not so good, so i'm
apologizing myself in advance. :)
To set the form's background, you don't need to add the PictureBox. You only need to set its BackgroundImage Property.
this.BackgroundImage = Image.FromFile(#"D://pic/b.jpg");
Then set Panel to transparent.
panel1.BackColor = Color.Transparent;
panel1.Dock = DockStyle.Fill;
To set FormA to transparent, also need to call SetStyle in constructor.
public FormA()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();
}
Here is the simple demo.
private void Form1_Load(object sender, EventArgs e)
{
FormA formA = new FormA();
formA.FormBorderStyle = FormBorderStyle.None;
formA.TopLevel = false;
formA.Dock = DockStyle.Fill;
panel1.Controls.Add(formA);
formA.BackColor = Color.Transparent;
formA.Show();
}
Hope this can help you.

Max size of a form, enable Maximize Windows form state C#

Currently I am creating a form where the user should not be allowed to change the width or height of the Form, but they should be able to maximize the form when they want to.
I've tried the following:
Form myForm = new Form() {
MaximumSize = new Size(500, 500),
MinimumSize = new Size(500, 500),
WindowState = FormWindowState.Maximized
};
The problem is that when the form displays it's size is width=500 and height=500, but I would like it to be maximized.
Is there another way to stop the user of adjusting the height and width?
Do you want the form maximized, or do you want it 500x500 pixels? You can't have both at the same time.
As for preventing them changing the form size, you can set the FormBorderStyle to one of the fixed options. This will prevent them resizing it.
From MSDN: [https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formborderstyle(v=vs.110).aspx][1]
The border style of the form determines how the outer edge of the form appears. In addition to changing the border display for a form, certain border styles prevent the form from being sized. For example, the FormBorderStyle.FixedDialog border style changes the border of the form to that of a dialog box and prevents the form from being resized. The border style can also affect the size or availability of the caption bar section of a form.
This is very easy to solve. A form has a function called SizeChanged. If you press the maximize button it won't trigger so this function can be used so solve the problem. You just set the window size in SizeChanged to 500, 500 and your problem will be solved. There may be fancier ways to solve it but this option is to my opinion the easiest.
private void Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(500, 500);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
this.Size = new Size(500, 500);
}
This should do the job :) Success!

How to remove Picture Box background after picture box?

How to remove that picturebox background after picture box?
I want to make when a mouse hover some picturebox slot, it will show tooltip text. But it was very disturbing picture box slot and must be made invisible.
Look here
Thank you :)
Controls have a Visbile property, if you set it to fase, the control will not be visibe anymore.
Or is your issue when to make it invisible? You may need to handle the MouseLeave event, and place this code there:
private void OnMouseLeavePicturebox(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}

c# detect windows really resized instead moved

In my winform program I need to detect when the form is resized: but the ResizeEndmethod is also called when the form is simply moved into the desk..
Is it possible check only when the windows is only resized??
In my mind I can save the last width and the last height and into the ResizeEnd method like this:
int lastWidth;
int lastHeigth;
private void frmMain_ResizeEnd(object sender, EventArgs e)
{
if (lastHeigth != this.Height || lastWidth != this.Width)
{
lastHeigth = this.Height;
lastWidth = this.Width;
fireResize();
}
}
But this is an ugly solution...
Only marginally better than your original solution, but at least it adresses the problem instead of just quoting the docs.
Obviousy the problem is that Resize fires all the time, so a flag seems to be necessary:
bool sizing = false;
private void Form1_ResizeEnd(object sender, EventArgs e)
{
if (!sizing) return;
if (sizing) {sizing = false; /*do your stuff*/ }
}
private void Form1_Resize(object sender, EventArgs e)
{
sizing = true;
}
Of course it would be nice to have an indicator in the EventArgs of ResizeEnd but can't see a simpler way to do it.
BTW, instead of checking Width and Height using Size would also be a small improvement..
Why not use this? This works fine for me...
public Form1()
{
this.Resize += Form1_Resize;
}
void Form1_Resize(object sender, EventArgs e)
{
// do what you want to do
}
Here read this from MSDN
The ResizeBegin event is raised when the user begins to resize a form, typically by clicking and dragging one of the borders or the sizing grip located on the lower-right corner of the form. This action puts the form into a modal sizing loop until the resize operation is completed. Typically, the following set of events occurs during a resize operation:
A single ResizeBegin event occurs as the form enters resizing mode.
Zero or more pairs of Resize and SizeChanged events occur as the form's Size is modified.
A single ResizeEnd event occurs as the form exits resizing mode.
Note:
Just clicking without dragging on a border or resizing grip will generate the ResizeBegin and ResizeEnd events without any intermediate Resize and SizeChanged event pairs.
The ResizeBegin and ResizeEnd pair of events is also raised when the user moves the form, typically by clicking and dragging on the caption bar. These events are not generated by programmatic manipulation of the form, for example by changing the Size or Location properties.
Use simple Resize event. It's only triggered on resizing.
How about checking the documentation? The second search engine hit is the following.
Form.ResizeBegin Event - MSDN
The ResizeBegin event is raised when the user begins to resize a form, typically by clicking and dragging one of the borders or the sizing grip located on the lower-right corner of the form. This action puts the form into a modal sizing loop until the resize operation is completed. Typically, the following set of events occurs during a resize operation:
A single ResizeBegin event occurs as the form enters resizing mode.
Zero or more pairs of Resize and SizeChanged events occur as the form's Size is modified.
A single ResizeEnd event occurs as the form exits resizing mode.
Just clicking without dragging on a border or resizing grip will generate the ResizeBegin and ResizeEnd events without any intermediate Resize and SizeChanged event pairs.
The ResizeBegin and ResizeEnd pair of events is also raised when the user moves the form, typically by clicking and dragging on the caption bar. These events are not generated by programmatic manipulation of the form, for example by changing the Size or Location properties.

Some questions about Windows Presentation Foundation (C#)

I am working on a program that contains (among other things) a WPF window for which I am using the next code to maximize it at a MouseDoubleClick Event:
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
this.Topmost = true;
Now, what I want to do is that when the window is maximized and the mouse exits the screen (goes to the bottom of the screen until it exits the screen) a new window to appear at the bottom of the screen(WPF or WindowsForm) that will contains several things (buttons, a scrollBars, etc) and that will be active only as long as the mouse is over it (just like in BSplayer). My question is how to do that ? I'm really a starter with WPF, I don't know XAML and I would prefer to do as much as I can using C# code.
So: how do I know when the mouse leaves the screen and how do I make that window to appear on bottom of the screen (without minimizing or doing anything else with the original window) ?
I tried using this.MouseLeave but it doesn't work when the window is maximized.
And if I am asking this question here, I will use my chance to also ask two other things:
When the WPF window is maximized and if the mouse hasn't been moved for more than 5 seconds, than I want the mouse to be hidden and to become visible again only when the mouse moves. How do I do this ?
When the WPF window is not maximized, I want that the border of the screen to be very small, almost invisible (no minimize, close or other button). I am using this.WindowStyle = System.Windows.WindowStyle.ToolWindow but it still leaves the exit/close button there; If I use this.WindowStyle = System.Windows.WindowStyle.None it looks perfect, but then I can't move the window. I there anyway to make the window movable with WindowStyle.None ? Preferably, when I keep the mouse pressed on the interior of the screen, I want to be able to drag the WPF window around on my screen.
Really need help to these problems. It's a pretty important project that I am working on.
Answer to this question
When the WPF window is maximized and if the mouse hasn't been moved for more than 5 seconds, than I want the mouse to be hidden and to become visible again only when the mouse moves. How do I do this ?
This can be achieved by using the timer with an interval of 5 seconds. When timer elapse set mouse cursor to None to hide it and when mouse moves, reset the mouse cursor to original one.
Put below code in the constructor:
this.MouseMove += new MouseEventHandler(MainWindow_MouseMove);
tm = new System.Timers.Timer();
tm.Interval = 5000;
tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed);
tm.Start();
Below are event defination:
void MainWindow_MouseMove(object sender, MouseEventArgs e)
{
tm.Stop();
tm.Start();
// Reseting the time back to original. Here I have assumed that original one is Arrow.
this.Dispatcher.Invoke(new Action(() =>
{
Mouse.OverrideCursor = Cursors.Arrow;
}));
}
void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.Dispatcher.Invoke(new Action(() =>
{
if (Mouse.OverrideCursor != Cursors.None)
{
Mouse.OverrideCursor = Cursors.None;
currentCursor = Mouse.OverrideCursor;
}
}));
}
Hope this helps !!

Categories

Resources