The instructions from the website http://www.activewin.com/tips/reg/desktop_3.shtml[^][^] are now working(make sure you restart/logoff or it will not work, as I found).
Here is the code for a working program that accomplishes the following:
1. Left Align icons on the screen(test this by moving desktop icon(s) to a
different part of the screen and then press the 'Arrange Icons' button in the compiled app)
2. Lock icons on the desktop so that they can only be rearranged.
The application accomplishes this be creating the NoSaveSettings key as mentioned in the above given website(this sets the Auto Arrange to on after the system has been shutdown/restart/user logged off).
3. Restart the computer. CAUTION before pressing the Add 'NoSaveSettingsKey' button save all of your work as it will restart the computer. You have been warned, use the code at your own risk.
Here is the code:
#include <windows.h>
#include <Reason.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void arrange();
void noSaveSettings();
void restart();
HWND hwnd;
HWND hLeftArrangeButton;
HWND hDoNotSaveSettings;
#define WINDOWCLASSNAME "WindowsApp" /* Class Name */
#define WINDOWTITLE "Auto Arrange"
#define WINDOWWIDTH 640
#define WINDOWHEIGHT 480
#define BUTTONONETEXT "Arrange Icons"
#define BUTTONTWOTEXT "Add 'NoSaveSettingsKey'"
#define ERRORTITLE "Error!"
#define REGKEYNAME "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"
#ifndef SHTDN_REASON_MAJOR_OPERATINGSYSTEM
#define SHTDN_REASON_MAJOR_OPERATINGSYSTEM 0x00020000 //from reason.h
#endif
#ifndef SHTDN_REASON_MINOR_RECONFIG
#define SHTDN_REASON_MINOR_RECONFIG 0x00000004 //from reason.h
#endif
#ifndef SHTDN_REASON_FLAG_PLANNED
#define SHTDN_REASON_FLAG_PLANNED 0x80000000 //from reason.h
#endif
int WINAPI WinMain(HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = WINDOWCLASSNAME;
wincl.lpfnWndProc = WndProc;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
if(!RegisterClassEx(&wincl))
{
MessageBox(NULL, "Window Registration Failed!", ERRORTITLE,
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
0,
WINDOWCLASSNAME,
WINDOWTITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
WINDOWWIDTH,
WINDOWHEIGHT,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
hLeftArrangeButton = CreateWindow("BUTTON", BUTTONONETEXT, WS_CHILD | WS_VISIBLE | BS_TEXT,
20, 80, 240, 30, hwnd, NULL, hThisInstance, NULL);
hDoNotSaveSettings = CreateWindow("BUTTON", BUTTONTWOTEXT, WS_CHILD | WS_VISIBLE | BS_TEXT,
20, 120, 240, 30, hwnd, NULL, hThisInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", ERRORTITLE,
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nFunsterStil);
UpdateWindow(hwnd);
while(GetMessage(&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
void arrange()
{
const int arrange = 4118;
const int leftAlign = 1;
HWND desktopHandle;
HWND childWindow;
HWND childOfChildWindow;
desktopHandle = FindWindow("Progman", NULL);
childWindow = GetWindow(desktopHandle, GW_CHILD);
childOfChildWindow = GetWindow(childWindow, GW_CHILD);
SendMessage(childOfChildWindow, arrange, leftAlign , 0);
}
void noSaveSettings()
{
HKEY hk;
DWORD dwDisp;
DWORD value = 1;
if (RegCreateKeyEx(HKEY_CURRENT_USER, REGKEYNAME,
0, NULL, REG_OPTION_NON_VOLATILE,KEY_WRITE, NULL, &hk, &dwDisp))
{
MessageBox(NULL,"Unable to create Explorer key",ERRORTITLE,MB_OK);
}
if (RegSetValueEx(hk,"NoSaveSettings",0, REG_DWORD, (LPBYTE) &value, sizeof(DWORD)))
{
MessageBox(NULL,"Unable to add NoSaveSettings",ERRORTITLE,MB_OK);
RegCloseKey(hk);
}
RegCloseKey(hk);
}
void restart()
{
HANDLE tokenHandle;
TOKEN_PRIVILEGES theTokenPrivilege;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenHandle))
{
MessageBox(NULL,"Could not obtain a token",ERRORTITLE,MB_OK);
}
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &theTokenPrivilege.Privileges[0].Luid);
theTokenPrivilege.PrivilegeCount = 1;
theTokenPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(tokenHandle, FALSE, &theTokenPrivilege, 0, (PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
{
MessageBox(NULL,"Unable to restart the computer",ERRORTITLE,MB_OK);
return;
}
if (!ExitWindowsEx(EWX_REBOOT | EWX_FORCE, SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
SHTDN_REASON_MINOR_RECONFIG | SHTDN_REASON_FLAG_PLANNED))
{
MessageBox(NULL,"Unable to restart the computer",ERRORTITLE,MB_OK);
return;
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
HWND hwndCtl = (HWND)lParam;
int code = HIWORD(wParam);
if (hwndCtl == hLeftArrangeButton)
{
arrange();
}
if (hwndCtl == hDoNotSaveSettings)
{
noSaveSettings();
restart();
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
Hope this helps.
|