Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

mfc-dimmer

By: a guest on Nov 2nd, 2010  |  syntax: C++  |  size: 4.36 KB  |  views: 83  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. /**********************************************************************************************
  3.        
  4.         MFC screen dim test
  5.                 :: oystein                      :: November 2010
  6.                
  7.         Creates a simple window - click it to toggle whether a translucent black "dimmer" window
  8.         is shown. The dimmer-window covers the entire screen, but the taskbar ("superbar" in
  9.         Windows 7) will jump on top of it if clicked - it seems. Simple suggestions to fix that
  10.         are welcome.
  11.        
  12.         Should work on Windows 2000 and later.
  13.  
  14.         Disclaimer: This is my first MFC program ever, so if anything seems wrong, it probably is.
  15.         I have previously only coded with pure Win32 API, and hacked this together using online
  16.         tutorials. Code provided "as-is" with no guarantees - I can not be held responsible for
  17.         anything bad that happens if you run this program.
  18.  
  19. ***********************************************************************************************/
  20.  
  21. #include "stdafx.h"
  22.  
  23. #undef WINVER
  24. #define WINVER 0x500 // Windows 2000 & above, because of layered windows
  25.  
  26.  
  27. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  28. //
  29. //                                               Black window used to dim everything else
  30. //
  31. class CDimWnd : public CFrameWnd
  32. {              
  33. public:
  34.         CDimWnd()
  35.         {
  36.                 // Get screen res into rect
  37.                 RECT rc;
  38.                 GetDesktopWindow()->GetWindowRect(&rc);
  39.  
  40.                 CreateEx(WS_EX_LAYERED |                // Layered window for translucency
  41.                                  WS_EX_TRANSPARENT |    // Click through
  42.                                  WS_EX_TOPMOST |                // Always on top
  43.                                  WS_EX_TOOLWINDOW,              // Do not appear in taskbar & similar
  44.                                  NULL, TEXT(""),
  45.                                  WS_POPUP,                              // No frame/borders - though there is
  46.                                                                                 // still some border left - we'll remove
  47.                                                                                 // it with regions
  48.  
  49.                                  0, 0, rc.right + 10, rc.bottom + 10, // Make the window 10px larger
  50.                                                                                                           // than screen resolution in both
  51.                                                                                                           // directions - it is still positioned
  52.                                                                                                           // at 0,0
  53.                                  NULL, NULL);
  54.                
  55.                 // Grab a part of the window the size of the desktop - but 5px into it  
  56.                 // Because the window is larger than the desktop res, the borders are removed
  57.                 CRgn rgn;                        
  58.                 rgn.CreateRectRgn(rc.left + 5, rc.top + 5, rc.right + 5, rc.bottom + 5);
  59.                 SetWindowRgn((HRGN)rgn, FALSE);
  60.                 rgn.Detach();                              
  61.                
  62.                 // We have to reposition window - (0,0) of window has not changed
  63.                 SetWindowPos(NULL, -5, -5, 0, 0, SWP_NOSIZE | SWP_NOZORDER);           
  64.  
  65.                 // This is where we set the opacity of the window: 0-255
  66.                 SetLayeredWindowAttributes(RGB(0,0,0), 150, LWA_ALPHA);                                        
  67.         }
  68.         void Close()
  69.         {
  70.                 CFrameWnd::OnClose();
  71.         }
  72.         BOOL CDimWnd::OnEraseBkgnd(CDC* pDC); // Set BKG color
  73.         DECLARE_MESSAGE_MAP()
  74. };
  75.  
  76. BOOL CDimWnd::OnEraseBkgnd(CDC* pDC)
  77. {
  78.     // Set brush to desired background color
  79.     CBrush backBrush(RGB(0, 0, 0));
  80.  
  81.     // Save old brush
  82.     CBrush* pOldBrush = pDC->SelectObject(&backBrush);
  83.  
  84.     CRect rect;
  85.     pDC->GetClipBox(&rect);     // Erase the area needed
  86.  
  87.     pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
  88.     pDC->SelectObject(pOldBrush);      
  89.     return TRUE;
  90. }
  91.  
  92. BEGIN_MESSAGE_MAP(CDimWnd, CFrameWnd)
  93.         ON_WM_ERASEBKGND()
  94. END_MESSAGE_MAP()
  95.  
  96. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  97.  
  98.  
  99. // Global variable - is screen dimmed?
  100. bool g_bIsDimmed = false;
  101.  
  102.  
  103. // The main window
  104. class CMainWnd : public CFrameWnd
  105. {    
  106.         // Contains a CDimWnd - I'm not sure if this is the "MFC way" of doing things
  107.         CDimWnd dimmer;
  108.        
  109. public:
  110.         CMainWnd()
  111.         {
  112.                 Create(NULL, TEXT("Screen dimmer - Press left mouse button on window to toggle"),
  113.                         WS_OVERLAPPEDWINDOW, CRect(50, 50, 400, 250));
  114.         }
  115.         // Left mouse button toggles dimming
  116.         afx_msg void OnLButtonDown(UINT Flags, CPoint Point)
  117.         {
  118.                 if(!g_bIsDimmed)
  119.                 {
  120.                         dimmer.ShowWindow(SW_SHOW);
  121.                         dimmer.BringWindowToTop();                     
  122.                         g_bIsDimmed = true;
  123.                 }
  124.                 else
  125.                 {                      
  126.                         dimmer.ShowWindow(SW_HIDE);            
  127.                         g_bIsDimmed = false;
  128.                 }
  129.         }
  130.         DECLARE_MESSAGE_MAP()
  131. };
  132.  
  133. BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
  134.         ON_WM_LBUTTONDOWN()
  135. END_MESSAGE_MAP()
  136.  
  137.  
  138. // The app
  139. class CApp : public CWinApp
  140. {
  141. public:        
  142.         virtual BOOL InitInstance();
  143. };
  144.  
  145. BOOL CApp::InitInstance()
  146. {              
  147.         m_pMainWnd = new CMainWnd();              
  148.         m_pMainWnd->ShowWindow(m_nCmdShow);          
  149.         m_pMainWnd->UpdateWindow();        
  150.         return TRUE;
  151. }
  152.  
  153. CApp HelloApp;