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

📄 desktop_scdlg.cpp

📁 最新visualC++编程200例书籍源码包括对数据库的操作
💻 CPP
字号:
// desktop_scDlg.cpp : implementation file
//

#include "stdafx.h"
#include "desktop_sc.h"
#include "desktop_scDlg.h"
#include <atlbase.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDesktop_scDlg dialog

CDesktop_scDlg::CDesktop_scDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDesktop_scDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDesktop_scDlg)
	m_szGUID = _T("");
	m_szCmd = _T("");
	m_szIconPath = _T("");
	m_szDName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_szGUID ="{9F0DBE59-C479-49c5-951B-2545242FBDF6}"; //设置GUID
	m_szCmd = "cmd /K dir c:\\"; //设置命令
	m_szIconPath = "F:\\res\\MFC.ico"; //图标文件
	m_szDName = "我的图标"; //设置显示的名称

}

void CDesktop_scDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDesktop_scDlg)
	DDX_Text(pDX, IDC_GUID, m_szGUID);
	DDX_Text(pDX, IDC_CMD, m_szCmd);
	DDX_Text(pDX, IDC_ICONP, m_szIconPath);
	DDX_Text(pDX, IDC_DNAME, m_szDName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDesktop_scDlg, CDialog)
	//{{AFX_MSG_MAP(CDesktop_scDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CREATE, OnCreate)
	ON_BN_CLICKED(IDC_DEL, OnDel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDesktop_scDlg message handlers

BOOL CDesktop_scDlg::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
	
	// TODO: Add extra initialization here
	
	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 CDesktop_scDlg::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 CDesktop_scDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDesktop_scDlg::OnCreate() 
{
	UpdateData(TRUE);
	//
	char szTemp[256];

	CRegKey m_kdsktp;
	//创建GUID入口
	sprintf(szTemp,"CLSID\\%s",m_szGUID);
	m_kdsktp.Create(HKEY_CLASSES_ROOT,szTemp);
	m_kdsktp.SetValue(m_szDName);
	m_kdsktp.Close();

	//设置图标
	sprintf(szTemp,"CLSID\\%s\\DefaultIcon",m_szGUID);
	m_kdsktp.Create(HKEY_CLASSES_ROOT,szTemp);
	m_kdsktp.SetValue(m_szIconPath);
	m_kdsktp.Close();

	//设置双击图标时的打开命令
	sprintf(szTemp,"CLSID\\%s\\Shell\\Open\\Command",m_szGUID);
	m_kdsktp.Create(HKEY_CLASSES_ROOT,szTemp );
	m_kdsktp.SetValue(m_szCmd);
	m_kdsktp.Close();

	//设置菜单属性
	sprintf(szTemp,"CLSID\\%s\\ShellFolder",m_szGUID);
	BYTE bValue[4];
	m_kdsktp.Create(HKEY_CLASSES_ROOT,szTemp);
	bValue[0]=0x0; //"00.00.00.00"
	bValue[1]=0x0;
	bValue[2]=0x0;
	bValue[3]=0x0;
	RegSetValueEx(m_kdsktp.m_hKey,"Attributes",0,REG_BINARY,bValue,4);
	m_kdsktp.Close();

	//在桌面放置图标
	sprintf(szTemp,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Desktop\\NameSpace\\%s",m_szGUID);
	m_kdsktp.Create(HKEY_LOCAL_MACHINE,szTemp);
	m_kdsktp.SetValue(m_szDName);
	m_kdsktp.Close();

	//通知桌面更新
	SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_FLUSHNOWAIT,0, 0);
}

void CDesktop_scDlg::OnDel() 
{
	UpdateData(TRUE);
	//
	char szTemp[256];

	CRegKey m_kdsktp;
	//删除GUID入口
	sprintf(szTemp,"CLSID");
	m_kdsktp.Open(HKEY_CLASSES_ROOT,szTemp);
	m_kdsktp.DeleteSubKey(m_szGUID);
	m_kdsktp.Close();

	//删除桌面图标
	sprintf(szTemp,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Desktop\\NameSpace");
	m_kdsktp.Open(HKEY_LOCAL_MACHINE,szTemp);
	m_kdsktp.DeleteSubKey(m_szGUID);
	m_kdsktp.Close();

	//通知桌面更新
	SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_FLUSHNOWAIT,0, 0);
}

⌨️ 快捷键说明

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