📄 cddlg.cpp
字号:
// cdDlg.cpp : implementation file
//
#include "stdafx.h"
#include "cd.h"
#include "cdDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
CButton m_ok;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
virtual BOOL OnInitDialog();
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
DDX_Control(pDX, IDOK, m_ok);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCdDlg dialog
CCdDlg::CCdDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCdDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCdDlg)
m_bAutoRun=(::GetPrivateProfileInt("程序设置","自动运行",0,".\\Settings.ini")==0 ?false:true);
m_bMinimized=(::GetPrivateProfileInt("程序设置","启动时最小化",0,".\\Settings.ini")==0 ?false:true);
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_btanchu=false;
}
void CCdDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCdDlg)
DDX_Control(pDX, IDC_HOTKEY1, m_hotkey);
DDX_Control(pDX, IDC_TANCHU, m_tanchu);
DDX_Control(pDX, IDC_GUANBI, m_guanbi);
DDX_Check(pDX, IDC_CHECK_AUTORUN, m_bAutoRun);
DDX_Check(pDX, IDC_CHECK_MINIMIZED, m_bMinimized);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCdDlg, CDialog)
ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(CCdDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
ON_MESSAGE(MYWM_NOTIFYICON,OnMyIconNotify)
ON_MESSAGE(WM_HOTKEY,OnHotKey)
ON_BN_CLICKED(IDC_TANCHU, OnTanchu)
ON_BN_CLICKED(IDC_GUANBI, OnGuanbi)
ON_BN_CLICKED(IDC_TUICHU, OnTuichu)
ON_BN_CLICKED(IDC_GUANYU, OnGuanyu)
ON_WM_MOUSEMOVE()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_CHECK_AUTORUN, OnCheckAutorun)
ON_BN_CLICKED(IDC_CHECK_MINIMIZED, OnCheckMinimized)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCdDlg message handlers
BOOL CCdDlg::OnInitDialog()
{
CDialog::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
m_menu.LoadMenu(IDR_MENU1);
TaskBarAddIcon(GetSafeHwnd(),100,AfxGetApp()->LoadIcon(IDR_MAINFRAME),_T("光驱控制-三峡大学"));
m_tooltip.Create(this);
m_tooltip.SetDelayTime(100);
m_tooltip.AddTool(GetDlgItem(IDC_TANCHU),"单击此处弹出光驱");
m_tooltip.AddTool(GetDlgItem(IDC_GUANBI),"单击此处关闭光驱");
m_tooltip.AddTool(GetDlgItem(IDC_GUANYU),"关于 光驱控制");
m_tooltip.AddTool(GetDlgItem(IDC_TUICHU),"单击此处退出程序");
/*注*/ WORD virtualKey=::GetPrivateProfileInt("热键设置","HotkeyVK",0,".\\Settings.ini");
/*册*/ WORD systemKey=::GetPrivateProfileInt("热键设置","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(m_hWnd,200, fsModifiers, virtualKey);
/*键*/ UpdateData(FALSE);
if(this->m_bMinimized)
PostMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCdDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
if(nID==SC_MINIMIZE)
{
ShowWindow(SW_HIDE);
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CCdDlg::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 CCdDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCdDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
cur=AfxGetApp()->LoadCursor(IDC_CURSOR1);
SetCursor(cur);
ReleaseCapture();
SendMessage(WM_NCLBUTTONDOWN,HTCAPTION,0);
CDialog::OnLButtonDown(nFlags, point);
}
void CCdDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
new CFaderWnd(this);
CAboutDlg dlg;
dlg.DoModal();
CDialog::OnLButtonDblClk(nFlags, point);
}
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
/*注*/ WORD virtualKey=::GetPrivateProfileInt("热键设置","HotkeyVK",0,".\\Settings.ini");
/*册*/ WORD systemKey=::GetPrivateProfileInt("热键设置","HotkeySystem",0,".\\Settings.ini");
/*系*/
/*统*/
/*热*/ 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(m_hWnd,200, fsModifiers, virtualKey);
/*键*/ UpdateData(false);
return TRUE;
}
void CCdDlg::OnContextMenu(CWnd*, CPoint point)
{
{ if (point.x == -1 && point.y == -1){ //keystroke invocation CRect rect; GetClientRect(rect); ClientToScreen(rect); point = rect.TopLeft(); point.Offset(5, 5); } CMenu menu; VERIFY(menu.LoadMenu(CG_IDR_POPUP_CD_DLG)); CMenu* pPopup = menu.GetSubMenu(0); ASSERT(pPopup != NULL); CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD) pWndPopupOwner = pWndPopupOwner->GetParent(); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner); }
}
void CCdDlg::OnTanchu()
{
mciSendString("set cdaudio door open",0,0,NULL);
m_btanchu=true;
m_tanchu.EnableWindow(false);
m_guanbi.EnableWindow(true);
}
void CCdDlg::OnGuanbi()
{
mciSendString("set cdaudio door closed",0,0,NULL);
// MessageBox("光驱已经关闭了");
m_btanchu=false;
m_guanbi.EnableWindow(false);
m_tanchu.EnableWindow(true);
}
void CCdDlg::OnTuichu()
{
CFaderWnd* pf=new CFaderWnd(this);
pf->m_bMainWnd=true;
//CDialog::OnOK();
this->ShowWindow(SW_HIDE);
}
void CCdDlg::OnGuanyu()
{
new CFaderWnd(this);
CAboutDlg dlg;
dlg.DoModal();
}
BOOL CCdDlg::TaskBarAddIcon(HWND hwnd, UINT uID, HICON hicon, LPSTR lpszTip)
{
NOTIFYICONDATA d;
d.cbSize=sizeof(NOTIFYICONDATA);
d.hWnd=hwnd;
d.uID=uID;
d.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
d.uCallbackMessage=MYWM_NOTIFYICON;
d.hIcon=hicon;
if(lpszTip)
lstrcpy(d.szTip,lpszTip);
else
d.szTip[0]='\0';
return Shell_NotifyIcon(NIM_ADD,&d);
}
BOOL CCdDlg::TaskBarDeleteIcon(HWND hwnd, UINT uID)
{
NOTIFYICONDATA d;
d.cbSize=sizeof(NOTIFYICONDATA);
d.hWnd=hwnd;
d.uID=uID;
return Shell_NotifyIcon(NIM_DELETE,&d);
}
void CCdDlg::OnMyIconNotify(WPARAM wParam,LPARAM lParam)
{
UINT uMouseMsg=LOWORD(lParam);
switch(uMouseMsg)
{
case WM_LBUTTONDOWN:
{
if(this->IsWindowVisible())
ShowWindow(SW_HIDE);
else
ShowWindow(SW_SHOWNORMAL);
}break;
case WM_RBUTTONDOWN:
{
CPoint pt;
GetCursorPos(&pt);
AfxGetMainWnd()->SetForegroundWindow();
CMenu* psub=(CMenu*)m_menu.GetSubMenu(0);
psub->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,pt.x,pt.y,this);
AfxGetMainWnd()->PostMessage(WM_NULL,0,0);
}
break;
}
}
BOOL CCdDlg::DestroyWindow()
{
TaskBarDeleteIcon(GetSafeHwnd(),100);
return CDialog::DestroyWindow();
}
LRESULT CCdDlg::OnHotKey(WPARAM wp,LPARAM lp)
{
if(wp==200)
{
if(m_btanchu)
{
mciSendString("set cdaudio door closed",0,0,NULL);
//MessageBox("光驱已经关闭了");
m_btanchu=false;
}
else
{
//MessageBox("光驱已经弹出");
mciSendString("set cdaudio door open",0,0,NULL);
m_btanchu=true;
}
}
return 0;
}
//DEL BOOL CCdDlg::m_btanchu()
//DEL {
//DEL
//DEL }
BOOL CCdDlg::PreTranslateMessage(MSG* pMsg)
{
m_tooltip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
void CCdDlg::OnMouseMove(UINT nFlags, CPoint point)
{
cur=AfxGetApp()->LoadCursor(IDC_CURSOR1);
SetCursor(cur);
CDialog::OnMouseMove(nFlags, point);
}
void CCdDlg::OnButton1()
{
UpdateData(TRUE);
WORD virtualKey, systemKey;
m_hotkey.GetHotKey(virtualKey, systemKey);
CString strVirtualKey,strSystemKey;
strVirtualKey.Format("%d",virtualKey);
strSystemKey.Format("%d",systemKey);
::WritePrivateProfileString("热键设置","HotkeyVK",strVirtualKey,".\\Settings.ini");
::WritePrivateProfileString("热键设置","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(m_hWnd, 200, fsModifiers, virtualKey);
}
void CCdDlg::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 ) // 添加
{
::WritePrivateProfileString("程序设置","自动运行",m_bAutoRun? "1":"0",".\\Settings.ini");
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 CCdDlg::OnCheckMinimized()
{
UpdateData(true);
::WritePrivateProfileString("程序设置","启动时最小化",m_bMinimized? "1":"0",".\\Settings.ini");
}
void CAboutDlg::OnOK()
{
new CFaderWnd(this);
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -