#include "stdafx.h"
#include "TransparentWindow.h"
#include "TransparentWindowDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CTransparentWindowDlg::CTransparentWindowDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTransparentWindowDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTransparentWindowDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SLIDER_TRANS, transSlider_);
}
BEGIN_MESSAGE_MAP(CTransparentWindowDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_HSCROLL()
END_MESSAGE_MAP()
// CTransparentWindowDlg message handlers
BOOL CTransparentWindowDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
//设置分层属性
::SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE,
GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE)|WS_EX_LAYERED);
//设置透明度 0 - completely transparent 255 - opaque
::SetLayeredWindowAttributes(GetSafeHwnd(), 0, 200, LWA_ALPHA);
transSlider_.SetRange(0, 255);
transSlider_.SetPos(200);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTransparentWindowDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
DrawBkgnd();
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTransparentWindowDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CTransparentWindowDlg::OnHScroll(UINT nSBCode,
UINT nPos,
CScrollBar* pScrollBar)
{
::SetLayeredWindowAttributes(GetSafeHwnd(), 0,
transSlider_.GetPos(), LWA_ALPHA);
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CTransparentWindowDlg::DrawBkgnd()
{
PAINTSTRUCT ps;
HDC hdc = ::BeginPaint(GetSafeHwnd(), &ps);
//HDC hdc = ::GetDC(m_hWnd);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hBmp = (HBITMAP)LoadImage(AfxGetInstanceHandle(),
_T("D:\\Sample.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
BITMAP bmp;
::GetObject(hBmp, sizeof(BITMAP), &bmp);
::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0,
bmp.bmWidth, bmp.bmHeight, SWP_NOMOVE);
HBITMAP hBmpOld = (HBITMAP)SelectObject(hdcMem, hBmp);
BitBlt(hdc, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hBmpOld);
DeleteDC(hdcMem);
::EndPaint(GetSafeHwnd(), &ps);
//::ReleaseDC(m_hWnd, hdc);
}
- 1
- 2
前往页