Click here to Skip to main content
15,893,904 members
Articles / Programming Languages / C#

Set Window Action - (Minimize / Maximize)

Rate me:
Please Sign up or sign in to vote.
3.64/5 (25 votes)
22 Sep 2008CPOL 48.8K   707   15  
Application to minimize / maximize a window.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace WindowAction
{
    public partial class NotepadMinimize : Form
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

        private struct POINTAPI
        {
            public int x;
            public int y;
        }

        private struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        private struct WINDOWPLACEMENT
        {
            public int length;
            public int flags;
            public int showCmd;
            public POINTAPI ptMinPosition;
            public POINTAPI ptMaxPosition;
            public RECT rcNormalPosition;
        }


        public NotepadMinimize()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WindowAction_MinimizeNotepad();
        }

        void WindowAction_MinimizeNotepad()
        {
            System.IntPtr app_hwnd;
            WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
            app_hwnd = FindWindow("Notepad", null);
            GetWindowPlacement(app_hwnd, ref wp);
            wp.showCmd = 2;
            SetWindowPlacement(app_hwnd, ref wp);
        }

        private void minWindow()
        {
            Process[] processes = Process.GetProcessesByName(textBox1.Text.Trim());
            foreach (Process p in processes)
            {
                System.IntPtr app_hwnd;
                WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
                app_hwnd = p.MainWindowHandle;
                GetWindowPlacement(app_hwnd, ref wp);
                wp.showCmd = 2; // 1- Normal; 2- Minimize; 3-Maximize;
                SetWindowPlacement(app_hwnd, ref wp);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            minWindow();
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Proteans Software Solutions Pvt
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions