Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1328
  • Last Modified:

how to make transparent control in win32 window

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
Raj_Kau
Asked:
Raj_Kau
  • 8
  • 6
  • 2
2 Solutions
 
jkrCommented:
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
 
Raj_KauAuthor Commented:
hi jkr
it make transparent but they make full window transparent not the particular cotrol or bitmap.

Raj
0
 
jkrCommented:
Have you checked the 2nd article also?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Raj_KauAuthor Commented:
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
 
MazenCommented:
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
 
MazenCommented:
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
 
Raj_KauAuthor Commented:
Mazen

i already try your second suggestion but its not working.


Raj
0
 
MazenCommented:
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
 
Raj_KauAuthor Commented:
thanks mazen
that look cool. i also will try what you suggest but please you also send.


Raj
0
 
MazenCommented:
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
 
Raj_KauAuthor Commented:
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
 
MazenCommented:
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
 
Raj_KauAuthor Commented:
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
 
MazenCommented:
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
 
Raj_KauAuthor Commented:
Mazen

Yes i use VC++ 6.0.


Raj
0
 
Raj_KauAuthor Commented:
Mazen

can you have the idea about "AlphaBlend"

Raj
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 8
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now