Solved

how to make transparent control in win32 window

Posted on 2006-11-13
19
1,114 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 78
substring method in java 1 120
ffmpeg - "rtsp://...... Operation not permitted" 4 80
Problem to App source 6 40
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 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