CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2012

    Child Window with a Transparent Background

    Hello, I am new to Windows programming. I am currently attempting to port an application from Mac OS to Windows. The application is written in C. This is what I have done so far: I have installed Windows 8 CP and Visual Studio 11 beta on a laptop. I have found an example from msdn called: "Direct2D desktop app printing sample".
    I have decided to make it the starting point of my new application. Just because I don't like C++ too much, I have translated most of the code in C (but still keeping the .cpp extension). It works. The first true modification is that I want to add a second window and it must have a transparent background. The Mac application I am porting has two windows, although the user is not aware of this fact. The main window is opaque and draws a very complex plot. The overlay window is on top of it and hilights the selection and draws other temporary effects. When the selection changes, there is no need to draw the plot again (it would be extremely time-consuming).
    The Windows example I have found creates a main window and a child window. Drawing is perfomed into the latter with Direct2D. I have created 2 child windows. In both cases I set wcex.hbrBackground = nullptr;
    I have tried to intercept the message WM_ERASEBKGND but it does not arrive to my childs. My starting example start drawing with:
        d2dContext->Clear( D2D1::ColorF( D2D1::ColorF::White ) );
    If I omit the Clear instruction the background becomes black. If I use a transparent color the background is still black. I have created the second window with the style WS_TRANSPARENT.
    I have two questions, if anybody is so kind to help me.
    1. Is my approach correct? Is it possible to update one of the child windows without forcing the sibling to redraw itself? Or do I need two top-level windows?
    2. How can I get a transparent background? The example that I have found is quite complicated. It creates the D2D1 device, its bitmap and a lot of other objects. Maybe I should change one of the properties of one of those objects.... maybe not.

  2. #2
    Join Date
    Jun 2011
    Buenos Aires, Argentina

    Re: Child Window with a Transparent Background

    Hi, welcome! I'm actually having a similar problem and was just about to post! I've been trying to create a new form from my main form to highlight an area of the main form, and would need it to be semi-transparent to show the user where to look but without hiding what I'm trying to show. The problem is, no mather how I set the new form's properties, it will not show semi-transparent!!

    Not sure if this will help you, but this is what I have so far:

    1. What I'm doing (and it's not working) is, on the child form, set:
    .AllowTransparency = true;
    .Opacity = 0.5 // 50%
    .BackColor = Color.Blue;
    .TransparencyKey = Color.Blue; // Same as BackColor
    That should show a translucent window, but it doesn't work for me.

    2. Other options
    MSDN article on transparent forms
    Transparent borderless forms
    Very basic tutorial on transparent forms

    A final mention goes to this article on Forms transparent to mouse events that doesn't have much to do with optical visibility but might be helpful anyway.

    Hope it helps... and if you manage to show a transparent form from another one, please let me know!

  3. #3
    Join Date
    May 2012

    Re: Child Window with a Transparent Background

    Not, I have not found a complete solution yet. Just because this is a performance issue, I have decided to redraw everything. The final effect is the same, but the program can become very slow is some cases. An alternative would be to save the static part of the drawing as a bitmap, instead of repeating it every time. That optimization would be equivalent to what I was expecting from the operating system. In this moment I have already turned my attention to other details of my program. I will return to the optimization much later on.

  4. #4
    Join Date
    Feb 2012

    Re: Child Window with a Transparent Background

    hi world !

    [ autoit ] ( Popup )
    #include <GuiConstants.au3>
    $Main_GUI = GUICreate("Main")
    $Btn_Exit = GUICtrlCreateButton("E&xit", 10, 10, 90, 20)
    GUISetState(@SW_SHOW, $Main_GUI)
    $Child_GUI = GUICreate("Child", 200, 100, 10, 50);, $WS_POPUP );,$WS_EX_LAYERED
    GUISetBkColor(0xfffaf0, $Child_GUI)
    $Btn_Test = GUICtrlCreateButton("Test", 10, 10, 90, 20)
    GUISetState(@SW_SHOW, $Child_GUI)
    DllCall("user32.dll", "int", "SetParent", "hwnd", WinGetHandle($Child_GUI), "hwnd", WinGetHandle($Main_GUI))

    While 1
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE, $Btn_Exit
    Case $Btn_Test
    MsgBox(0, "Test", "Hit Button on Child Window")

    the code is exactly what i need: a child window stuck within the parent. but i need a transparent (transparent background, not set transparency) child without borders.

    but how, since $WS_EX_LAYERED does not work with child windows ?



Tags for this Thread

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.