⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myshortcutdlg.cpp

📁 这是书上的代码
💻 CPP
字号:
// myshortcutDlg.cpp : implementation file
//

#include "stdafx.h"
#include "myshortcut.h"
#include "myshortcutDlg.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 };
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMyshortcutDlg dialog

CMyshortcutDlg::CMyshortcutDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyshortcutDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyshortcutDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    choosed_object=-1;
    choosed_place=0;
	chk1=chk2=chk3=chk4=0;

	//初始化变量browinfo
	memset(&browinfo, 0, sizeof(BROWSEINFO) );
	browinfo.hwndOwner = (m_pParentWnd==NULL)?NULL:m_pParentWnd->GetSafeHwnd();
	browinfo.pszDisplayName = 0;
	browinfo.pidlRoot = 0;
	browinfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
	browinfo.lpfn = NULL;
	browinfo.lpszTitle = "";
    //初始化结束


}

void CMyshortcutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyshortcutDlg)
	DDX_Control(pDX, IDC_CHECK4, m_check4);
	DDX_Control(pDX, IDC_CHECK3, m_check3);
	DDX_Control(pDX, IDC_CHECK2, m_check2);
	DDX_Control(pDX, IDC_CHECK1, m_check1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyshortcutDlg, CDialog)
	//{{AFX_MSG_MAP(CMyshortcutDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyshortcutDlg message handlers

BOOL CMyshortcutDlg::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);
		}
	}

	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyshortcutDlg::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 CMyshortcutDlg::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 CMyshortcutDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyshortcutDlg::OnRadio1() 
{
	// TODO: Add your control notification handler code here
choosed_object=2;	
}

void CMyshortcutDlg::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	choosed_object=4;

}

void CMyshortcutDlg::OnOK() 
{
	// TODO: Add extra validation here
	char crEnVar[MAX_PATH];
	::GetEnvironmentVariable ("USERPROFILE",crEnVar,MAX_PATH);   
     chk1=m_check1.GetCheck();
     chk2=m_check2.GetCheck();
	 chk3=m_check3.GetCheck();
     chk4=m_check4.GetCheck();
     choosed_place=chk1+chk2+chk3+chk4;

if(choosed_place)
{
	if(choosed_object==2)   //   为文件创建快捷方式
	{
	
	CFileDialog  dlg(TRUE,"","",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"ALL Files (*.*)|*.*|All files (*.*)|*.*||",this);
	dlg.m_ofn.lpstrTitle ="选择要创建FileLink的文件";
	if(dlg.DoModal ()==IDOK)
	{

		if(strlen(crEnVar)>0)
		{
			CString mFile=dlg.GetPathName ();
            CString destPath=CString(crEnVar);

		if(m_check1.GetCheck()==1)
		{
			destPath+="\\桌面\\";
			destPath+=dlg.GetFileName ();
			destPath+=".lnk";
			lasting(mFile,destPath);
		}

		if(m_check2.GetCheck()==1)
		{
			destPath+="\\「开始」菜单\\";
			destPath+=dlg.GetFileName ();
			destPath+=".lnk";
			lasting(mFile,destPath);
		
		}

		if(m_check3.GetCheck()==1)
		{
			destPath+="\\「开始」菜单\\程序\\";			
			destPath+=dlg.GetFileName ();
			destPath+=".lnk";
			lasting(mFile,destPath);
		
		}
			
		if(m_check4.GetCheck()==1)
		{
			destPath+="\\「开始」菜单\\程序\\附件\\";			
			destPath+=dlg.GetFileName ();
			destPath+=".lnk";
			lasting(mFile,destPath);
		
		}			
			
			
		}
		else
		{
			::AfxMessageBox ("不能获取环境变量",MB_OK);
		}
	}

	
	}
	else
	{
		if(choosed_object==4)  //为文件夹创建快捷方式
		{
		
     char  pathname[MAX_PATH];
     CString subname;
     CString pa(pathname);
	 
	 itemlist=SHBrowseForFolder( &browinfo);
     SHGetPathFromIDList(itemlist, pathname);	 
     
	 if(pa.GetLength()<=3)
	 {
		 subname+=pa.Left(1);
	     subname+="盘";
	 }

	 else
	 {
	  
	subname+=pa.Mid(pa.ReverseFind('\\')+1);
		
	 }
	 



       CString destPath=CString(crEnVar);

		if(m_check1.GetCheck()==1)
		{
			destPath+="\\桌面\\";
			destPath+=subname;
			destPath+=".lnk";
			lasting(pathname,destPath);
			
		}

		if(m_check2.GetCheck()==1)
		{
			destPath+="\\「开始」菜单\\";
			destPath+=subname;
			destPath+=".lnk";
			lasting(pathname,destPath);
			
		}


		if(m_check3.GetCheck()==1)
		{
			destPath+="\\「开始」菜单\\程序\\";
			destPath+=subname;
			destPath+=".lnk";
			lasting(pathname,destPath);
			
		}



		if(m_check4.GetCheck()==1)
		{
			destPath+="\\「开始」菜单\\程序\\附件\\";
			destPath+=subname;
			destPath+=".lnk";
			lasting(pathname,destPath);
			
		}


     
		
		}
		
		else     //没有选择对象,程序结束
		{
			MessageBox("没有选择创建快捷方式的对象","",MB_ICONWARNING);

		
		}
	}
}
else
{

      ::AfxMessageBox ("没有选择建立快捷方式的位置",MB_OK);
}

	CDialog::OnOK();
}

BOOL CMyshortcutDlg::lasting(const CString strPathObj,const CString strPathLink)
{

BOOL bret=FALSE;
IShellLink *ps1;
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink,NULL,
   CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&ps1)))
{
	IPersistFile *ppf;
	ps1->SetPath(strPathObj);
	if(SUCCEEDED(ps1->QueryInterface(IID_IPersistFile,(LPVOID *)&ppf)))
	{
		WORD wsz[MAX_PATH];
		MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,strPathLink,-1,wsz,MAX_PATH);
		if(SUCCEEDED(ppf->Save (wsz,TRUE)))
			bret=TRUE;
		ppf->Release();
	}
ppf->Release();	
}
	return bret;

}








⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -