Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7722341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:08:47+00:00 2026-06-01T04:08:47+00:00

I am trying to minimize the window with the title located in the string

  • 0

I am trying to minimize the window with the title located in the string p (specified by the user at runtime). The window is guaranteed to be a main window, because the user can only choose from main windows. I have tried showCmd, flags, and a mixture of the two, but every time, regardless of whether the window is minimized, I am shown the dialog box that states "Minimizing". How can I fix it?

private void button1_Click(object sender, EventArgs e)
{
    foreach (Process proc in Process.GetProcesses())
    {
        if (proc.MainWindowTitle.Contains(p))
        {
            IntPtr handle = proc.Handle;
            Program.WINDOWPLACEMENT wp = new Program.WINDOWPLACEMENT();
            Program.GetWindowPlacement(handle, ref wp);
            if ((wp.showCmd & 2) == 2)
            {
                wp.showCmd = 9;
                MessageBox.Show("Restoring");
            }
            else
            {
                wp.showCmd = 2;
                MessageBox.Show("Minimizing");
            }
            wp.length = Marshal.SizeOf(wp);
            Program.SetWindowPlacement(handle, ref wp);
            break;
        }
    }  

}

What I’m using as p –

string p;
PictureBox i;
bool windowShowing = false;
bool minimized = false;
public TaskbarItem(string window)
{
    InitializeComponent();
    p = window;
    button1.Text = window;
}

To get the window:

class WindowEnumerator
{
    public delegate bool EnumDelegate(IntPtr hWnd, int lParam);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsWindowVisible(IntPtr hWnd);

    [DllImport("user32.dll", EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

    [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);


    public static List<IntPtr> GetChildWindows(IntPtr parent)
    {
        List<IntPtr> result = new List<IntPtr>();
        GCHandle listHandle = GCHandle.Alloc(result);
        try
        {
            EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
            EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
        }
        finally
        {
            if (listHandle.IsAllocated)
                listHandle.Free();
        }
        return result;
    }
    public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

    public static LinkedList<IntPtr> EnumMethod2(bool includeChild)
    {
        LinkedList<IntPtr> pss = new LinkedList<IntPtr>();
        Process[] ps = Process.GetProcesses();
        foreach (Process p in ps)
        {
            string s = p.MainWindowTitle;
            if (s.Equals("") || s.Equals("Mod_Taskbar"))
            {
                continue;
            }
            LinkedListNode<IntPtr> node = new LinkedListNode<IntPtr>(p.MainWindowHandle);
            pss.AddLast(node);
            if (includeChild){
                List<IntPtr> l = GetChildWindows(p.MainWindowHandle);
                foreach (IntPtr i in l)
                {
                    StringBuilder stb = new StringBuilder(255);
                    WindowEnumerator.GetWindowText(i, stb, 255);
                    if (stb.ToString().StartsWith("Address:") || stb.ToString().EndsWith("View") || stb.ToString().EndsWith("Control") ||
                        stb.ToString().EndsWith("Bar") || stb.ToString().EndsWith("bar") || stb.ToString().Contains("Host"))
                    {
                        continue;
                    }
                    if (Vars.excludes.Contains(stb.ToString()))
                    {
                        continue;
                    }
                    pss.AddAfter(node, i);
                }
            }
        }
        return pss;
    }
    private static bool EnumWindow(IntPtr handle, IntPtr pointer)
    {
        GCHandle gch = GCHandle.FromIntPtr(pointer);
        List<IntPtr> list = gch.Target as List<IntPtr>;
        list.Add(handle);
        return true;
    }
}

And:

private void LoadWindows(object sender, EventArgs e)
{
    panel1.Controls.Clear();
    foreach (IntPtr p in WindowEnumerator.EnumMethod2(false))
    {
        StringBuilder stb = new StringBuilder(255);
        WindowEnumerator.GetWindowText(p, stb, 255);
        TaskbarItem t = new TaskbarItem(stb.ToString());
        t.Dock = DockStyle.Left;
        panel1.Controls.Add(t);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:08:49+00:00Added an answer on June 1, 2026 at 4:08 am

    Did you check what value the wp.showCmd has?

    Try with this code:

    foreach (Process proc in Process.GetProcesses()) 
    { 
        if (proc.MainWindowTitle.Contains(p)) 
        { 
            IntPtr handle = proc.Handle; 
            Program.WINDOWPLACEMENT wp = new Program.WINDOWPLACEMENT(); 
            Program.GetWindowPlacement(handle, ref wp); 
            if ((wp.showCmd & 2) == 2) 
            { 
                wp.showCmd = 9; 
                MessageBox.Show("Restoring"); 
            } 
            else 
            { 
                wp.showCmd = 2; 
                MessageBox.Show("Minimizing"); 
            } 
            wp.length = Marshal.SizeOf(wp); 
            Program.SetWindowPlacement(handle, ref wp); 
            break; 
        } 
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to minimize a window to the tray, but it seems it refuses
I am trying to get showstate of a window. I know that I can
I've been trying to minimize all my functions in my app but can't really
Instance A is trying to restore instance B's window, but I can't get the
I am trying to minimize the performance penalty of communicating across AppDomains in the
I am trying to minimize the height of the table shown below. Firebug tells
I'm trying to find a way to maximize & minimize a certain application using
i am trying to get a c# winforms application to startup only in the
I am trying to hook in to the messages of a window to detect
I'm trying to animate my window's location. SCENARIO : I have a window, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.