【VC开源代码栏目提醒】:网学会员VC开源代码为您提供TransparencyDlg.cpp参考,解决您在TransparencyDlg.cpp学习中工作中的难题,参考学习。
// TransparencyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Transparency.h"
#include "TransparencyDlg.h"
#include "Title.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// 全局变量
typedef BOOL (WINAPI *lpfn) (HWND hWnd, COLORREF cr, BYTE bAlpha, DWORD dwFlags);
lpfn g_pSetLayeredWindowAttributes;
#define WS_EX_LAYERED 0x00080000
#define LWA_ALPHA 2
/////////////////////////////////////////////////////////////////////////////
// CTransparencyDlg dialog
CTransparencyDlg::CTransparencyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTransparencyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTransparencyDlg)
m_sText = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bAlpha = 120;
m_hWnd = GetSafeHwnd();
}
void CTransparencyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTransparencyDlg)
DDX_Text(pDX, IDC_TEXT, m_sText);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTransparencyDlg, CDialog)
//{{AFX_MSG_MAP(CTransparencyDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OPEN, OnOpen)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_BN_CLICKED(IDC_UP, OnUp)
ON_BN_CLICKED(IDC_DOWN, OnDown)
ON_BN_CLICKED(IDC_OTHER, OnOther)
ON_BN_CLICKED(IDC_SELF, OnSelf)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTransparencyDlg message handlers
BOOL CTransparencyDlg::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
// 获取函数 SetLayeredWindowAttributes 在User32.dll中的指针
HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL"));
g_pSetLayeredWindowAttributes = (lpfn)GetProcAddress(hUser32, "SetLayeredWindowAttributes");
if (g_pSetLayeredWindowAttributes == NULL)
::PostQuitMessage(0);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTransparencyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (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
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTransparencyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTransparencyDlg::OnOpen()
{
CFileDialog dlg(TRUE, "*", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "文本
文档(*.txt)|*.txt||");
if(dlg.DoModal() == IDOK)
{
CFile file;
file.Open(dlg.GetPathName(), CFile::modeReadWrite);
char* buf = new char [file.GetLength()];
memset(buf, 0, file.GetLength());
file.Read(buf, file.GetLength());
m_sText = CString(buf);
file.Close();
UpdateData(FALSE);
}
}
void CTransparencyDlg::OnSave()
{
UpdateData(TRUE);
if (m_sText.GetLength() <= 0)
return;
CFileDialog dlg(FALSE, "*", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "文本
文档(*.txt)|*.txt||");
if(dlg.DoModal() == IDOK)
{
CFile file;
file.Open(dlg.GetPathName(), CFile::modeCreate | CFile::modeReadWrite);
file.Write(m_sText, m_sText.GetLength());
file.Close();
}
}
void CTransparencyDlg::OnUp()
{
if (m_bAlpha > 10)
{
m_bAlpha -= 10;
HideForm(m_hWnd);
}
}
void CTransparencyDlg::OnDown()
{
if (m_bAlpha < 250)
{
m_bAlpha += 10;
HideForm(m_hWnd);
}
}
void CTransparencyDlg::OnOther()
{
CTitle dlg;
if (dlg.DoModal() == IDOK)
{
m_hWnd = ::FindWindow(NULL, dlg.m_sTitle);
if (m_hWnd == NULL)
m_hWnd = GetSafeHwnd();
HideFo