Implement 'Edit DWORD' dialog.

svn path=/trunk/; revision=8975
This commit is contained in:
Eric Kohl 2004-04-05 12:00:11 +00:00
parent 7838aa4dc8
commit c78a2cfb83
3 changed files with 272 additions and 41 deletions

View file

@ -143,6 +143,23 @@ BEGIN
END END
IDD_EDIT_DWORD DIALOG 32, 24, 252, 104
STYLE DS_SETFONT | DS_MODALFRAME | DS_NOIDLEMSG | DS_3DLOOK | DS_CONTEXTHELP |
WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Edit DWORD"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT "Value &name:",IDC_STATIC,6,6,134,8
EDITTEXT IDC_VALUE_NAME,6,17,240,12,ES_AUTOHSCROLL | ES_READONLY
LTEXT "&Value data:",IDC_STATIC,6,35,161,8
EDITTEXT IDC_VALUE_DATA,6,46,116,12,ES_AUTOHSCROLL
GROUPBOX "Base",IDC_STATIC,130,35,116,39
AUTORADIOBUTTON "&Hexadecimal",IDC_FORMAT_HEX,141,46,60,10,WS_GROUP
AUTORADIOBUTTON "&Decimal",IDC_FORMAT_DEC,141,59,60,10
DEFPUSHBUTTON "OK",IDOK,142,82,50,14
PUSHBUTTON "Cancel",IDCANCEL,196,82,50,14
END
/* /*
* String Table * String Table
*/ */

View file

@ -28,13 +28,25 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <shellapi.h> #include <shellapi.h>
#include <ctype.h>
#include "main.h" #include "main.h"
#include "regproc.h" #include "regproc.h"
#include "resource.h" #include "resource.h"
typedef enum _EDIT_MODE
{
EDIT_MODE_DEC,
EDIT_MODE_HEX
} EDIT_MODE;
static const TCHAR* editValueName; static const TCHAR* editValueName;
static TCHAR* stringValueData; static TCHAR* stringValueData;
static DWORD dwordValueData;
static EDIT_MODE dwordEditMode = EDIT_MODE_HEX;
void error(HWND hwnd, INT resId, ...) void error(HWND hwnd, INT resId, ...)
{ {
@ -79,13 +91,17 @@ INT_PTR CALLBACK modify_string_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, L
SetFocus(GetDlgItem(hwndDlg, IDC_VALUE_DATA)); SetFocus(GetDlgItem(hwndDlg, IDC_VALUE_DATA));
return FALSE; return FALSE;
case WM_COMMAND: case WM_COMMAND:
switch (LOWORD(wParam)) { switch (LOWORD(wParam))
{
case IDOK: case IDOK:
if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA))) { if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA)))
if ((len = GetWindowTextLength(hwndValue))) { {
if ((len = GetWindowTextLength(hwndValue)))
{
if (stringValueData) if (stringValueData)
{ {
if ((valueData = HeapReAlloc(GetProcessHeap(), 0, stringValueData, (len + 1) * sizeof(TCHAR)))) { if ((valueData = HeapReAlloc(GetProcessHeap(), 0, stringValueData, (len + 1) * sizeof(TCHAR))))
{
stringValueData = valueData; stringValueData = valueData;
if (!GetWindowText(hwndValue, stringValueData, len + 1)) if (!GetWindowText(hwndValue, stringValueData, len + 1))
*stringValueData = 0; *stringValueData = 0;
@ -93,7 +109,8 @@ INT_PTR CALLBACK modify_string_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, L
} }
else else
{ {
if ((valueData = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR)))) { if ((valueData = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR))))
{
stringValueData = valueData; stringValueData = valueData;
if (!GetWindowText(hwndValue, stringValueData, len + 1)) if (!GetWindowText(hwndValue, stringValueData, len + 1))
*stringValueData = 0; *stringValueData = 0;
@ -116,6 +133,173 @@ INT_PTR CALLBACK modify_string_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, L
return FALSE; return FALSE;
} }
LRESULT CALLBACK DwordEditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WNDPROC oldwndproc;
oldwndproc = (WNDPROC)GetWindowLong(hwnd, GWL_USERDATA);
switch (uMsg)
{
case WM_CHAR:
if (dwordEditMode == EDIT_MODE_DEC)
{
if (isdigit(wParam & 0xff))
{
break;
}
else
{
return 0;
}
}
else if (dwordEditMode == EDIT_MODE_HEX)
{
if (isxdigit(wParam & 0xff))
{
break;
}
else
{
return 0;
}
}
else
{
break;
}
}
return CallWindowProc(oldwndproc, hwnd, uMsg, wParam, lParam);
}
INT_PTR CALLBACK modify_dword_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WNDPROC oldproc;
TCHAR* valueData;
HWND hwndValue;
int len;
TCHAR ValueString[32];
LPTSTR Remainder;
DWORD Base;
DWORD Value;
switch(uMsg) {
case WM_INITDIALOG:
dwordEditMode = EDIT_MODE_HEX;
/* subclass the edit control */
hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA);
oldproc = (WNDPROC)GetWindowLong(hwndValue, GWL_WNDPROC);
SetWindowLong(hwndValue, GWL_USERDATA, (LONG)oldproc);
SetWindowLong(hwndValue, GWL_WNDPROC, (LONG)DwordEditSubclassProc);
if(editValueName && strcmp(editValueName, _T("")))
{
SetDlgItemText(hwndDlg, IDC_VALUE_NAME, editValueName);
}
else
{
SetDlgItemText(hwndDlg, IDC_VALUE_NAME, _T("(Default)"));
}
CheckRadioButton (hwndDlg, IDC_FORMAT_HEX, IDC_FORMAT_DEC, IDC_FORMAT_HEX);
sprintf (ValueString, "%lx", dwordValueData);
SetDlgItemText(hwndDlg, IDC_VALUE_DATA, ValueString);
SetFocus(GetDlgItem(hwndDlg, IDC_VALUE_DATA));
return FALSE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_FORMAT_HEX:
if (HIWORD(wParam) == BN_CLICKED && dwordEditMode == EDIT_MODE_DEC)
{
dwordEditMode = EDIT_MODE_HEX;
if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA)))
{
if ((len = GetWindowTextLength(hwndValue)))
{
if (!GetWindowText(hwndValue, ValueString, 32))
{
Value = 0;
}
else
{
Value = strtoul (ValueString, &Remainder, 10);
}
}
else
{
Value = 0;
}
}
sprintf (ValueString, "%lx", Value);
SetDlgItemText(hwndDlg, IDC_VALUE_DATA, ValueString);
return TRUE;
}
break;
case IDC_FORMAT_DEC:
if (HIWORD(wParam) == BN_CLICKED && dwordEditMode == EDIT_MODE_HEX)
{
dwordEditMode = EDIT_MODE_DEC;
if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA)))
{
if ((len = GetWindowTextLength(hwndValue)))
{
if (!GetWindowText(hwndValue, ValueString, 32))
{
Value = 0;
}
else
{
Value = strtoul (ValueString, &Remainder, 16);
}
}
else
{
Value = 0;
}
}
sprintf (ValueString, "%lu", Value);
SetDlgItemText(hwndDlg, IDC_VALUE_DATA, ValueString);
return TRUE;
}
break;
case IDOK:
if ((hwndValue = GetDlgItem(hwndDlg, IDC_VALUE_DATA)))
{
if ((len = GetWindowTextLength(hwndValue)))
{
if (!GetWindowText(hwndValue, ValueString, 32))
{
EndDialog(hwndDlg, IDCANCEL);
return TRUE;
}
Base = (dwordEditMode == EDIT_MODE_HEX) ? 16 : 10;
dwordValueData = strtoul (ValueString, &Remainder, Base);
}
else
{
EndDialog(hwndDlg, IDCANCEL);
return TRUE;
}
}
EndDialog(hwndDlg, IDOK);
return TRUE;
case IDCANCEL:
EndDialog(hwndDlg, IDCANCEL);
return TRUE;
}
}
return FALSE;
}
BOOL ModifyValue(HWND hwnd, HKEY hKey, LPCTSTR valueName) BOOL ModifyValue(HWND hwnd, HKEY hKey, LPCTSTR valueName)
{ {
DWORD valueDataLen; DWORD valueDataLen;
@ -123,12 +307,13 @@ BOOL ModifyValue(HWND hwnd, HKEY hKey, LPCTSTR valueName)
LONG lRet; LONG lRet;
BOOL result = FALSE; BOOL result = FALSE;
if (!hKey) return FALSE; if (!hKey)
return FALSE;
editValueName = valueName; editValueName = valueName;
lRet = RegQueryValueEx(hKey, valueName, 0, &type, 0, &valueDataLen); lRet = RegQueryValueEx(hKey, valueName, 0, &type, 0, &valueDataLen);
if(lRet != ERROR_SUCCESS && (!strcmp(valueName, _T("")) || valueName == NULL)) if (lRet != ERROR_SUCCESS && (!_tcscmp(valueName, _T("")) || valueName == NULL))
{ {
lRet = ERROR_SUCCESS; /* Allow editing of (Default) values which don't exist */ lRet = ERROR_SUCCESS; /* Allow editing of (Default) values which don't exist */
type = REG_SZ; type = REG_SZ;
@ -136,20 +321,24 @@ BOOL ModifyValue(HWND hwnd, HKEY hKey, LPCTSTR valueName)
stringValueData = NULL; stringValueData = NULL;
} }
if (lRet != ERROR_SUCCESS) { if (lRet != ERROR_SUCCESS)
{
error(hwnd, IDS_BAD_VALUE, valueName); error(hwnd, IDS_BAD_VALUE, valueName);
goto done; goto done;
} }
if ( (type == REG_SZ) || (type == REG_EXPAND_SZ) ) { if ((type == REG_SZ) || (type == REG_EXPAND_SZ))
{
if (valueDataLen > 0) if (valueDataLen > 0)
{ {
if (!(stringValueData = HeapAlloc(GetProcessHeap(), 0, valueDataLen))) { if (!(stringValueData = HeapAlloc(GetProcessHeap(), 0, valueDataLen)))
{
error(hwnd, IDS_TOO_BIG_VALUE, valueDataLen); error(hwnd, IDS_TOO_BIG_VALUE, valueDataLen);
goto done; goto done;
} }
lRet = RegQueryValueEx(hKey, valueName, 0, 0, stringValueData, &valueDataLen); lRet = RegQueryValueEx(hKey, valueName, 0, 0, stringValueData, &valueDataLen);
if (lRet != ERROR_SUCCESS) { if (lRet != ERROR_SUCCESS)
{
error(hwnd, IDS_BAD_VALUE, valueName); error(hwnd, IDS_BAD_VALUE, valueName);
goto done; goto done;
} }
@ -158,18 +347,39 @@ BOOL ModifyValue(HWND hwnd, HKEY hKey, LPCTSTR valueName)
{ {
stringValueData = NULL; stringValueData = NULL;
} }
if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_STRING), hwnd, modify_string_dlgproc) == IDOK) {
if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_STRING), hwnd, modify_string_dlgproc) == IDOK)
{
if (stringValueData) if (stringValueData)
{ {
lRet = RegSetValueEx(hKey, valueName, 0, type, stringValueData, lstrlen(stringValueData) + 1); lRet = RegSetValueEx(hKey, valueName, 0, type, stringValueData, lstrlen(stringValueData) + 1);
} }
else else
{
lRet = RegSetValueEx(hKey, valueName, 0, type, NULL, 0); lRet = RegSetValueEx(hKey, valueName, 0, type, NULL, 0);
if (lRet == ERROR_SUCCESS) result = TRUE;
} }
} else if ( type == REG_DWORD ) { if (lRet == ERROR_SUCCESS)
MessageBox(hwnd, "Can't edit dwords for now", "Error", MB_OK | MB_ICONERROR); result = TRUE;
} else { }
}
else if (type == REG_DWORD)
{
lRet = RegQueryValueEx(hKey, valueName, 0, 0, (LPBYTE)&dwordValueData, &valueDataLen);
if (lRet != ERROR_SUCCESS)
{
error(hwnd, IDS_BAD_VALUE, valueName);
goto done;
}
if (DialogBox(0, MAKEINTRESOURCE(IDD_EDIT_DWORD), hwnd, modify_dword_dlgproc) == IDOK)
{
lRet = RegSetValueEx(hKey, valueName, 0, type, (LPBYTE)&dwordValueData, sizeof(DWORD));
if (lRet == ERROR_SUCCESS)
result = TRUE;
}
}
else
{
error(hwnd, IDS_UNSUPPORTED_TYPE, type); error(hwnd, IDS_UNSUPPORTED_TYPE, type);
} }

View file

@ -114,4 +114,8 @@
#define IDC_VALUE_NAME 2001 #define IDC_VALUE_NAME 2001
#define IDC_VALUE_DATA 2002 #define IDC_VALUE_DATA 2002
#define IDD_EDIT_DWORD 2003
#define IDC_FORMAT_HEX 2004
#define IDC_FORMAT_DEC 2005
#define IDC_STATIC -1 #define IDC_STATIC -1