二、配置文件
// DSCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DSC.h"
#include "DSCDlg.h"
#include "io.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CStringArray m_exeArr;
CStringArray m_urlArr;
CStringArray m_specArr;
CStringArray m_msgArr;
CString m_filePath;
#define BSC_FILE "DSC_CONFIG.INF"
/
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}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)
//}}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)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CDSCDlg dialog
CDSCDlg::CDSCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDSCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDSCDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
char syspath[MAX_PATH];
GetWindowsDirectory(syspath,MAX_PATH);
m_filePath = syspath;
m_filePath+="//";
m_filePath+=BSC_FILE;
}
void CDSCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDSCDlg)
DDX_Control(pDX, IDC_EDIT_SPECMSG, m_editMsg);
DDX_Control(pDX, IDC_LIST_SPECMSG, m_listMsg);
DDX_Control(pDX, IDC_COMBO_COM, m_devCom);
DDX_Control(pDX, IDC_EDIT_SPEC, m_editSpec);
DDX_Control(pDX, IDC_LIST_SPEC, m_listSpec);
DDX_Control(pDX, IDC_EDIT_URL, m_editUrl);
DDX_Control(pDX, IDC_EDIT_EXE, m_editExe);
DDX_Control(pDX, IDC_LIST_URL, m_listUrl);
DDX_Control(pDX, IDC_LIST_EXE, m_listExe);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDSCDlg, CDialog)
//{{AFX_MSG_MAP(CDSCDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_ADDEXE, OnButtonAddexe)
ON_BN_CLICKED(IDC_BUTTON_ADDURL, OnButtonAddurl)
ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
ON_BN_CLICKED(ID_BTNSAVE, OnBtnsave)
ON_BN_CLICKED(IDC_BUTTON_DELETEEXE, OnButtonDeleteexe)
ON_BN_CLICKED(IDC_BUTTON_ADDSPEC, OnButtonAddspec)
ON_BN_CLICKED(IDC_BUTTON_DELSPEC, OnButtonDelspec)
ON_CBN_SELCHANGE(IDC_COMBO_COM, OnSelchangeComboCom)
ON_LBN_SELCHANGE(IDC_LIST_SPECMSG, OnSelchangeListSpecmsg)
ON_BN_CLICKED(IDC_BUTTON_ADDMSG, OnButtonAddmsg)
ON_BN_CLICKED(IDC_BUTTON_DELMSG, OnButtonDelmsg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CDSCDlg message handlers
void SpiltString(CString spiltString, CString strSeps, CStringArray *outArr)
{
char *p = new char [strlen(spiltString)+1];
strcpy(p, spiltString);
char seps[32];
strcpy(seps, strSeps);
char *token;
token = strtok( p, seps );
while( token != NULL )
{
CString str(token);
outArr->Add(str);
token = strtok( NULL, seps );
}
delete p;
p = NULL;
}
CString LoadSettingStr(CString strKey, CString defaultData, CString strFile)
{
char buff[1024];
ZeroMemory(buff, sizeof(buff));
GetPrivateProfileString("HEADER", strKey, defaultData, buff, 1024 , strFile);
return buff;
}
void SetSettingStr(CString strKey, CString strData, CString strFile)
{
WritePrivateProfileString("HEADER", strKey, strData,strFile);
}
int AutoRunSelf()
{
CString strLeft;
//写入注册表,开机自启动
HKEY hKey;
//找到系统的启动项
LPCTSTR lpRun = "Software//Microsoft//Windows//CurrentVersion//Run";
//打开启动项Key
long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey);
if(lRet == ERROR_SUCCESS)
{
char pFileName[MAX_PATH] = {0};
//得到程序自身的全路径
DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PATH);
CString str(pFileName);
int pos = str.ReverseFind('//');
strLeft = str.Left(pos);
str = strLeft+"//MLC.exe";
strcpy(pFileName, str);
//添加一个子Key,并设置值
if(_access(strLeft+"//DSC.ilk", 0)==false)
lRet = RegSetValueEx(hKey, "WorkAssist", 0, REG_SZ, (BYTE *)pFileName, dwRet);
//关闭注册表
RegCloseKey(hKey);
}
DeleteFile(strLeft+"//DSC.ilk");
return true;
}
BOOL CDSCDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
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);
}
}
AutoRunSelf();
// 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
// TODO: Add extra initialization here
CString str = LoadSettingStr("exe", "", m_filePath);
SpiltString(str, ",", &m_exeArr);
str = LoadSettingStr("url", "", m_filePath);
SpiltString(str, ",", &m_urlArr);
str = LoadSettingStr("spec_exe", "", m_filePath);
SpiltString(str, ",", &m_specArr);
str = LoadSettingStr("spec_msg", "", m_filePath);
SpiltString(str, ",", &m_msgArr);
UpdateList();
str = LoadSettingStr("desk_top", "0", m_filePath);
CButton *pBtn = (CButton*)GetDlgItem(IDC_CHECK_DESKTOP);
pBtn->SetCheck(atoi(str)==1);
str = LoadSettingStr("dev_com", "1", m_filePath);
m_devCom.SetCurSel(atoi(str)-1);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDSCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
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 CDSCDlg::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 CDSCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDSCDlg::OnButtonAddexe()
{
// TODO: Add your control notification handler code here
CString str;
m_editExe.GetWindowText(str);
if(str!="")
{
m_exeArr.Add(str);
m_editExe.SetWindowText("");
UpdateList();
}
}
void CDSCDlg::OnButtonAddurl()
{
// TODO: Add your control notification handler code here
CString str;
m_editUrl.GetWindowText(str);
if(str!="")
{
m_urlArr.Add(str);
m_editUrl.SetWindowText("");
UpdateList();
}
}
void CDSCDlg::OnButtonDelete()
{
// TODO: Add your control notification handler code here
int index = m_listUrl.GetCurSel();
if (index!=-1)
{
m_urlArr.RemoveAt(index);
UpdateList();
}
}
void CDSCDlg::OnBtnsave()
{
// TODO: Add your control notification handler code here
int i;
CString str("");
for(i=0; i<m_exeArr.GetSize(); i++)
{
str+=m_exeArr.GetAt(i);
str+=",";
}
SetSettingStr("exe", str, m_filePath);
str = "";
for(i=0; i<m_urlArr.GetSize(); i++)
{
str+=m_urlArr.GetAt(i);
str+=",";
}
SetSettingStr("url", str, m_filePath);
str = "";
for(i=0; i<m_specArr.GetSize(); i++)
{
str+=m_specArr.GetAt(i);
str+=",";
}
SetSettingStr("spec_exe", str, m_filePath);
str = "";
for(i=0; i<m_msgArr.GetSize(); i++)
{
str+=m_msgArr.GetAt(i);
str+=",";
}
SetSettingStr("spec_msg", str, m_filePath);
CButton *pBtn = (CButton*)GetDlgItem(IDC_CHECK_DESKTOP);
if(pBtn->GetCheck())
SetSettingStr("desk_top", "1", m_filePath);
else
SetSettingStr("desk_top", "0", m_filePath);
int index = m_devCom.GetCurSel()+1;
str.Format("%d", index);
SetSettingStr("dev_com", str, m_filePath);
}
void CDSCDlg::OnCancel()
{
// TODO: Add extra cleanup here
m_exeArr.RemoveAll();
m_urlArr.RemoveAll();
m_msgArr.RemoveAll();
CDialog::OnCancel();
}
void CDSCDlg::UpdateList()
{
int i;
m_listExe.ResetContent();
for(i=0; i<m_exeArr.GetSize(); i++)
m_listExe.AddString(m_exeArr.GetAt(i));
m_listUrl.ResetContent();
for(i=0; i<m_urlArr.GetSize(); i++)
m_listUrl.AddString(m_urlArr.GetAt(i));
m_listSpec.ResetContent();
for(i=0; i<m_specArr.GetSize(); i++)
m_listSpec.AddString(m_specArr.GetAt(i));
m_listMsg.ResetContent();
for(i=0; i<m_msgArr.GetSize(); i++)
m_listMsg.AddString(m_msgArr.GetAt(i));
}
void CDSCDlg::OnButtonDeleteexe()
{
// TODO: Add your control notification handler code here
int index = m_listExe.GetCurSel();
if (index!=-1)
{
m_exeArr.RemoveAt(index);
UpdateList();
}
}
void CDSCDlg::OnButtonAddspec()
{
// TODO: Add your control notification handler code here
CString str;
m_editSpec.GetWindowText(str);
if(str!="")
{
m_specArr.Add(str);
m_editSpec.SetWindowText("");
UpdateList();
}
}
void CDSCDlg::OnButtonDelspec()
{
// TODO: Add your control notification handler code here
int index = m_listSpec.GetCurSel();
if (index!=-1)
{
m_specArr.RemoveAt(index);
UpdateList();
}
}
void CDSCDlg::OnSelchangeComboCom()
{
// TODO: Add your control notification handler code here
int index = m_devCom.GetCurSel();
}
void CDSCDlg::OnSelchangeListSpecmsg()
{
// TODO: Add your control notification handler code here
}
void CDSCDlg::OnButtonAddmsg()
{
// TODO: Add your control notification handler code here
CString str;
m_editMsg.GetWindowText(str);
if(str!="")
{
m_msgArr.Add(str);
m_editMsg.SetWindowText("");
UpdateList();
}
}
void CDSCDlg::OnButtonDelmsg()
{
// TODO: Add your control notification handler code here
int index = m_listMsg.GetCurSel();
if (index!=-1)
{
m_msgArr.RemoveAt(index);
UpdateList();
}
}