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

📄 assocdlg.c

📁 Virtual Floppy Driver
💻 C
📖 第 1 页 / 共 2 页
字号:

		item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;

		item.iItem = ListView_GetItemCount(hList);
		item.pszText = ext;

		item.iItem = ListView_InsertItem(hList, &item);

		if (item.iItem == -1) {
			DEBUG_TRACE1(
				"AddItem : ListView_InsertItem - %s", ErrMsg(GetLastError()));
		}
	}
	else {

		// update icon index

		item.mask = LVIF_IMAGE | LVIF_PARAM;

		if (!ListView_SetItem(hList, &item)) {
			DEBUG_TRACE1(
				"AddItem : ListView_SetItem - %s", ErrMsg(GetLastError()));
		}
	}

	if (item.iItem >= 0) {

		// set program name

		ListView_SetItemText(hList, item.iItem, 1, prog);

		// count items with 'checked' icon

		if (item.iImage == 1) {
			nCheckedItems++;
		}
	}

	return item.iItem;
}

//
//	handles list control notification messages
//	NM_CLICK	- an item is clicked
//	LVN_KEYDOWN	- a key is pressed
//
void OnAssocListNotify(HWND hDlg, LPNMHDR pNMHDR)
{
	// if registry is read-only, do nothing

	if (!bRegWritable) {
		return;
	}

	switch (pNMHDR->code) {
	case NM_CLICK:
		{
			LVHITTESTINFO hit;

			memset(&hit, 0, sizeof(hit));

			if (!GetCursorPos(&hit.pt)) {
				DEBUG_TRACE1(
					"OnAssocListNotify : GetCursorPos - %s", ErrMsg(GetLastError()));
				break;
			}

			if (!ScreenToClient(pNMHDR->hwndFrom, &hit.pt)) {
				DEBUG_TRACE1(
					"OnAssocListNotify : ScreenToClient - %s", ErrMsg(GetLastError()));
				break;
			}

			ListView_HitTest(pNMHDR->hwndFrom, &hit);

			if (hit.iItem >= 0 && (hit.flags & LVHT_ONITEMICON)) {

				// icon of an item is clicked

				ToggleItem(pNMHDR->hwndFrom, hit.iItem);

				//	enable/disable 'clear' and 'apply' buttons

				EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_CLEAR), nCheckedItems);
				EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_ALL), (nCheckedItems < ListView_GetItemCount(pNMHDR->hwndFrom)));
				EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_APPLY), nChangedItems);
			}
		}
		break;

	case LVN_KEYDOWN:
		if (((LPNMLVKEYDOWN)pNMHDR)->wVKey == VK_SPACE) {
			int item;

			item = ListView_GetNextItem(pNMHDR->hwndFrom, -1, LVNI_SELECTED);

			if (item >= 0) {

				// space key is pressed while the focus on an item

				ToggleItem(pNMHDR->hwndFrom, item);

				//	enable/disable 'clear' and 'apply' buttons

				EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_CLEAR), nCheckedItems);
				EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_ALL), (nCheckedItems < ListView_GetItemCount(pNMHDR->hwndFrom)));
				EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_APPLY), nChangedItems);
			}
		}
		break;
	}

	return;
}

//
//	switch an item's checked state
//
void ToggleItem(HWND hList, int iItem)
{
	LVITEM item;

	memset(&item, 0, sizeof(item));

	item.mask = LVIF_IMAGE | LVIF_PARAM;
	item.iItem = iItem;
	
	if (!ListView_GetItem(hList, &item)) {
		DEBUG_TRACE1(
			"ToggleItem : ListView_GetItem - %s", ErrMsg(GetLastError()));

		return;
	}

	if (item.iImage == 0 || item.iImage == 1) {

		//	deal only with 'checked' or 'unchecked'
		//	ignore grayed icons

		//	is current state the same as the original state?

		if (item.iImage == item.lParam) {
			nChangedItems++;
		}
		else {
			nChangedItems--;
		}

		//	change icon index

		if (item.iImage == 0) {
			nCheckedItems++;
			item.iImage = 1;
		}
		else if (item.iImage == 1) {
			nCheckedItems--;
			item.iImage = 0;
		}

		if (!ListView_SetItem(hList, &item)) {
			DEBUG_TRACE1(
				"ToggleItem : ListView_SetItem - %s", ErrMsg(GetLastError()));
		}
	}

	return;
}

//
//	New button is clicked
//
void OnAssocNew(HWND hDlg, HWND hBtn)
{
	HWND hList;
	char ext[MAX_PATH], prog[MAX_PATH];
	LVITEM item;
	DWORD ret;

	if (!bRegWritable) {
		return;
	}

	if (DialogBoxParam(
		hAppInstance,
		MAKEINTRESOURCE(IDD_NEWEXT),
		hDlg,
		AddExtProc,
		(LPARAM)(ext + 1)) <= 0)
	{
		DEBUG_TRACE1(
			"OnAssocNew : DialogBoxParam - %s", ErrMsg(GetLastError()));
		return;
	}

	hList = GetDlgItem(hDlg, IDC_ASSOC_LIST);

	if (hList == NULL) {
		DEBUG_TRACE1(
			"OnAssocNew : GetDlgItem - %s", ErrMsg(GetLastError()));
		return;
	}

	ext[0] = '.';
	
	memset(&item, 0, sizeof(item));

	ret = GetAssociatedProgram(ext[1] == '.' ? &ext[1] : ext, prog);

	if (ret != ERROR_SUCCESS) {
		DEBUG_TRACE2(
			"OnAssocListNotify : GetAssociatedProgram(\"%s\") - %s",
			ext[1] == '.' ? &ext[1] : ext, ret);
	}

	//	Insert an item to the list control

	item.iItem = AddItem(hList, ext[1] == '.' ? &ext[1] : ext, prog);

	if (item.iItem == -1) {
		return;
	}

	//	Get the new image's check state

	item.mask = LVIF_IMAGE;

	if (!ListView_GetItem(hList, &item)) {
		DEBUG_TRACE1(
			"OnAssocListNotify : ListView_GetItem - %s", ErrMsg(GetLastError()));
		return;
	}

	//	if the item is not checked, check it

	if (item.iImage == 0) {
		item.iImage = 1;
		
		if (!ListView_SetItem(hList, &item)) {
			DEBUG_TRACE1(
				"OnAssocListNotify : ListView_SetItem - %s", ErrMsg(GetLastError()));
			return;
		}

		nCheckedItems++;
		nChangedItems++;
	}

	//	enable / disable 'clear' and 'apply' button

	EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_CLEAR), nCheckedItems);
	EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_ALL), nCheckedItems < ListView_GetItemCount(hList));
	EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_APPLY), nChangedItems);

	SendMessage(hBtn, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);

	return;
}

//
//	check all / clear all button is clicked
//
void OnAssocCheckAll(HWND hDlg, HWND hBtn, int check)
{
	HWND hList;
	LVITEM item;
	
	if (!bRegWritable) {
		return;
	}

	hList = GetDlgItem(hDlg, IDC_ASSOC_LIST);

	if (hList == NULL) {
		DEBUG_TRACE1(
			"OnAssocClear : GetDlgItem - %s", ErrMsg(GetLastError()));

		return;
	}

	nChangedItems = 0;

	memset(&item, 0, sizeof(item));
	item.iItem = -1;

	while ((item.iItem = ListView_GetNextItem(hList, item.iItem, LVNI_ALL)) != -1) {
		item.mask = LVIF_PARAM;
		
		if (!ListView_GetItem(hList, &item)) {
			DEBUG_TRACE1(
				"OnAssocClear : ListView_GetItem - %s", ErrMsg(GetLastError()));
			break;
		}

		if (item.lParam != check) {
			nChangedItems++;
		}

		item.mask = LVIF_IMAGE;
		item.iImage = check;
		
		if (!ListView_SetItem(hList, &item)) {
			DEBUG_TRACE1(
				"OnAssocClear : ListView_SetItem - %s", ErrMsg(GetLastError()));
			break;
		}
	}

	nCheckedItems = check ? ListView_GetItemCount(hList) : 0;

	EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_CLEAR), nCheckedItems);
	EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_ALL), (nCheckedItems < ListView_GetItemCount(hList)));
	EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_APPLY), nChangedItems);

	SetFocus(GetDlgItem(hDlg, IDC_ASSOC_LIST));

	SendMessage(hBtn, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);

	return;
}

//
//	apply button is clicked
//
void OnAssocApply(HWND hDlg, HWND hBtn)
{
	HWND hList;
	LVITEM item;
	char app_path[MAX_PATH], type_desc[MAX_PATH], verb_desc[MAX_PATH];
	char ext[MAX_PATH], prog[MAX_PATH];

	if (!bRegWritable) {
		return;
	}

	hList = GetDlgItem(hDlg, IDC_ASSOC_LIST);

	if (hList == NULL) {
		DEBUG_TRACE1(
			"OnAssocApply : GetDlgItem - %s", ErrMsg(GetLastError()));

		return;
	}

	if (!GetModuleFileName(hAppInstance, app_path, sizeof(app_path))) {
		DEBUG_TRACE1(
			"OnAssocApply : GetModuleFileName - %s", ErrMsg(GetLastError()));

		return;
	}

	if (!LoadString(hAppInstance, IDS_ASSOC_FILETYPE_DESC, type_desc, sizeof(type_desc))) {
		DEBUG_TRACE1(
			"OnAssocApply : LoadString - %s", ErrMsg(GetLastError()));

		type_desc[0] = '\0';

		// this is not fatal
	}

	if (!LoadString(hAppInstance, IDS_ASSOC_FILETYPE_VERB, verb_desc, sizeof(verb_desc))) {
		DEBUG_TRACE1(
			"OnAssocApply : LoadString - %s", ErrMsg(GetLastError()));

		verb_desc[0] = '\0';

		// this also is not fatal
	}

	nCheckedItems = 0;
	nChangedItems = 0;

	memset(&item, 0, sizeof(item));
	item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
	item.iItem = -1;
	item.pszText = ext;
	item.cchTextMax = sizeof(ext);

	while ((item.iItem = ListView_GetNextItem(hList, item.iItem, LVNI_ALL)) != -1) {
		DWORD ret;

		if (!ListView_GetItem(hList, &item)) {
			DEBUG_TRACE1(
				"OnAssocApply : ListView_GetItem - %s", ErrMsg(GetLastError()));
			break;
		}

		if (item.iImage == 0 && item.lParam == 1) {
			// initially checked, but currently unchecked

			ret = RestoreAssociation(ext, REG_FILETYPE_PREFIX);

			if (ret != ERROR_SUCCESS) {
				DEBUG_TRACE2(
					"OnAssocApply : RemoveAssociation(\"%s\") - %s",
					ext, ret);
				break;
			}
		}
		else if (item.iImage == 1 && item.lParam == 0) {
			// initially unchecked, and currently checked

			ret = AddAssociation(ext,
				REG_FILETYPE_PREFIX,
				type_desc,
				REG_FILETYPE_PREFIX,
				verb_desc,
				app_path,
				VFD_ICON_IDX_IMAGE);

			if (ret != ERROR_SUCCESS) {
				DEBUG_TRACE2(
					"OnAssocApply : AddAssociation(\"%s\") - %s",
					ext, ret);
				break;
			}
		}

		if ((ret = GetAssociatedProgram(ext, prog)) == ERROR_SUCCESS) {
			AddItem(hList, ext, prog);
		}
		else {
			DEBUG_TRACE2(
				"OnAssocApply : GetAssociatedProgram(\"%s\") - %s",
				ext, ret);
		}
	}

	EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_CLEAR), nCheckedItems);
	EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_ALL), (nCheckedItems < ListView_GetItemCount(hList)));
	EnableWindow(GetDlgItem(hDlg, IDC_ASSOC_APPLY), nChangedItems);

	SetFocus(GetDlgItem(hDlg, IDC_ASSOC_LIST));

	SendMessage(hBtn, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);

	return;
}

//
//	Add extension dialog procedure
//
BOOL CALLBACK AddExtProc(
	HWND hDlg,
	UINT msg,
	WPARAM wParam,
	LPARAM lParam)
{
	static char *sExtension = NULL;

	switch (msg) {
	case WM_INITDIALOG:
		sExtension = (char *)lParam;
		break;

	case WM_COMMAND:
		switch (wParam) {
		case IDOK:
			EndDialog(hDlg, 
				GetDlgItemText(hDlg, IDC_EXTENSION, sExtension, MAX_PATH - 1));
			return TRUE;

		case IDCANCEL:
			EndDialog(hDlg, 0);
			return TRUE;

		case MAKELONG(IDC_EXTENSION, EN_CHANGE):
			OnAddExtEdit(hDlg, (HWND)lParam);
			break;
		}
	}
	return 0;
}

//
//	text in the extention edit box has been changed
//
void OnAddExtEdit(HWND hDlg, HWND hEdit)
{
	char buf[MAX_PATH], *p;
	BOOL valid = FALSE;

	if (!GetWindowText(hEdit, buf, sizeof(buf))) {
		if (GetLastError() != ERROR_SUCCESS) {
			DEBUG_TRACE1(
				"OnAssocEditChange : GetWindowText - %s", ErrMsg(GetLastError()));

			return;
		}
	}

	p = buf;

	if (*p == '.') {
		p++;
	}

	while (*p) {
		if (strchr(".\\/:,;*?\"<>| ", *p)) {
			valid = FALSE;
			break;
		}
		else {
			valid = TRUE;
		}
		p++;
	}

	//	enable 'OK' button if text is valid for an extension

	EnableWindow(GetDlgItem(hDlg, IDOK), valid);

	return;
}

// End of File

⌨️ 快捷键说明

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