www.pudn.com > 20041201221023_vczx_progtemplate.rar > Page2.cpp
// Page2.cpp : implementation file
//
#include "stdafx.h"
#include "Page.h"
#include "Page2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPage2 property page
IMPLEMENT_DYNCREATE(CPage2, CPropertyPage)
CPage2::CPage2() : CPropertyPage(CPage2::IDD)
{
//{{AFX_DATA_INIT(CPage2)
m_bAutoRun = (::GetPrivateProfileInt("Settings","bAutoRun",1,".\\Settings.ini")==0? false:true);
m_check_minimize =(::GetPrivateProfileInt("Settings","bMinimized",0,".\\Settings.ini")==0 ?false:true);
m_bTopMost = (::GetPrivateProfileInt("Settings","bTopMost",1,".\\Settings.ini")==0? false:true);
//}}AFX_DATA_INIT
}
CPage2::~CPage2()
{
}
void CPage2::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPage2)
DDX_Control(pDX, IDC_SLIDER1, m_sld);
DDX_Control(pDX, IDC_HOTKEY1, m_hotkey);
DDX_Check(pDX, IDC_CHECK_AUTORUN, m_bAutoRun);
DDX_Check(pDX, IDC_CHECK_MINIMIZE, m_check_minimize);
DDX_Check(pDX, IDC_CHECK_TOPMOST, m_bTopMost);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPage2, CPropertyPage)
//{{AFX_MSG_MAP(CPage2)
ON_BN_CLICKED(IDC_CHECK_TOPMOST, OnCheckTopmost)
ON_BN_CLICKED(IDC_CHECK_AUTORUN, OnCheckAutorun)
ON_BN_CLICKED(IDC_CHECK_MINIMIZE, OnCheckMinimize)
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER1, OnReleasedcaptureSlider1)
ON_WM_HSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPage2 message handlers
void CPage2::OnCheckTopmost()
{
UpdateData(true);
if(m_bTopMost)
{
AfxGetMainWnd()->SetWindowPos(&CWnd::wndTopMost,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE);
m_bTopMost=true;
}
else
{
AfxGetMainWnd()->SetWindowPos(&CWnd::wndNoTopMost ,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE);
m_bTopMost=false;
}
::WritePrivateProfileString("Settings","bTopMost",m_bTopMost ? "1":"0",".\\Settings.ini");
}
void CPage2::OnCheckAutorun()
{
UpdateData(true);
const TCHAR gcszAutoRunKey[]= _T( "Software\\microsoft\\windows\\currentversion\\run" );
const TCHAR gcszWindowClass[] = _T("我的程序");//设置开机程序自启动键值位置和其下新建子键值(可设为程序名)
HKEY hKey;
LONG lRet, lRet2;
DWORD dwLength, dwDataType;
TCHAR szItemValue[MAX_PATH], szPrevValue[MAX_PATH];
TCHAR szBuffer[MAX_PATH];
GetModuleFileName( NULL, szItemValue, MAX_PATH ); // 得到程序全路径名
//MessageBox(szItemValue);
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, gcszAutoRunKey,0, KEY_READ | KEY_WRITE, &hKey );
if( lRet != ERROR_SUCCESS ) // 打开注册表键
MessageBox("设置自动启动失败,请检查注册表是否被禁用!","使闹钟",MB_OK| MB_ICONEXCLAMATION);
dwLength = sizeof( szBuffer );
lRet = RegQueryValueEx( hKey, gcszWindowClass,NULL, &dwDataType, (LPBYTE)szBuffer, &dwLength );
// 查询自动运行项目是否存在
if( m_bAutoRun ) // 添加
{
if( lRet != ERROR_SUCCESS ) // 自动运行项目不存在
lRet2 = RegSetValueEx( hKey, gcszWindowClass,0, REG_SZ, (LPBYTE)szItemValue, strlen( szItemValue ) );
else // 存在, 比较二者是否相同
{
dwLength = sizeof( szPrevValue );
lRet2 = RegQueryValueEx( hKey, gcszWindowClass,0, &dwDataType,(LPBYTE)szPrevValue, &dwLength );
if( lstrcmp( szItemValue, szPrevValue ) ) // 不相同则替换
{
lRet2 = RegDeleteValue( hKey, gcszWindowClass );
lRet2 = RegSetValueEx( hKey, gcszWindowClass,0, REG_SZ,(LPBYTE)szItemValue, strlen( szItemValue ) );
}
}
}
else // 删除
if( lRet == ERROR_SUCCESS )
lRet2 = RegDeleteValue( hKey, gcszWindowClass ); // 自动运行项目已存在则删除
RegCloseKey( hKey ); // 关闭注册表键
if( lRet2 != ERROR_SUCCESS )
MessageBox("设置自动启动失败,请检查注册表是否被禁用!","使闹钟",MB_OK| MB_ICONEXCLAMATION);
}
void CPage2::OnCheckMinimize()
{
UpdateData(true);
::WritePrivateProfileString("Settings","bMinimized",m_check_minimize? "1":"0",".\\Settings.ini");
}
void CPage2::OnLButtonDown(UINT nFlags, CPoint point)
{
AfxGetMainWnd()->SendMessage(WM_NCLBUTTONDOWN,HTCAPTION,0);
CPropertyPage::OnLButtonDown(nFlags, point);
}
void CPage2::OnSave()
{
UpdateData(TRUE);
WORD virtualKey, systemKey;
m_hotkey.GetHotKey(virtualKey, systemKey);
CString strVirtualKey,strSystemKey;
strVirtualKey.Format("%d",virtualKey);
strSystemKey.Format("%d",systemKey);
::WritePrivateProfileString("HotKey","HotkeyVK",strVirtualKey,".\\Settings.ini");
::WritePrivateProfileString("HotKey","HotkeySystem",strSystemKey,".\\Settings.ini");
UINT fsModifiers = 0;
if (systemKey & HOTKEYF_ALT)
{
fsModifiers |= MOD_ALT;
}
if (systemKey & HOTKEYF_CONTROL)
{
fsModifiers |= MOD_CONTROL;
}
if (systemKey & HOTKEYF_SHIFT)
{
fsModifiers |= MOD_SHIFT;
}
RegisterHotKey(AfxGetMainWnd()->m_hWnd, 400, fsModifiers, virtualKey);
}
BOOL CPage2::OnInitDialog()
{
CPropertyPage::OnInitDialog();
/*------------------------------------窗体透明---------------------------------*/
CString str;
if( this->OSVer()=="Win2000" ) //保证在Win2000 WinXp中才调用
{
m_sld.SetRange(10,255);
m_sld.SetPos(::GetPrivateProfileInt("Settings","nTransparent",240,".\\Settings.ini"));
SetWindowLong(AfxGetMainWnd()->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(AfxGetMainWnd()->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun =(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
fun(AfxGetMainWnd()->GetSafeHwnd(),0,m_sld.GetPos(),2);
FreeLibrary(hInst);
}
str.Format("窗体的透明度:%%%d",100 * m_sld.GetPos() / 255);
SetDlgItemText(IDC_STATE,str);
}
else
{
str="该系统不支持窗体透明";
SetDlgItemText(IDC_STATE,str);
GetDlgItem(IDC_SLIDER1)->EnableWindow(false);
//GetDlgItem(IDC_SLIDER1)->ShowWindow(false);
//GetDlgItem(IDC_SLD)->ShowWindow(false);
}
/**********************************对话框透明**************************************//*------------------------------------窗体透明---------------------------------*/
/*注*/ WORD virtualKey=::GetPrivateProfileInt("HotKey","HotkeyVK",0,".\\Settings.ini");
/*册*/ WORD systemKey=::GetPrivateProfileInt("HotKey","HotkeySystem",0,".\\Settings.ini");
/*系*/ m_hotkey.SetHotKey(virtualKey, systemKey);
/*统*/
/*热*/ UINT fsModifiers = NULL;
/*键*/ if (systemKey & HOTKEYF_ALT)// convert systemkeys to modifiers
/**/ {
/**/ fsModifiers |= MOD_ALT;
/**/ }
/**/ if (systemKey & HOTKEYF_CONTROL)
/**/ {
/**/ fsModifiers |= MOD_CONTROL;
/**/ }
/*注*/ if (systemKey & HOTKEYF_SHIFT)
/*册*/ {
/*系*/ fsModifiers |= MOD_SHIFT;
/*统*/ }
/*热*/ RegisterHotKey(AfxGetMainWnd()->m_hWnd,400, fsModifiers, virtualKey);
/*键*/ UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CPage2::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CPropertyPage::PreTranslateMessage(pMsg);
}
void CPage2::OnReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult)
{
BYTE m=(BYTE)m_sld.GetPos();
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes"); //取得SetLayeredWindowAttributes函数指针
if(fun)
fun(AfxGetMainWnd()->GetSafeHwnd(),0,m,2);
FreeLibrary(hInst);
};
CString str;
str.Format("窗体的透明度:%%%d",100 * m / 255);
SetDlgItemText(IDC_STATE,str);
*pResult = 0;
CString str2;
str2.Format("%d",m);
::WritePrivateProfileString("Settings","nTransparent",str2,".\\Settings.ini");
*pResult = 0;
}
void CPage2::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
BYTE m=(BYTE)m_sld.GetPos();
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes"); //取得SetLayeredWindowAttributes函数指针
if(fun)
fun(AfxGetMainWnd()->GetSafeHwnd(),0,m,2);
FreeLibrary(hInst);
};
CString str;
str.Format("窗体的透明度:%%%d",100 * m / 255);
SetDlgItemText(IDC_STATE,str);
/* CString str2;
str2.Format("%d",m);
::WritePrivateProfileString("闹钟设置","nTransparent",str2,".\\Settings.ini");
*/
CPropertyPage::OnHScroll(nSBCode, nPos, pScrollBar);
}
CString CPage2::OSVer()
{
OSVERSIONINFOEX os;
::ZeroMemory(&os,sizeof(os));
os.dwOSVersionInfoSize=sizeof(os);
::GetVersionEx(reinterpret_cast(&os));
if(os.dwPlatformId==VER_PLATFORM_WIN32_NT && os.dwMajorVersion >=5)
return "Win2000";
if(os.dwPlatformId ==VER_PLATFORM_WIN32_WINDOWS)
return "Win9x";
}