2009년 11월 6일 금요일

투명 윈도우 만들기

#define _WIN32_WINNT 0x500
#include <windows.h>
#include "resource.h"

 

#ifndef WS_EX_LAYERED
#define WS_EX_LAYERED 0x00080000
#define LWA_COLORKEY 0x00000001
#define LWA_ALPHA  0x00000002
#endif


typedef BOOL (WINAPI *lpSLWA)(HWND hWnd, COLORREF cr, BYTE bAlpha, DWORD dwFlags);

 

BOOL CALLBACK MainDlgProc(HWND hDlg,UINT iMessage,WPARAM wParam,LPARAM lParam);
HWND hDlgMain;

 

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
 ,LPSTR lpszCmdParam,int nCmdShow)
{
                 DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),

                                   HWND_DESKTOP, MainDlgProc);
 
 return 0;
}

 

static bool SetTransparentWindow(HWND hTransWindow, int transPercent, DWORD *color=NULL)
{
                HMODULE hUser32 = GetModuleHandle("USER32.DLL");
                if(hUser32 != NULL)
                {
                           lpSLWA pSetLayeredWindowAttributes = (lpSLWA)

                                   GetProcAddress(hUser32,"SetLayeredWindowAttributes");
                           if(pSetLayeredWindowAttributes != NULL)
                          {
                                 int wndStyle =

                                             GetWindowLong(hTransWindow,GWL_EXSTYLE);
                                 if(!(wndStyle & WS_EX_LAYERED))
                                                 SetWindowLong(hTransWindow,GWL_EXSTYLE,

                                                                                        wndStyle|WS_EX_LAYERED);

                                 if(color != NULL)
                                                pSetLayeredWindowAttributes(hTransWindow,

                                                 *color,transPercent,LWA_ALPHA|LWA_COLORKEY);
                                 else
                                                 pSetLayeredWindowAttributes(hTransWindow,0,

                                                                                          transPercent,LWA_ALPHA);
                                 return true;
                         }
                }
                return false;
  }

 

BOOL CALLBACK MainDlgProc(HWND hDlg,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
                HDC hdc;
                PAINTSTRUCT ps;
                HBRUSH hBrush, hOldBrush;
                static int iOpa=200;

                switch (iMessage) {
                               case WM_INITDIALOG:
                                               hDlgMain = hDlg;
                                               MessageBox(hDlg,"Hello_01!","Question",MB_OK);
                                               return TRUE;
                               case WM_COMMAND:
                                switch (LOWORD(wParam)) {
                                               case IDOK:
                                               case IDCANCEL:
                                                               MessageBox(hDlg,"Good Bye!",

                                                                                          "Question",MB_OK);
                                                               EndDialog(hDlgMain,0);
                                                               return TRUE;
                                               case IDC_BTNPLUS:
                                                               iOpa+=25;
                                                               if (iOpa > 255)
                                                                              iOpa=255;
                                                               SetTransparentWindow(hDlg,iOpa);
                                                               return TRUE;
                                               case IDC_BTNMINUS:
                                                               iOpa-=25;
                                                               if (iOpa < 50)
                                                                               iOpa=50;
                                                               SetTransparentWindow(hDlg,iOpa);
                                                               return FALSE;
                                               }
                                              case WM_PAINT:
                                                              hdc=BeginPaint(hDlg, &ps);
                                                              hBrush=CreateSolidBrush(RGB(0,0,255));
                                                              hOldBrush=

                                                                           (HBRUSH)SelectObject(hdc,hBrush);
                                                              SelectObject(hdc,

                                                                           GetStockObject(NULL_PEN));
                                                              Rectangle(hdc,10,10,300,80);
                                                              SelectObject(hdc,hOldBrush);
                                                              DeleteObject(hBrush);
                                                              EndPaint(hDlg, &ps);
                                                              return 0;
                               }
                return FALSE;
}

댓글 없음:

댓글 쓰기