Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to get the window state?

How to get the window state?

Scheduled Pinned Locked Moved C#
testingbeta-testingtutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sheela Krishnan
    wrote on last edited by
    #1

    Hi, I am trying to get the windowstate of a window based on its process id. Below is what I tried... But the GetWindowPlacement method returns false most of the times inspite of the length of the windowplacvement object being initialized. Even if the method returns true, its showCmd is always NORMAL. While testing my window was always MAXIMIZED! Can anyone point what I am missing...? Can anyone explain how the window state is maintained. ...... Thanks! FormWindowState GetWindowState(int processID) { FormWindowState formWindowState = FormWindowState.Normal; WINDOWPLACEMENT windowPlacement = new WINDOWPLACEMENT(); //If there is not proces by this ID, then return Minimized. uint processHandle = (uint)Process.GetProcessById(processID).Handle; if ( processHandle.Equals(0) ) { return FormWindowState.Minimized; } windowPlacement.length = Marshal.SizeOf(windowPlacement); bool test = Win32Wrapper.GetWindowPlacement(processHandle,ref windowPlacement); if ( windowPlacement.showCmd == SHOW_STATE.SW_SHOWMINIMIZED ) { formWindowState = FormWindowState.Minimized; } else if ( windowPlacement.showCmd == SHOW_STATE.SW_SHOWMAXIMIZED ) { formWindowState = FormWindowState.Maximized; } }

    D 1 Reply Last reply
    0
    • S Sheela Krishnan

      Hi, I am trying to get the windowstate of a window based on its process id. Below is what I tried... But the GetWindowPlacement method returns false most of the times inspite of the length of the windowplacvement object being initialized. Even if the method returns true, its showCmd is always NORMAL. While testing my window was always MAXIMIZED! Can anyone point what I am missing...? Can anyone explain how the window state is maintained. ...... Thanks! FormWindowState GetWindowState(int processID) { FormWindowState formWindowState = FormWindowState.Normal; WINDOWPLACEMENT windowPlacement = new WINDOWPLACEMENT(); //If there is not proces by this ID, then return Minimized. uint processHandle = (uint)Process.GetProcessById(processID).Handle; if ( processHandle.Equals(0) ) { return FormWindowState.Minimized; } windowPlacement.length = Marshal.SizeOf(windowPlacement); bool test = Win32Wrapper.GetWindowPlacement(processHandle,ref windowPlacement); if ( windowPlacement.showCmd == SHOW_STATE.SW_SHOWMINIMIZED ) { formWindowState = FormWindowState.Minimized; } else if ( windowPlacement.showCmd == SHOW_STATE.SW_SHOWMAXIMIZED ) { formWindowState = FormWindowState.Maximized; } }

      D Offline
      D Offline
      Dennis C Dietrich
      wrote on last edited by
      #2

      Sheela Krishnan wrote: I am trying to get the windowstate of a window based on its process id. Below is what I tried... But the GetWindowPlacement method returns false most of the times inspite of the length of the windowplacvement object being initialized. Even if the method returns true, its showCmd is always NORMAL. While testing my window was always MAXIMIZED! Assuming that Win32Wrapper.GetWindowPlacement() indeed does only encapsulate the native Win32 call, you're simply passing the wrong value. GetWindowPlacement()[^] expects a handle to a window, not to a process. If you're interested in the state of the main window of a process you can use the Process.MainWindowHandle Property[^] to obtain the correct handle. Best regards Dennis

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups