CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Transparent windows

    One of our clients is using some kind of transparent window app which sits on top of our app. Their app has some extra controls and it also has some kind of transparent section (so it looks like our app is somehow part of their app). If they move their mouse over the transparent section, various controls in our app will 'light up' as the mouse passes over them (just like they would normally). And if they click on a certain control, the click gets passed to our app which does whatever it would do normally.

    The problem is that once they've clicked on a control, further mouse events seem to stop getting passed to our app (as if the transparent window itself has now captured the input focus). I don't have any experience with transparent windows but I gather they're a feature of Windows Forms and .NET

    Is there anyone here who's used transparent windows who can tell me if the above behaviour is what they'd expect?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Transparent windows

    Quote Originally Posted by John E View Post
    I don't have any experience with transparent windows but I gather they're a feature of Windows Forms and .NET
    They're a feature of Windows, and exposed by the Windows API (as well as .NET).

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Transparent windows

    Thanks Arjay. In my experiments I tried building an older style transparent window - i.e. one which displays a bitmap with a bitmap mask which makes some of the pixels transparent (or in this case, all of them). The older style transparency seems to work as expected. But in our client's program, the window has some kind of property which makes it transparent (no bitmaps are involved).

    Unfortunately, our client's program is written in C# rather than C++ so I don't quite understand what's going on. Fortunately, the window doesn't need to be resizable - so I've just told them to try the older style window.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Transparent windows

    When it's the customer's app and you don't have access to the source code, it's going to be tough to try to figure out what they are doing wrong. But I don't have to tell you that.

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Transparent windows

    Quote Originally Posted by John E View Post
    The older style transparency seems to work as expected. But in our client's program, the window has some kind of property which makes it transparent (no bitmaps are involved).
    What you call older style transparency must be a region-based window which makes the window shape be following the shape of non-transparent bitmap regions. This naturally lets user input events come to underlying window, as the "transparent" regions do not belong to upper window.

    However the layered window transparency introduced in WinXP only makes the window look transparent (while keeping rect shape alright), but accepts all user input events in any its region, no matter transparent or not. This sort of window must artificially re-route user input and focus to the window it covers, or make sure it has WS_EX_NOACTIVATE claimed.
    Best regards,
    Igor

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Transparent windows

    Thanks Igor. Our client has sent me a cut-down version of their transparent app to experiment with. That's the good news...

    The bad news is that it's written in C# rather than C++ so I'm struggling to understand things at the moment. However, I can't see any evidence that C# uses those C-style properties, such as WS_EX_NOACTIVATE. Any idea how I'd add the NOACTIVATE property to a C# window?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Transparent windows

    Use pinvoke and the SetWindowLong to set the style. http://www.pinvoke.net/default.aspx/...indowLong.html

  8. #8
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Transparent windows

    Hmmm... I'm struggling

    According to that article it looked like I needed to add 2 lines in order to use SetWindowLong()

    Code:
        public partial class MainWindow : Window
        {
            // These 2 lines added by me:-
            [DllImport("user32.dll")]
            static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    
            // rest of class
        }
    But after adding those lines, a build now gives me these errors:-

    Code:
    error CS0246: The type or namespace name 'DllImportAttribute' could not be found (are you missing a using directive or an assembly reference?)
    error CS0246: The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    I know absolutely nothing about C# so I don't know what I've done wrong...
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Transparent windows

    Much to learn you still have, my young padawan, but learn you will.

    Add
    Code:
    using System;
    using System.Runtime.InteropServices;

  10. #10
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Transparent windows

    Thanks Arjay. That now compiles and links - but of course, when I later come to call SetWindowLong() I'll need to pass the HWND for my top-level window. Does C# have a way to obtain that HWND? Even in C++ it's a bit convoluted from what I can recall (I've been using GTK+ for the past 10 years so I've forgotten a lot of of this MS specific stuff )
    Last edited by John E; December 6th, 2016 at 03:50 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Transparent windows

    Even in C++ it's a bit convoluted from what I can recall
    Using c with WIN32, there is FindWindow() if you know the window title . See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    Or to enumerate all the top level windows there is EnumWindows(). See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    As I'm like you and don't program in c#, I don't know if there is a c# way of doing this...

    PS This may be of use as it relates to c#
    https://code.msdn.microsoft.com/wind...level-9aa9d7c1
    http://stackoverflow.com/questions/4...indows-using-c
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Transparent windows

    Quote Originally Posted by John E View Post
    Thanks Arjay. That now compiles and links - but of course, when I later come to call SetWindowLong() I'll need to pass the HWND for my top-level window. Does C# have a way to obtain that HWND? Even in C++ it's a bit convoluted from what I can recall (I've been using GTK+ for the past 10 years so I've forgotten a lot of of this MS specific stuff )
    It's the .Handle property of the form. Use Spy to retrieve the handle of the window you are interested in and match it with the corresponding .Handle to the corresponding parent/child form. I say parent/child because you may have to figure out which form window is the one you need to set the style on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured