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
  1. Home
  2. General Programming
  3. C#
  4. Problem with setwindowPOS and returning from topmost to standard window position?

Problem with setwindowPOS and returning from topmost to standard window position?

Scheduled Pinned Locked Moved C#
questionhelp
19 Posts 3 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.
  • L Lost User

    Fine, but we cannot guess what your code is actually doing.

    T Offline
    T Offline
    turbosupramk3
    wrote on last edited by
    #7

    Here is the button click function, would you like me to copy/paste the entire code?

    private void btnSelectTypingBox_Click(object sender, EventArgs e)
    {
    try
    {
    if (tempBlocker == false)
    {
    ////MessageBox.Show("1");
    if (btnSelectTypingBox.Text == "Select Box")
    {
    btnSelectTypingBox.Text = "Waiting";
    tempBlocker = false;
    SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); // set window foreground to top most window
    }
    else if (btnSelectTypingBox.Text == "Waiting")
    {
    btnSelectTypingBox.Text = "Select Box";

                    }
                }         
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " | " + ex.Source + " | " + ex.InnerException + " | " + ex.StackTrace);
            }
        }
    
    L 1 Reply Last reply
    0
    • T turbosupramk3

      Here is the button click function, would you like me to copy/paste the entire code?

      private void btnSelectTypingBox_Click(object sender, EventArgs e)
      {
      try
      {
      if (tempBlocker == false)
      {
      ////MessageBox.Show("1");
      if (btnSelectTypingBox.Text == "Select Box")
      {
      btnSelectTypingBox.Text = "Waiting";
      tempBlocker = false;
      SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); // set window foreground to top most window
      }
      else if (btnSelectTypingBox.Text == "Waiting")
      {
      btnSelectTypingBox.Text = "Select Box";

                      }
                  }         
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message + " | " + ex.Source + " | " + ex.InnerException + " | " + ex.StackTrace);
              }
          }
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #8

      How about

      SetWindowPos(this.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE| SWP_NOZORDER);

      ? some extra SWP values might be required... :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      T 1 Reply Last reply
      0
      • L Luc Pattyn

        How about

        SetWindowPos(this.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE| SWP_NOZORDER);

        ? some extra SWP values might be required... :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        T Offline
        T Offline
        turbosupramk3
        wrote on last edited by
        #9

        Hi Luc, Here is what I tried, but it did not work for me

        private void btnTest_Click(object sender, EventArgs e)
        {
        SetWindowPos(this.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
        }

        And here is my pininvoke code

        private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
        private static readonly IntPtr HWND_TOP = new IntPtr(0);

            private const UInt32 SWP\_NOSIZE = 0x0001;
            private const UInt32 SWP\_NOMOVE = 0x0002;
            private const UInt32 SWP\_NOZORDER = 0x0004;
            private const UInt32 SWP\_NOREDRAW = 0x0008;
            private const UInt32 SWP\_NOACTIVATE = 0x0010;
            private const UInt32 SWP\_DRAWFRAME = 0x0020;
            private const UInt32 SWP\_FRAMECHANGED = 0x0020;
            private const UInt32 SWP\_SHOWWINDOW = 0x0040;
            private const UInt32 SWP\_HIDEWINDOW = 0x0080;
            private const UInt32 SWP\_NOCOPYBITS = 0x0100;
            private const UInt32 SWP\_NOOWNERZORDER = 0x0200;
            private const UInt32 SWP\_NOREPOSITION = 0x0200;
            private const UInt32 SWP\_NOSENDCHANGING = 0x0400;
            private const UInt32 SWP\_DEFERERASE = 0x2000;
            private const UInt32 SWP\_ASYNCWINDOWPOS = 0x4000;
        
            private const UInt32 TOPMOST\_FLAGS = SWP\_NOMOVE | SWP\_NOSIZE;
            private const UInt32 NOTOPMOST\_FLAGS = SWP\_SHOWWINDOW;
        
            \[DllImport("user32.dll")\]
            \[return: MarshalAs(UnmanagedType.Bool)\]
            public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
        
        L L 2 Replies Last reply
        0
        • T turbosupramk3

          Hi Luc, Here is what I tried, but it did not work for me

          private void btnTest_Click(object sender, EventArgs e)
          {
          SetWindowPos(this.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
          }

          And here is my pininvoke code

          private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
          private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
          private static readonly IntPtr HWND_TOP = new IntPtr(0);

              private const UInt32 SWP\_NOSIZE = 0x0001;
              private const UInt32 SWP\_NOMOVE = 0x0002;
              private const UInt32 SWP\_NOZORDER = 0x0004;
              private const UInt32 SWP\_NOREDRAW = 0x0008;
              private const UInt32 SWP\_NOACTIVATE = 0x0010;
              private const UInt32 SWP\_DRAWFRAME = 0x0020;
              private const UInt32 SWP\_FRAMECHANGED = 0x0020;
              private const UInt32 SWP\_SHOWWINDOW = 0x0040;
              private const UInt32 SWP\_HIDEWINDOW = 0x0080;
              private const UInt32 SWP\_NOCOPYBITS = 0x0100;
              private const UInt32 SWP\_NOOWNERZORDER = 0x0200;
              private const UInt32 SWP\_NOREPOSITION = 0x0200;
              private const UInt32 SWP\_NOSENDCHANGING = 0x0400;
              private const UInt32 SWP\_DEFERERASE = 0x2000;
              private const UInt32 SWP\_ASYNCWINDOWPOS = 0x4000;
          
              private const UInt32 TOPMOST\_FLAGS = SWP\_NOMOVE | SWP\_NOSIZE;
              private const UInt32 NOTOPMOST\_FLAGS = SWP\_SHOWWINDOW;
          
              \[DllImport("user32.dll")\]
              \[return: MarshalAs(UnmanagedType.Bool)\]
              public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #10

          Hi, two things: 1. "did not work for me" isn't helpful; what did it do? and what did it not do that you were hoping to get? please be precise. 2. why is this in a new Click handler, I was expecting this to go in the else block, as counterpart to your SetWindowPos(this.Handle, HWND_TOPMOST...). :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          T 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, two things: 1. "did not work for me" isn't helpful; what did it do? and what did it not do that you were hoping to get? please be precise. 2. why is this in a new Click handler, I was expecting this to go in the else block, as counterpart to your SetWindowPos(this.Handle, HWND_TOPMOST...). :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            T Offline
            T Offline
            turbosupramk3
            wrote on last edited by
            #11

            It did not remove the topmost flag as the application is still set to topmost. I would like it to go from a topmost application back to a standard z ordered application as it is before the topmost flag is set. I was testing it with a test button for simplification is all, as soon as I get a working example I will then incorporate it into an else block

            1 Reply Last reply
            0
            • T turbosupramk3

              Hi Luc, Here is what I tried, but it did not work for me

              private void btnTest_Click(object sender, EventArgs e)
              {
              SetWindowPos(this.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
              }

              And here is my pininvoke code

              private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
              private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
              private static readonly IntPtr HWND_TOP = new IntPtr(0);

                  private const UInt32 SWP\_NOSIZE = 0x0001;
                  private const UInt32 SWP\_NOMOVE = 0x0002;
                  private const UInt32 SWP\_NOZORDER = 0x0004;
                  private const UInt32 SWP\_NOREDRAW = 0x0008;
                  private const UInt32 SWP\_NOACTIVATE = 0x0010;
                  private const UInt32 SWP\_DRAWFRAME = 0x0020;
                  private const UInt32 SWP\_FRAMECHANGED = 0x0020;
                  private const UInt32 SWP\_SHOWWINDOW = 0x0040;
                  private const UInt32 SWP\_HIDEWINDOW = 0x0080;
                  private const UInt32 SWP\_NOCOPYBITS = 0x0100;
                  private const UInt32 SWP\_NOOWNERZORDER = 0x0200;
                  private const UInt32 SWP\_NOREPOSITION = 0x0200;
                  private const UInt32 SWP\_NOSENDCHANGING = 0x0400;
                  private const UInt32 SWP\_DEFERERASE = 0x2000;
                  private const UInt32 SWP\_ASYNCWINDOWPOS = 0x4000;
              
                  private const UInt32 TOPMOST\_FLAGS = SWP\_NOMOVE | SWP\_NOSIZE;
                  private const UInt32 NOTOPMOST\_FLAGS = SWP\_SHOWWINDOW;
              
                  \[DllImport("user32.dll")\]
                  \[return: MarshalAs(UnmanagedType.Bool)\]
                  public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #12

              The problem is that your X, Y, cx and cy values are all zero, and when you switch to NoTopMost, you have omitted the flags to keep the size and position. Edit your code and replace the definition of NOTOPMOST_FLAGS with the following.

                  private const UInt32 NOTOPMOST\_FLAGS = TOPMOST\_FLAGS | SWP\_SHOWWINDOW;
              

              Had you explained exactly what happened when you ran your code we could have helped earlier.

              T 1 Reply Last reply
              0
              • L Lost User

                The problem is that your X, Y, cx and cy values are all zero, and when you switch to NoTopMost, you have omitted the flags to keep the size and position. Edit your code and replace the definition of NOTOPMOST_FLAGS with the following.

                    private const UInt32 NOTOPMOST\_FLAGS = TOPMOST\_FLAGS | SWP\_SHOWWINDOW;
                

                Had you explained exactly what happened when you ran your code we could have helped earlier.

                T Offline
                T Offline
                turbosupramk3
                wrote on last edited by
                #13

                Thank you. Strangely enough when I have the above code in a separate button it removes the topmost flag correctly, but when I have it at the end of the send.keys function it does not remove the topmost flag? Any ideas on what could be causing that?

                L 1 Reply Last reply
                0
                • T turbosupramk3

                  Thank you. Strangely enough when I have the above code in a separate button it removes the topmost flag correctly, but when I have it at the end of the send.keys function it does not remove the topmost flag? Any ideas on what could be causing that?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #14

                  turbosupramk3 wrote:

                  what could be causing that?

                  Not without seeing the actual code.

                  T 1 Reply Last reply
                  0
                  • L Lost User

                    turbosupramk3 wrote:

                    what could be causing that?

                    Not without seeing the actual code.

                    T Offline
                    T Offline
                    turbosupramk3
                    wrote on last edited by
                    #15

                    Sure thing, the first block is my Pininvoke code, then the standard function and then the test button. The standard function code will put the form to topmost, but will not remove the topmost flag. The test button will remove the topmost flag after the standard function places the topmost flag.

                    private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
                    private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
                    private static readonly IntPtr HWND_TOP = new IntPtr(0);

                        private const UInt32 SWP\_NOSIZE = 0x0001;
                        private const UInt32 SWP\_NOMOVE = 0x0002;
                        private const UInt32 SWP\_NOZORDER = 0x0004;
                        private const UInt32 SWP\_NOREDRAW = 0x0008;
                        private const UInt32 SWP\_NOACTIVATE = 0x0010;
                        private const UInt32 SWP\_DRAWFRAME = 0x0020;
                        private const UInt32 SWP\_FRAMECHANGED = 0x0020;
                        private const UInt32 SWP\_SHOWWINDOW = 0x0040;
                        private const UInt32 SWP\_HIDEWINDOW = 0x0080;
                        private const UInt32 SWP\_NOCOPYBITS = 0x0100;
                        private const UInt32 SWP\_NOOWNERZORDER = 0x0200;
                        private const UInt32 SWP\_NOREPOSITION = 0x0200;
                        private const UInt32 SWP\_NOSENDCHANGING = 0x0400;
                        private const UInt32 SWP\_DEFERERASE = 0x2000;
                        private const UInt32 SWP\_ASYNCWINDOWPOS = 0x4000;
                    
                        private const UInt32 TOPMOST\_FLAGS = SWP\_NOMOVE | SWP\_NOSIZE;
                        private const UInt32 NOTOPMOST\_FLAGS = TOPMOST\_FLAGS | SWP\_SHOWWINDOW;
                    
                        \[DllImport("user32.dll")\]
                        \[return: MarshalAs(UnmanagedType.Bool)\]
                        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
                    
                        private void btnType\_Click(object sender, EventArgs e)
                        {
                            try
                            {
                                if (tbxTagList.Text == "")
                                {
                                    MessageBox.Show("Please enter in at least 1 tag");
                                    return;
                                }
                    
                                int x = Convert.ToInt32(tbxXCoordinates.Text);
                                int y = Convert.ToInt32(tbxYCoordinates.Text);
                                SimulateMouseClick(x, y);
                                foreach (char character in tbxTagList.Text)
                                {
                    
                                    if ((character != '\\n') && (character != '\\r'))
                                    {
                                        SendKeys.Send(character.ToString());
                                    }
                    
                                    if (character == '\\n')
                                    {
                                        SendKeys.Send("{Enter}
                    
                    L 1 Reply Last reply
                    0
                    • T turbosupramk3

                      Sure thing, the first block is my Pininvoke code, then the standard function and then the test button. The standard function code will put the form to topmost, but will not remove the topmost flag. The test button will remove the topmost flag after the standard function places the topmost flag.

                      private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
                      private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
                      private static readonly IntPtr HWND_TOP = new IntPtr(0);

                          private const UInt32 SWP\_NOSIZE = 0x0001;
                          private const UInt32 SWP\_NOMOVE = 0x0002;
                          private const UInt32 SWP\_NOZORDER = 0x0004;
                          private const UInt32 SWP\_NOREDRAW = 0x0008;
                          private const UInt32 SWP\_NOACTIVATE = 0x0010;
                          private const UInt32 SWP\_DRAWFRAME = 0x0020;
                          private const UInt32 SWP\_FRAMECHANGED = 0x0020;
                          private const UInt32 SWP\_SHOWWINDOW = 0x0040;
                          private const UInt32 SWP\_HIDEWINDOW = 0x0080;
                          private const UInt32 SWP\_NOCOPYBITS = 0x0100;
                          private const UInt32 SWP\_NOOWNERZORDER = 0x0200;
                          private const UInt32 SWP\_NOREPOSITION = 0x0200;
                          private const UInt32 SWP\_NOSENDCHANGING = 0x0400;
                          private const UInt32 SWP\_DEFERERASE = 0x2000;
                          private const UInt32 SWP\_ASYNCWINDOWPOS = 0x4000;
                      
                          private const UInt32 TOPMOST\_FLAGS = SWP\_NOMOVE | SWP\_NOSIZE;
                          private const UInt32 NOTOPMOST\_FLAGS = TOPMOST\_FLAGS | SWP\_SHOWWINDOW;
                      
                          \[DllImport("user32.dll")\]
                          \[return: MarshalAs(UnmanagedType.Bool)\]
                          public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
                      
                          private void btnType\_Click(object sender, EventArgs e)
                          {
                              try
                              {
                                  if (tbxTagList.Text == "")
                                  {
                                      MessageBox.Show("Please enter in at least 1 tag");
                                      return;
                                  }
                      
                                  int x = Convert.ToInt32(tbxXCoordinates.Text);
                                  int y = Convert.ToInt32(tbxYCoordinates.Text);
                                  SimulateMouseClick(x, y);
                                  foreach (char character in tbxTagList.Text)
                                  {
                      
                                      if ((character != '\\n') && (character != '\\r'))
                                      {
                                          SendKeys.Send(character.ToString());
                                      }
                      
                                      if (character == '\\n')
                                      {
                                          SendKeys.Send("{Enter}
                      
                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #16

                      I cannot see anything wrong with that code, and the documentation does not give any further ideas.

                      T 2 Replies Last reply
                      0
                      • L Lost User

                        I cannot see anything wrong with that code, and the documentation does not give any further ideas.

                        T Offline
                        T Offline
                        turbosupramk3
                        wrote on last edited by
                        #17

                        Ok, thank you for trying!

                        1 Reply Last reply
                        0
                        • L Lost User

                          I cannot see anything wrong with that code, and the documentation does not give any further ideas.

                          T Offline
                          T Offline
                          turbosupramk3
                          wrote on last edited by
                          #18

                          I don't know why, but the addition of the thread sleep as pictured below fixed it?

                                      tbxXCoordinates.Text = "";
                                      tbxYCoordinates.Text = "";
                                      
                                      Thread.Sleep(250);
                                      SetWindowPos(this.Handle, HWND\_NOTOPMOST, 0, 0, 0, 0, NOTOPMOST\_FLAGS);
                          
                          L 1 Reply Last reply
                          0
                          • T turbosupramk3

                            I don't know why, but the addition of the thread sleep as pictured below fixed it?

                                        tbxXCoordinates.Text = "";
                                        tbxYCoordinates.Text = "";
                                        
                                        Thread.Sleep(250);
                                        SetWindowPos(this.Handle, HWND\_NOTOPMOST, 0, 0, 0, 0, NOTOPMOST\_FLAGS);
                            
                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #19

                            I wonder if using SendKeys.SendWait Method (String) (System.Windows.Forms)[^] instead of Send might have the same effect?

                            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