I made a custom window for my app and I wrote some code if a user clicks my custom maximize button:
private void MaxThis(object sender, System.Windows.RoutedEventArgs e)
{ if (WindowState == WindowState.Maximized){
WindowState = WindowState.Normal;}
else {
this.Top = 0;
this.Left = 0;
this.MaxWidth = System.Windows.SystemParameters.WorkArea.Width;
this.MaxHeight = System.Windows.SystemParameters.WorkArea.Height;
this.WindowState = WindowState.Maximized;
}
}
The restoring to the normal state works fine. However when I want to maximize, it maximizes the window with a small margin on the right and bottom of the screen. Clicking maximize again fixes this somehow. How do I fix this to maximize at the first click...?
Take away the Height and Width properties you have set in XAML for your window
Try to use just
this.WindowState = WindowState.Maximized;
Perhaps code, goes before messing Windows API action.
Sorry, my mistake.
Then you should use Windows API to raise Maximize event.
Try this code:
[DllImport("user32.dll")]
public static extern int SendMessage(
int hWnd, // handle to destination window
uint Msg, // message
long wParam, // first message parameter
long lParam // second message parameter
);
public const int WM_SIZE = 0x0005;
public const int SIZE_MAXIMIZED = 2;
And in your click event:
SendMessage(this.Handle, WM_SIZE, SIZE_MAXIMIZED, 0);
Related
I have a requirement to override the Minimize function of the Telerik radWindow (clicking the Minimize Button on the Title Bar). Instead of the Window Minimizing to the bottom left of the screen I intend to simply reduce the Height of the Window leaving just the Title Bar showing (in the original position). Restoring the Window will again simply set the Height back to its original value to expand the Window….
So, I need to set Handle = true when the SC_MINIMIZE Message is received, this will prevent the Window from minimizing, then i can implement my own code to change the Height of the Window… This is the code so far…
private void RadWindow_HostCreated(object sender, HostWindowCreatedEventArgs e)
{
e.HostWindow.SourceInitialized += HostWindow_SourceInitialized;
}
private void HostWindow_SourceInitialized(object sender, EventArgs e)
{
var source = PresentationSource.FromVisual((Window)sender) as HwndSource;
if (source != null) source.AddHook(new HwndSourceHook(HandleMessages));
}
private IntPtr HandleMessages(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// 0x0112 == WM_SYSCOMMAND, 'Window' command message.
// 0xF020 == SC_MINIMIZE, command to minimize the window.
if (msg == 0x0112 && ((int)wParam & 0xFFF0) == 0xF020)
{
handled = true; // not catching SC_MINIMIZE here
}
return IntPtr.Zero;
}
However, while I seem to be catching Windows Messages in the HandleMessages function, it is not setting handled = true; when I click the Minimize Button…
Any help would be appreciated…
I am developing a Windows Forms application in C# in which I have a form which must start in a maximized state and not allow users to restore or resize it. I have already configured the form to start in maximized mode, disable the restore and maximize button and locked the borders of the form but when the title bar is double clicked, the form restores to a smaller size which is unexpected. The following are the properties I set to achieve the required behaviour:
FormBorderStyle = FixedSingle
MaximizeBox = False
WindowState = Maximized
Can someone please help me solve this problem and explain me the solution?
Thanks in advance.
You have to remember that your form starts with some default size values and double click is just toggling between 2 states.
Within your normal state the form will retrieve it's last ( in your case default ) size which you can override :
Width = Screen.PrimaryScreen.Bounds.Width;
Height = Screen.PrimaryScreen.Bounds.Height;
Another thing is that your application has something called start position which ( from what I remember ) defaults to the center of the screen and you can change it using :
Form.StartPosition = new Point(0, 0); // top-left corner
Now all you have to do in your applicaiton is to check for the toggle between window states. Easiest way would be to use WndProc and wait for messages listed in this msdn page :
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MAXIMIZE = 0xF030;
const int SC_RESTORE = 0xF120;
if (m.Msg == WM_SYSCOMMAND)
{
switch((int)m.WParam)
{
case SC_RESTORE:
// your window was restored ( double clicked on the command bar )
// set it's window state back to maximize or do whatever
break;
case SC_MAXIMIZE:
// your window was maximized .. no actions needed, just for debugging purpose
break;
}
}
base.WndProc(ref m);
}
This can be accomplished by catching the event and overriding it:
private void Form_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.WindowState = FormWindowState.Maximized;
this.MaximizeBox = false;
this.MinimumSize = Screen.GetWorkingArea(this.Location).Size;
}
private const int WM_NCLBUTTONDBLCLK = 0x00A3;
//double click on a title bar
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCLBUTTONDBLCLK)
{
m.Result = IntPtr.Zero;
return;
}
base.WndProc(ref m);
}
Firstly, I apologize if the title does not make much sense, as I did not know the best way to explain it.
Now to really explain it. What I have done is created a control in a Class Library project in Visual Studio 2013. This control is supposed to act as the caption bar for form that is set with the "FormBorderStyle" as "None". This imitation caption bar control is supposed to move the form, just like a normal forms' caption bar would.
I have achieved this, but only in the forms code. This is the code I use:
private int mouseStartX, mouseStartY;
private int formStartX, formStartY;
private bool FormDragging = false;
private void titleBar_MouseDown(object sender, MouseEventArgs e)
{
this.mouseStartX = MousePosition.X;
this.mouseStartY = MousePosition.Y;
this.formStartX = this.Location.X;
this.formStartY = this.Location.Y;
FormDragging = true;
}
private void titleBar_MouseMove(object sender, MouseEventArgs e)
{
if (FormDragging)
{
this.Location = new Point(
this.formStartX + MousePosition.X - this.mouseStartX,
this.formStartY + MousePosition.Y - this.mouseStartY
);
}
}
private void titleBar_MouseUp(object sender, MouseEventArgs e)
{
FormDragging = false;
}
"this.*" is obviously referring to the form, when in the forms code. So of course, if I were to simply put this into the controls code, it'd obviously be referring to the control, and thus the control would be the one moving around on the form.
I've also created a control in the Class Library that acts as a close button. All I had to do was:
Form.ActiveForm.Close();
Same for minimize being:
Form.ActiveForm.WindowState = FormWindowState.Minimized;
And maximize being:
Form.ActiveForm.WindowState = FormWindowState.Maximized;
On the controls' click events.
When I try to replace "this." with "Form.ActiveForm.", in the first code posted - it returns this error:
'System.Windows.Forms.Form' does not contain a definition for 'mouseStarX' and no extension method 'mousStartX' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?)
That's about it, I don't know how else to go about this.
There is a simple pinvoke you can use to move the form via your control.
Adapted from C# - Make a borderless form movable?, instead of using Form.ActiveForm, you would use this.FindForm() to get the parent form of the control. It's used here to pass the form's handle value:
public class MyHeader : Control {
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg,
int wParam, int lParam);
[DllImportAttribute("user32.dll")]
private static extern bool ReleaseCapture();
protected override void OnMouseDown(MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
ReleaseCapture();
SendMessage(this.FindForm().Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
base.OnMouseDown(e);
}
}
For closing the form, you would use the same method:
this.FindForm().Close();
I have been dealing with this issue and i dont know how to proceed.
I have a project with both windows forms and WPF forms.
I want every form to be displayed like this (its a WPF one):
WPF = http://imageshack.us/photo/my-images/545/wpfk.png/
I achieved this windows style = none and canresize = yes. I dont actually want it to be resized. I just want that thin border arround the form. But if i put canresize = false i lose the border. I also want to be able to move the window in the screen, not to be static in that place.
I need all that for my winforms too.
Winforms:
WINFORM = http://imageshack.us/photo/my-images/836/winforms.png/
I hope you guys understand what i need. Graphically, it has to be like the first image.
I'm not sure if it helps, but you can create Forms collection for this. (a)
http://support.microsoft.com/kb/815707/en-us
Solution: Paste this code into your form or base form.
private const int WS_SYSMENU = 0x80000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style &= ~WS_SYSMENU;
return cp;
}
}
Thanks Killercam for the help!
Solution for WPF Window:
public MainWindow()
{
SourceInitialized += Window_SourceInitialized;
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void Window_SourceInitialized(object sender, EventArgs e)
{
WindowInteropHelper wih = new WindowInteropHelper(this);
int style = GetWindowLong(wih.Handle, GWL_STYLE);
SetWindowLong(wih.Handle, GWL_STYLE, style & ~WS_SYSMENU);
}
private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x00080000;
[DllImport("user32.dll")]
private extern static int SetWindowLong(IntPtr hwnd, int index, int value);
[DllImport("user32.dll")]
private extern static int GetWindowLong(IntPtr hwnd, int index);
You just need to set the WinForms FormBorderStyle property in the designer to Sizable, FixedDialog, Fixed3D etc. One of these is bound to give you the behaviour you require.
I've created a custom layered WPF window with the following properties:
AllowsTransparency = True
ShowInTaskbar = False
Background = Transparent
Topmost = True
Icon = "Icon.ico"
I've added Icon.ico under "Project Properties"->"Application" tab.
The icon displays as the default WPF window icon if ShowInTaskBar is false, but displays correctly if ShowInTaskbar is true.
We want the icon to show up correctly in the Alt+Tab menu. How can we achieve this and have ShowInTaskbar = False?
There are several problems here. First of all, when ShowInTaskbar property is set to false, an invisible window gets created and assigned as a parent of current window. This invisible window's icon is displayed when switching between windows.
You can catch that window with Interop and set it's icon like this:
private void Window_Loaded(object sender, RoutedEventArgs e) {
SetParentIcon();
}
private void SetParentIcon() {
WindowInteropHelper ih = new WindowInteropHelper(this);
if(this.Owner == null && ih.Owner != IntPtr.Zero) { //We've found the invisible window
System.Drawing.Icon icon = new System.Drawing.Icon("ApplicationIcon.ico");
SendMessage(ih.Owner, 0x80 /*WM_SETICON*/, (IntPtr)1 /*ICON_LARGE*/, icon.Handle); //Change invisible window's icon
}
}
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
The other problems for you to think about would be:
Find out what happens when ShowInTaskbar property changes at runtime;
Extract an icon from your window rather than from file;