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. Way to retain focus of transparent form application.

Way to retain focus of transparent form application.

Scheduled Pinned Locked Moved C#
csharpquestion
10 Posts 5 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.
  • P Offline
    P Offline
    parth_patel
    wrote on last edited by
    #1

    In C#, if I make a transparent windows form using transparency key colored "control", then on clicking anywhere on screen(while application is in focus), transfers focus of the app to the one behind it, which is normal behaviour. If I want the application to retain the focus, is it possible to do anyhow ?

    L C 2 Replies Last reply
    0
    • P parth_patel

      In C#, if I make a transparent windows form using transparency key colored "control", then on clicking anywhere on screen(while application is in focus), transfers focus of the app to the one behind it, which is normal behaviour. If I want the application to retain the focus, is it possible to do anyhow ?

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

      Use a timer to set focus? Maybe P/Invoke to SetWindowFocus?

      Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

      P 1 Reply Last reply
      0
      • L Lost User

        Use a timer to set focus? Maybe P/Invoke to SetWindowFocus?

        Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

        P Offline
        P Offline
        parth_patel
        wrote on last edited by
        #3

        I do not want the window to loose focus at all, because user will be drawing on this transparent window. Isn't there a straightforward way to do it, than to hack/tweak things?

        L D 2 Replies Last reply
        0
        • P parth_patel

          I do not want the window to loose focus at all, because user will be drawing on this transparent window. Isn't there a straightforward way to do it, than to hack/tweak things?

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

          Did you try setting the topmost property to true?

          Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

          P 1 Reply Last reply
          0
          • P parth_patel

            In C#, if I make a transparent windows form using transparency key colored "control", then on clicking anywhere on screen(while application is in focus), transfers focus of the app to the one behind it, which is normal behaviour. If I want the application to retain the focus, is it possible to do anyhow ?

            C Offline
            C Offline
            crayzeecoder
            wrote on last edited by
            #5

            I got this from MSDN before

                \[DllImport("user32.dll")\]
                private extern static IntPtr SetActiveWindow(IntPtr handle);
                private const int WM\_ACTIVATE = 6;
                private const int WA\_INACTIVE = 0;
            
            
                protected override void WndProc(ref Message m)
                {
                    if (m.Msg == WM\_ACTIVATE)
                    {
                        if (((int)m.WParam & 0xFFFF) != WA\_INACTIVE)
                        {
                            if (m.LParam != IntPtr.Zero)
                            {
                                SetActiveWindow(m.LParam);
                            }
                            else
                            {
                                SetActiveWindow(IntPtr.Zero);
                            }
                        }
                    }
            
                    base.WndProc(ref m);
                } 
            

            I cant find the link for it though so I just found it in my own code found the link[^] :-D

            modified on Friday, June 19, 2009 3:39 PM

            P 1 Reply Last reply
            0
            • P parth_patel

              I do not want the window to loose focus at all, because user will be drawing on this transparent window. Isn't there a straightforward way to do it, than to hack/tweak things?

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              How about skipping all this garbage and taking a screen shot and drawing on that instead. No need to worry about transparency and mouse clicks going to the wrong form at all.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              P 1 Reply Last reply
              0
              • C crayzeecoder

                I got this from MSDN before

                    \[DllImport("user32.dll")\]
                    private extern static IntPtr SetActiveWindow(IntPtr handle);
                    private const int WM\_ACTIVATE = 6;
                    private const int WA\_INACTIVE = 0;
                
                
                    protected override void WndProc(ref Message m)
                    {
                        if (m.Msg == WM\_ACTIVATE)
                        {
                            if (((int)m.WParam & 0xFFFF) != WA\_INACTIVE)
                            {
                                if (m.LParam != IntPtr.Zero)
                                {
                                    SetActiveWindow(m.LParam);
                                }
                                else
                                {
                                    SetActiveWindow(IntPtr.Zero);
                                }
                            }
                        }
                
                        base.WndProc(ref m);
                    } 
                

                I cant find the link for it though so I just found it in my own code found the link[^] :-D

                modified on Friday, June 19, 2009 3:39 PM

                P Offline
                P Offline
                parth_patel
                wrote on last edited by
                #7

                This does not work. I tried this one as well as the [DllImport("user32", CharSet = CharSet.Auto)] private extern static int SendMessage( IntPtr handle, int msg, int wParam, IntPtr lParam); [DllImport("user32", CharSet = CharSet.Auto)] private extern static int PostMessage( IntPtr handle, int msg, int wParam, IntPtr lParam); private const int WM_ACTIVATE = 0x006; private const int WM_ACTIVATEAPP = 0x01C; private const int WM_NCACTIVATE = 0x086; /// <summary> /// Subclasses the owning form's existing Window Procedure to enables the /// title bar to remain active when a popup is show, and to detect if /// the user clicks onto another application whilst the popup is visible. /// </summary> /// <param name="m">Window Procedure Message</param> protected override void WndProc(ref Message m) { // check for WM_ACTIVATEAPP and WM_NCACTIVATE if (m.Msg == WM_NCACTIVATE) { // Check if the title bar will made inactive: if (((int)m.WParam) == 0) { xyz += m.Msg + " "; // If so reactivate it. SendMessage(this.Handle, WM_NCACTIVATE, 1, IntPtr.Zero); // Note it's no good to try and consume this message; // if you try to do that you'll end up with windows // that don't respond. } } else if (m.Msg == WM_ACTIVATEAPP) { // Check if the application is being deactivated. if ((int)m.WParam == 0) { xyz += m.Msg + " "; // And put the title bar into the inactive state: PostMessage(this.Handle, WM_NCACTIVATE, 0, IntPtr.Zero); } } base.WndProc(ref m); } code. Later code actually does the reverse of what I want, but still it does not work. Neither works.

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  How about skipping all this garbage and taking a screen shot and drawing on that instead. No need to worry about transparency and mouse clicks going to the wrong form at all.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  P Offline
                  P Offline
                  parth_patel
                  wrote on last edited by
                  #8

                  Yes, that's an option I am considering right now. But if I could make it draw directly on screen it would have been much more better. Thanks, Parth

                  1 Reply Last reply
                  0
                  • L Lost User

                    Did you try setting the topmost property to true?

                    Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

                    P Offline
                    P Offline
                    parth_patel
                    wrote on last edited by
                    #9

                    Yes, I tried that. It is not effective.

                    S 1 Reply Last reply
                    0
                    • P parth_patel

                      Yes, I tried that. It is not effective.

                      S Offline
                      S Offline
                      S Senthil Kumar
                      wrote on last edited by
                      #10

                      Why doesn't that work?

                      Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                      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