CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2004
    Posts
    5

    gradually changing opacity

    API function SetLayeredWindowAttributes sets the opacity for a window. Is it possible (and how) to make the window opacity level gradually change from the centre to the borders. For instance, can i set the opacity 95 percent for the centre and make it go down to 10% to the borders?
    Thanks!

  2. #2
    Join Date
    Jan 2004
    Posts
    56
    I dont know if there's a function like that. How about imitate it by making ur own 'alpha-blitting' function? There're alot of 'alpha-blitting' algorithms on Google.
    Trust urself!

  3. #3
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384
    Originally posted by sephiroth2m
    I dont know if there's a function like that. How about imitate it by making ur own 'alpha-blitting' function? There're alot of 'alpha-blitting' algorithms on Google.
    Hi sephiroth2m,
    Can you please post some URLs, I tired alpha-belneding on google it but it didnt return anything useful.
    Thanks in advance,
    Regards,
    Usman.
    Last edited by usman999_1; May 28th, 2004 at 12:38 PM.

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    How would you imitate it?
    I mean, that can't possible work always correctly.
    What happens when the window in the back is changed? You should somehow capture that and re-blit your window?

    Or am I missing something?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Apr 2004
    Posts
    5
    Thanks for your replies. I go the solution (with some help )
    Have a look:
    Code:
    #define _WIN32_WINNT 0x0500
    #define UNICODE
    #include <windows.h>
    #include <gdiplus.h>
    using namespace Gdiplus;
    
    const INT iWidth=400,iHeight=700;
    HBITMAP hBitmap;
    
    VOID OnPaint(HDC hdc){
    HDC hDC_BMP=CreateCompatibleDC(hdc);
    SelectObject(hDC_BMP,hBitmap);
    BitBlt(hdc,0,0,iWidth,iHeight,hDC_BMP,0,0,SRCCOPY);
    DeleteObject(hDC_BMP);
    }
    
    
    VOID CreateTrBitmap(HDC hdc){
    
    HDC hDC_BMP=CreateCompatibleDC(hdc);
    Bitmap *bmp=new Bitmap(iWidth,iHeight);
    Graphics gr(bmp);
    gr.Clear(Color(0,255,255,255));
    GraphicsPath path;
    path.AddEllipse(0, 0, iWidth, iHeight);
    PathGradientBrush pthGrBrush(&path);
    pthGrBrush.SetCenterColor(Color(0, 0, 0, 255));
    Color colors[] = {Color(255, 100,100,0)};
    int count = 1;
    pthGrBrush.SetSurroundColors(colors, &count);
    gr.FillEllipse(&pthGrBrush, 0, 0, iWidth, iHeight);
    bmp->GetHBITMAP(Color(0,0,0,0),&hBitmap);
    delete bmp;
    DeleteObject(hDC_BMP);
    
    }
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
    {
    	
       HWND                hWnd;
       MSG                 msg;
       WNDCLASS            wndClass;
       GdiplusStartupInput gdiplusStartupInput;
       ULONG_PTR           gdiplusToken;
       
       // Initialize GDI+.
       GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
       
       wndClass.style          = CS_HREDRAW | CS_VREDRAW;
       wndClass.lpfnWndProc    = WndProc;
       wndClass.cbClsExtra     = 0;
       wndClass.cbWndExtra     = 0;
       wndClass.hInstance      = hInstance;
       wndClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
       wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
       wndClass.hbrBackground  = (HBRUSH)GetStockObject(NULL_BRUSH);
       wndClass.lpszMenuName   = NULL;
       wndClass.lpszClassName  = TEXT("GettingStarted");
       
       RegisterClass(&wndClass);
       hWnd = CreateWindowEx(
    	   WS_EX_LAYERED,
          TEXT("GettingStarted"),   // window class name
          TEXT("Getting Started"),  // window caption
          WS_OVERLAPPEDWINDOW,      // window style
          CW_USEDEFAULT,            // initial x position
          CW_USEDEFAULT,            // initial y position
         iWidth,            // initial x size
          iHeight,            // initial y size
          NULL,                     // parent window handle
          NULL,                     // window menu handle
          hInstance,                // program instance handle
          NULL);                    // creation parameters
    	  
       ShowWindow(hWnd, iCmdShow);
       UpdateWindow(hWnd);
       
       while(GetMessage(&msg, NULL, 0, 0))
       {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
       }
       
       GdiplusShutdown(gdiplusToken);
       return msg.wParam;
    }  // WinMain
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
       WPARAM wParam, LPARAM lParam)
    {
    	HDC          hdc=NULL;
       PAINTSTRUCT  ps;
        BLENDFUNCTION blend;
    	POINT ptSrc={0,0};HDC hDC_BMP;
    	SIZE szDefault;
    	szDefault.cx=iWidth;
    	szDefault.cy=iHeight;
       switch(message)
       {
    
       case WM_CREATE:
           CreateTrBitmap(GetDC(hWnd));
           hDC_BMP=CreateCompatibleDC(hdc);
          SelectObject(hDC_BMP,hBitmap);
    	   blend.BlendOp=AC_SRC_OVER;
    	   blend.BlendFlags=0;
    	   blend.SourceConstantAlpha=255;
    	   blend.AlphaFormat=AC_SRC_ALPHA;
    	  if( UpdateLayeredWindow(hWnd,
    		NULL,&ptSrc,&szDefault,hDC_BMP,&ptSrc,NULL,&blend,
           ULW_ALPHA)==0)
    	   MessageBox(NULL,TEXT("Update failed!"),TEXT(""),MB_OK);
    	   DeleteObject(hDC_BMP);
    	   break;
    /*****************************************
    WM_PAINT NEVER GETS CALLED
       case WM_PAINT:
          hdc = BeginPaint(hWnd, &ps);
         OnPaint(hdc);
          EndPaint(hWnd, &ps);
          return 0;
    	  ************************/
       case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
       default:
          return DefWindowProc(hWnd, message, wParam, lParam);
       }
    } // WndProc

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    Yes, UpdateLayeredWindow is a solution, but it makes it a lot more difficult to display standard windows objects etc...
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

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