CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    c++ - how can i do a transparent form?

    i continue with problems for make the form transparent
    see these code:
    Code:
    SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
                        SetLayeredWindowAttributes(hwnd, clrBackColor, NULL, LWA_COLORKEY);
    heres how i create the form:
    Code:
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE|CS_HREDRAW | CS_VREDRAW , classname, strCaption.c_str(), WS_OVERLAPPEDWINDOW | BS_NOTIFY,
                                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, parent, NULL, mod, this);
    the form can be transparent, but, in same cases, i can lose the events form...i don't understand why... can anyone explain more about it, please?

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

    Re: c++ - how can i do a transparent form?

    Code:
    WS_EX_CLIENTEDGE|CS_HREDRAW | CS_VREDRAW
    This is definitely not what you think it is. Here you need only Extended Window Styles. You need CS_ styles in your windows class registration only. And this is the place where you should use WS_EX_LAYERED.

    Code:
    WS_OVERLAPPEDWINDOW | BS_NOTIFY
    Again, only Window Class Styles expected here, and button styles must be used only with button controls.

    Please read MSDN articles carefully prior to putting any API in use. Now it looks like you play a sort of programming casino throwing dice and waiting for a jackpot.
    Best regards,
    Igor

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: c++ - how can i do a transparent form?

    Quote Originally Posted by Igor Vartanov View Post
    Code:
    WS_EX_CLIENTEDGE|CS_HREDRAW | CS_VREDRAW
    This is definitely not what you think it is. Here you need only Extended Window Styles. You need CS_ styles in your windows class registration only. And this is the place where you should use WS_EX_LAYERED.

    Code:
    WS_OVERLAPPEDWINDOW | BS_NOTIFY
    Again, only Window Class Styles expected here, and button styles must be used only with button controls.

    Please read MSDN articles carefully prior to putting any API in use. Now it looks like you play a sort of programming casino throwing dice and waiting for a jackpot.
    true.. thanks for correct me:
    Code:
    hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, classname, strCaption.c_str(), WS_OVERLAPPEDWINDOW|CS_HREDRAW | CS_VREDRAW,
                                    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, parent, NULL, mod, this);
    if i create the form with WS_EX_LAYERED , the form is showed invisible.
    but i continue with some strange effects
    see how i put the form transparent:
    Code:
    property<color> BackColor
            {
                Get(color)
                {
                    return clrBackColor;
                },
                Set(color clrColor)
                {
    
    
                    if(clrBackColor!=clrColor && clrColor==-1)//if the color is changed and '-1'(transparent)
                    {
                        SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
                        SetLayeredWindowAttributes(hwnd, clrBackColor, Opacy, LWA_COLORKEY|LWA_ALPHA);
                        UpdateWindow(hwnd);
                        clrBackColor=-1;
                    }
                    else if(clrBackColor!=clrColor) //if the color is changed
                    {
                        SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) |(~ WS_EX_LAYERED));
                        SetLayeredWindowAttributes(hwnd, -1, Opacy, LWA_COLORKEY | LWA_ALPHA);
                        UpdateWindow(hwnd);
                        SetClassLongPtr(hwnd,GCLP_HBRBACKGROUND, (LONG)CreateSolidBrush(clrBackColor));
                        InvalidateRect(hwnd,NULL,true);
                    }
    
                }
            };
    is these code correct for add and take of the WS_EX_LAYERED style?
    i'm getting only black color

  4. #4
    Join Date
    Apr 2009
    Posts
    1,355

    Re: c++ - how can i do a transparent form?

    now i understand why, when i create the form with WS_EX_LAYERED, i get the form invisible
    by defauld, the opacy is '0'(zero).. so i did these:
    Code:
    hwnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_LAYERED, classname, strCaption.c_str(), WS_OVERLAPPEDWINDOW | CS_HREDRAW | CS_VREDRAW ,
                                      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, parent, NULL, mod, this);
    
                    SetLayeredWindowAttributes(hwnd, NULL, 255, LWA_ALPHA);
    now the form is showed with WS_EX_LAYERED style.
    now i must see why i only get 1 color(black) on backcolor

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: c++ - how can i do a transparent form?

    before i could change the backcolor with these code:
    Code:
    SetClassLongPtr(hwnd,GCLP_HBRBACKGROUND, (LONG)CreateSolidBrush(clrBackColor));
    now i only recive the black color and not red\green
    why these visual error?

  6. #6
    Join Date
    Apr 2009
    Posts
    1,355

    Re: c++ - how can i do a transparent form?

    now i found the error:
    Code:
    property<color> BackColor
            {
                Get(color)
                {
                    return clrBackColor;
                },
                Set(color clrColor)
                {
                    if(clrBackColor!=clrColor && clrColor==-1)//if the color is changed and '-1'(transparent)
                    {
                        SetLayeredWindowAttributes(hwnd, clrBackColor, Opacy, LWA_COLORKEY|LWA_ALPHA);
                        clrBackColor=-1;
                    }
                    else if(clrBackColor!=clrColor) //if the color is changed
                    {
                        SetClassLongPtr(hwnd,GCLP_HBRBACKGROUND, (LONG)CreateSolidBrush(clrColor));
                        InvalidateRect(hwnd,NULL,true);
                        clrBackColor=clrColor;
                    }
                }
            };
    i wasn't change the clrBackColor value and not use the clrColor variable. now works fine

  7. #7
    Join Date
    Apr 2009
    Posts
    1,355

    Re: c++ - how can i do a transparent form?

    for finish, i have 1 question: why, when i maximizate the form, i lose the caption bar and borders actions?(like resize the caption menu and that 3 buttons on form)

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

    Re: c++ - how can i do a transparent form?

    I have to admit that I fail to understand your question. But whatever effect you run into, this would definitely have nothing to do with layered window transparency.
    Best regards,
    Igor

  9. #9
    Join Date
    Apr 2009
    Posts
    1,355

    Re: c++ - how can i do a transparent form?

    Quote Originally Posted by Igor Vartanov View Post
    I have to admit that I fail to understand your question. But whatever effect you run into, this would definitely have nothing to do with layered window transparency.
    just test yourself:
    1 - put the form transparent;
    2 - now maximize the form;
    3 - can you click:
    - on close button?('X');
    - on icon menu?;
    - and the others 2 buttons?
    the caption bar is showed, but without any action activated.

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