Thread: transparent windows - SetLayeredWindowAttributes

  1. #1
    sockets mad
    Join Date
    Mar 2002
    Posts
    126

    transparent windows - SetLayeredWindowAttributes

    I'm attempting to try and learn how to use the new alphablended/layered window techniques introduced into Windows 2000 and later but I'm having a bit of trouble

    I found an Article in MSDN called "Layered Windows" http://msdn.microsoft.com/library/de...l/layerwin.asp and it gave some code to insert into WM_INITDIALOG message processing for a dialog box to make it appear as opaque

    // Set WS_EX_LAYERED on this window
    SetWindowLong(hwnd, GWL_EXSTYLE,
    GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
    // Make this window 70% alpha
    SetLayeredWindowAttributes(hwnd, 0, (255 * 70) / 100, LWA_ALPHA);

    I thought I'd try this to begin with but I then recieved the error "Undeclared identifier SetLayeredWindowAttributes" and a few others when it came to compile.

    In MSDN it says it is declared in Winuser.h; Include Windows.h which I have done. I opened up \platformsdk\include\Winuser.h in notepad and also found it in there.

    I use Microsoft Visual Studio .NET Enterprise Arhitect and it says at the bottom of the MSDN collection "Platform SDK Release Auguest 2001" which I think should include these functions especially since SetLayeredWindowAttributes is documented within it.

    Any help with this problem would be greatly appreciated. Also, if anyone has used the layered window functions for 2k/xp then any tips regarding that would also be helpful.

    Thanks in advance,

    Daniel Briley
    [email protected]

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    This should be in the Windows forum. Try placing

    #define _WIN32_WINNT 0x0500

    in the file, before you include windows.h.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Sorensen
    This should be in the Windows forum. Try placing

    #define _WIN32_WINNT 0x0500

    in the file, before you include windows.h.
    That wont do the job if he/she has an older version of the SDK....

    There is a work around that involves getting a pointer to the API function at runtime yourself. Also you must declare all the macros as well as your heders will not have them declared

    Try this for size

    Code:
    #include <windows.h>
    
    /* Declare Macros*/
    #ifndef WS_EX_LAYERED
    #define WS_EX_LAYERED           0x00080000
    #define LWA_COLORKEY            0x00000001
    #define LWA_ALPHA               0x00000002
    #endif 
    
    /*typedef a pointer to the API function*/
    typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
    
    lpfnSetLayeredWindowAttributes m_pSetLayeredWindowAttributes;
    
    
    
    LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
    
    char szClassName[] = "WindowsApp";
    HINSTANCE g_hInst;
    
    
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
    
    {
        HWND hwnd;               
    	MSG messages;            
    	WNDCLASSEX wincl; 
    
                /*Load dll that holds the API func*/
    	HMODULE hUser32 = GetModuleHandle("USER32.DLL");
    	
                /*Grab pointer to API*/
                m_pSetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32, "SetLayeredWindowAttributes");
    
    
    	if (NULL == m_pSetLayeredWindowAttributes)
    	MessageBox(NULL,"Error!","",MB_OK);
                return 1;
    
    	g_hInst = hThisInstance;
    
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProc;      
        wincl.style = CS_DBLCLKS;               
        wincl.cbSize = sizeof(WNDCLASSEX);
        wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0; 
        wincl.cbWndExtra = 0; 
    	wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
    
        
        if(!RegisterClassEx(&wincl)) return 0;
    
       
        hwnd = CreateWindowEx(0,szClassName, "Windows App",
    		WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
    		CW_USEDEFAULT,CW_USEDEFAULT, HWND_DESKTOP,NULL,            
               hThisInstance,NULL );    
    
       
        ShowWindow(hwnd, nFunsterStil);
    	UpdateWindow(hwnd);
       
        while(GetMessage(&messages, NULL, 0, 0))
        {
              TranslateMessage(&messages);
              DispatchMessage(&messages);
        }
    
        return messages.wParam;
    }
    
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, 
    							WPARAM wParam, LPARAM lParam)
    {
        switch (message)                
        {
               case WM_CREATE: 
    			SetWindowLong(hwnd, GWL_EXSTYLE,
    				GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
                                        /*Use pointer to function*/
    			m_pSetLayeredWindowAttributes(hwnd, 0, 
    				(255 * 70) / 100, LWA_ALPHA);
               
               break;
               
               case WM_PAINT:
               
               break;
               
               case WM_DESTROY:
               PostQuitMessage(0);        
               break;
               default:                   
               return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }

  4. #4
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Originally posted by Sorensen
    This should be in the Windows forum. Try placing

    #define _WIN32_WINNT 0x0500

    in the file, before you include windows.h.
    thanks, i think that might have something to do with it. When looking through the winuser.h file i did see that the declaration of the SetLayeredWindowAttributes API Call was contained in some preprocessor if statements to do with _WIN32_WINNT

    would this have anything to do with me using Windows XP (NT 5.1) rather than 2k (5.0)?

    I'm a "he" btw

    just going to go away and play around with the suggestions and thanks rob ford for making such a helpful and long post, much appreciated

    Daniel

  5. #5
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Thanks a lot, I went away and tried adding that #define preprocessor directive and it worked perfectly, would seem that August 2001 edition of the SDK is ok as I thought it would be but thanks for the help anyway Rob.

    If possible could you explain why i had to put that directive in my code so I can understand why I have done it, it would be very helpful.

    Thanks again,

    Daniel

    PS - I'll remember to post in the Windows Forum next time, sorry about that

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by codec

    If possible could you explain why i had to put that directive in my code so I can understand why I have done it, it would be very helpful.
    Which?

    Code:
    #define _WIN32_WINNT 0x0500
    - this tells the compiler that the target platform will at least be Win2000......this is because transparent layering did not exist in any of the Win95 family or WinNT 4 and below...

    Code:
    #ifndef WS_EX_LAYERED
    #define WS_EX_LAYERED           0x00080000
    #define LWA_COLORKEY            0x00000001
    #define LWA_ALPHA               0x00000002
    #endif
    
    /*typedef a pointer to the API function*/
    typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
    
    lpfnSetLayeredWindowAttributes m_pSetLayeredWindowAttributes;
    This first of all defined the macros as the hexadecimal values that the functions would expect......it first checks to see if the definition is already there....if not it declares them.....

    The typedef is simply declaring a pointer to a function......it says what params the function will recieve and what return value it will return...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone using Windows 7?
    By Sharke in forum General Discussions
    Replies: 60
    Last Post: 07-12-2009, 08:05 AM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  3. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM