Advertisement
Guest User

C++ layered transparent window - main.h

a guest
Dec 21st, 2010
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1.  
  2. // nur für Win 2000 und später kompilieren
  3. #define WINVER 0x0500
  4.  
  5. #include <windows.h>
  6. #include "main.h"
  7.  
  8. //using namespace std;
  9.  
  10. int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) {
  11.     HWND hwnd;          // Main-Window handle
  12.     MSG messages;           /* Here messages to the application are saved */
  13.     WNDCLASSEX wincl;       /* Data structure for the windowclass */   
  14.    
  15.     Init();
  16.  
  17.     /* The Window structure */
  18.     wincl.hInstance = hThisInstance;
  19.     wincl.lpszClassName = szClassName;
  20.     wincl.lpfnWndProc = WindowProcedure;    /* This function is called by windows */
  21.     wincl.style = CS_DBLCLKS;               /* Catch double-clicks */
  22.     wincl.cbSize = sizeof (WNDCLASSEX);
  23.  
  24.     /* Use default icon and mouse-pointer */
  25.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  26.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  27.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  28.     wincl.lpszMenuName = NULL;              /* No menu */
  29.     wincl.cbClsExtra = 0;                   /* No extra bytes after the window class */
  30.     wincl.cbWndExtra = 0;                   /* structure or the window instance */
  31.     /* Use Windows's default color as the background of the window */
  32.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  33.  
  34.     /* Register the window class, and if it fails quit the program */
  35.     if (!RegisterClassEx (&wincl))
  36.         return 0;
  37.  
  38.     hwnd = CreateWindowEx( // siehe http://msdn.microsoft.com/en-us/library/ms632680(v=vs.85).aspx
  39.         0,
  40.         szClassName,
  41.         "Test...",                  // Titel
  42.         WS_SYSMENU|WS_POPUP,        // Window-Long / Style
  43.         50,                         // position x (default: CW_USEDEFAULT)
  44.         200,                        // position y (default: CW_USEDEFAULT)
  45.         WINDOW_WIDTH,               // Breite
  46.         WINDOW_HEIGHT,              // Höhe
  47.         HWND_DESKTOP,               // Kind vom Desktop
  48.         NULL,                       // Kein Menü
  49.         hThisInstance,              // Program Instance handler
  50.         NULL
  51.     );
  52.    
  53.     // Long des Fensters auf LAYER setzen, damit es transparent werden kann (WS_EX_TRANSPARENT macht das Fenster komplett transparent)
  54.     SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
  55.    
  56.     // Alpha-Wert des Fensters setzen
  57.     //SetLayeredWindowAttributes(hwnd, 0, (255 * 70) / 100, LWA_ALPHA);
  58.  
  59.     // Nur bestimmten Ausschnitt zeigen
  60.     //SetWindowRgn(hwnd, CreateRectRgn(10,20,200,60), false);
  61.  
  62.     ShowWindow(hwnd, nFunsterStil); // Fenster sichtbar machen
  63.  
  64.  
  65.  
  66.     // Message-Loop (läuft solange wie GestMessage != 0 ist)
  67.     while (GetMessage (&messages, NULL, 0, 0)) {
  68.         TranslateMessage(&messages);     // virtual-key messagse to character messages
  69.         DispatchMessage(&messages);     // Event behandeln
  70.     }
  71.    
  72.     return messages.wParam;
  73. }
  74.  
  75.  
  76.  
  77. void Init() {
  78.    
  79.     col = RGB(128,0,0);
  80.     ColorRect.left = 10;
  81.     ColorRect.top = 120;
  82.     ColorRect.right = 290;
  83.     ColorRect.bottom = 160;
  84.  
  85. }
  86.  
  87.  
  88.  
  89. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  90. {
  91.    
  92.     if (message == WM_PAINT) {
  93.         PAINTSTRUCT ps;
  94.         brush = CreateSolidBrush(col);
  95.         HDC dc;
  96.         dc = NULL;
  97.         dc = BeginPaint(hwnd, &ps);
  98.         SelectObject(dc, brush);
  99.         FillRect(dc, &ColorRect, brush);
  100.         EndPaint(hwnd, &ps);
  101.     } else if (message == WM_DESTROY) {
  102.         DeleteObject(brush);
  103.         PostQuitMessage(0); // WM_QUIT zur message queue senden
  104.     } else {
  105.         return DefWindowProc(hwnd, message, wParam, lParam); // die message weitersenden damit evtl. andere Programme die message handeln können
  106.     }
  107.    
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement