Solved

how to make transparent control in win32 window

Posted on 2006-11-13
19
1,089 Views
Last Modified: 2013-11-20
Hi
i created a dialog in win32 and on that i have some bitmaps.

now i want to make some bitmap transparent.

Please help me

Raj
0
Comment
Question by:Raj_Kau
  • 8
  • 6
  • 2
19 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 17931021
Check out

http://www.codeproject.com/dialog/wintrans1.asp ("Making any application transparent in Windows 2000/XP")

and

http://www.codeproject.com/bitmap/trans_cimage.asp ("Drawing transparent bitmaps using CImage")
0
 
LVL 3

Author Comment

by:Raj_Kau
ID: 17935640
hi jkr
it make transparent but they make full window transparent not the particular cotrol or bitmap.

Raj
0
 
LVL 86

Expert Comment

by:jkr
ID: 17935660
Have you checked the 2nd article also?
0
 
LVL 3

Author Comment

by:Raj_Kau
ID: 17943842
jkr
i told you again what i want. i think that help u to solve my problem.

actually i want to make diffrent control whith diffrent percentage and i also want to make transparent to my window also.

i think now u help me better


Raj
0
 
LVL 1

Expert Comment

by:Mazen
ID: 17954179
Hello Raj,

  I suggest that you use PNG Images, they could be transparent images and can be loaded to win32 applications directly using GDI+.

However, if u insist on using BMP images and u need to make it transparent, you can use the function(alpha blended):

SetLayeredWindowAttributes(hwnd, 0, (255 * 70) / 100, LWA_ALPHA);

where 70 indicates that the opacity of the image is 70%. In this case, i think u need to handle every BMP as a seperate window.
Here's a link to explain how to use it:

http://msdn2.microsoft.com/en-us/library/ms997507.aspx
0
 
LVL 1

Expert Comment

by:Mazen
ID: 17954199
Going again through your comments, I want to add this: you can pass the handle of every control seperatley to this function and change the opacity of every control.

  Hope this helps.

     MJ
0
 
LVL 3

Author Comment

by:Raj_Kau
ID: 17962835
Mazen

i already try your second suggestion but its not working.


Raj
0
 
LVL 1

Expert Comment

by:Mazen
ID: 17963364
Hello Raj,

I have tried a simple application and it seems that this doesn't work if the control has WS_CHILD style. If you remove this style the controls will appear transparent, but you face other problems in this case which are related to the prosition of the controls and  resizement and movement of the parent window. I think what you can do is handle all window movement and resizement messages and relocate the controls your self.  
Note, you should use CreateWindowEx() when trying to create controls and use the extended style WS_EX_LAYERED.

I can post some code if you would like to try it or I can email u the application I worte.

     Mazen

   
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Author Comment

by:Raj_Kau
ID: 17970597
thanks mazen
that look cool. i also will try what you suggest but please you also send.


Raj
0
 
LVL 1

Assisted Solution

by:Mazen
Mazen earned 125 total points
ID: 17970686
Hello Raj,

Here's a sample code:
      RECT rt;
      GetWindowRect(hWnd, &rt);
         hButton1 = CreateWindowEx(WS_EX_LAYERED |
            WS_EX_TRANSPARENT |
             WS_EX_TOOLWINDOW, "Button","Button 1", WS_POPUP | WS_VISIBLE ,
                               rt.left + 50, rt.top+50,
                               100, 30,
                               hWnd, NULL,
                               hInstance,
                               NULL);
      hButton2 = CreateWindowEx(WS_EX_LAYERED |
            WS_EX_TRANSPARENT |
             WS_EX_TOOLWINDOW, "Button","Button 2", WS_BORDER | WS_POPUP | WS_VISIBLE | BS_PUSHBUTTON,
                               rt.left+ 200, rt.top+ 200,
                               100, 30,
                               hWnd, NULL,
                               hInstance,
                               NULL);

                        SetLayeredWindowAttributes(hButton1, 0, (255 * 70) / 100, LWA_ALPHA);
                        SetLayeredWindowAttributes(hButton2, 0, (255 * 70) / 100, LWA_ALPHA);

Don't forget to import the library User32.lib inorder to use the function SetLayeredWindowsAttributes, here's what you can write:
//define this in the header:

#ifndef WS_EX_LAYERED
#define WS_EX_LAYERED           0x00080000
#define LWA_COLORKEY            0x00000001
#define LWA_ALPHA               0x00000002
#endif
typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd,
                                  COLORREF crKey, BYTE bAlpha, DWORD dwFlags);

lpfnSetLayeredWindowAttributes SetLayeredWindowAttributes;
/**********************/
//and this inside any intailizing function:
      HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL"));
SetLayeredWindowAttributes =
                       (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32,
                       "SetLayeredWindowAttributes");

Try it and let me know. Try loading an image in the background inorder to see the transparency of the controls.

    Mazen
0
 
LVL 3

Author Comment

by:Raj_Kau
ID: 18017535
mazen
 sorry for late response. that code work but i do not want to use CreateWindowEx.

can you tell me how i load PNG image.

Raj
0
 
LVL 1

Expert Comment

by:Mazen
ID: 18017564
Hello Raj,

  Here's the code that can load png images or any image type:

HDC hdc = GetDC(hWnd);
CImage m_image;
m_image.Load("c:/1.png");
m_image.Draw(hdc,0,0);
ReleaseDC(hWnd,hdc);

You have to include this inorder for CImage to work:

#include "atlimage.h"

  Regards,
     Mazen
0
 
LVL 3

Author Comment

by:Raj_Kau
ID: 18018015
Mazen
i did not able to include
#include "atlimage.h"
when i include this it give fatal error
then how i can show this image into a static control..



Raj
0
 
LVL 1

Expert Comment

by:Mazen
ID: 18018058
Raj,

Are u using VC++ 6.0? Because if so, CImage is not supported and you will get a "fatal error" if you include atlimage.h . I'll post a code that uses GDI+ in this case to load images.

If you are using .net version of visual studio, then post to me the part where u included the atlimage.h library.

Waiting for your reply.

   Regards,
     Mazen

0
 
LVL 3

Author Comment

by:Raj_Kau
ID: 18018121
Mazen

Yes i use VC++ 6.0.


Raj
0
 
LVL 3

Author Comment

by:Raj_Kau
ID: 18018125
Mazen

can you have the idea about "AlphaBlend"

Raj
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Expand data scrubbing tool 13 31
How to know only "File created" with EventLog 3 29
twoTwo  challenge 35 87
Thin secure Windows 10 5 49
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now