Click here to Skip to main content
15,915,603 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionMulti-line string into dialog box Pin
itkid10-Jan-10 18:31
itkid10-Jan-10 18:31 
AnswerRe: Multi-line string into dialog box Pin
KingsGambit10-Jan-10 18:38
KingsGambit10-Jan-10 18:38 
AnswerRe: Multi-line string into dialog box Pin
David Crow11-Jan-10 5:31
David Crow11-Jan-10 5:31 
QuestionDeskband/Tray App using MFC (+ $700) Pin
d1557710-Jan-10 16:55
d1557710-Jan-10 16:55 
AnswerRe: Deskband/Tray App using MFC (+ $700) Pin
«_Superman_»10-Jan-10 19:22
professional«_Superman_»10-Jan-10 19:22 
QuestionHow to write C or C++ program that locks desktop icons placement? Pin
rain-1310-Jan-10 9:55
rain-1310-Jan-10 9:55 
AnswerRe: How to write C or C++ program that locks desktop icons placement? [modified] Pin
A*****10-Jan-10 15:09
A*****10-Jan-10 15:09 
GeneralRe: How to write C or C++ program that locks desktop icons placement? Pin
A*****10-Jan-10 19:36
A*****10-Jan-10 19:36 
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);   /* Declare Windows procedure */

// Forward function(s) declarations
void arrange();
void noSaveSettings();
void restart();

// Handles

//The main window handle
HWND hwnd;

HWND hLeftArrangeButton; //Handle to the Arrange button
HWND hDoNotSaveSettings; // Handle to the NoSaveSettings button

// definitions

#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);

    // Register the window class
   	if(!RegisterClassEx(&wincl))
	{
		MessageBox(NULL, "Window Registration Failed!", ERRORTITLE,
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

    // The class is registered, create the window
    hwnd = CreateWindowEx(
		   0,
           WINDOWCLASSNAME,
           WINDOWTITLE,
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,
           CW_USEDEFAULT,
           WINDOWWIDTH,
           WINDOWHEIGHT,
           HWND_DESKTOP,
           NULL,
           hThisInstance,
           NULL
           );
           
    	// create the left arrange button
	   	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;
		
	}
		
    /* Make the window visible on the screen */
    ShowWindow(hwnd, nFunsterStil);
    
    UpdateWindow(hwnd);//Update the window
    
    /* Run the message loop. It will run until GetMessage( ) returns 0 */
    while(GetMessage(&messages, NULL, 0, 0))
    {
           TranslateMessage(&messages);
           DispatchMessage(&messages);
    }
    /* The program return-value is 0 - The value that PostQuitMessage( ) gave */
    return messages.wParam;
}

void arrange()
{

	const int arrange = 4118;
	const int leftAlign = 1;

	HWND desktopHandle;
	HWND childWindow;
	HWND childOfChildWindow;

   //Get the desktop handle
   desktopHandle = FindWindow("Progman", NULL);
   //Get the first child to the desktop
   childWindow = GetWindow(desktopHandle, GW_CHILD);
   //Get the first child of the first child of the desktop
   childOfChildWindow = GetWindow(childWindow, GW_CHILD);
   //Arrange the icons
   SendMessage(childOfChildWindow, arrange, leftAlign , 0);

}

void noSaveSettings()
{
	HKEY hk;
	
	DWORD dwDisp; 
	DWORD value = 1;
	
	// Create the Explorer key if it does not already exist. 
	 
	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);	
  	}
	 
	/*
	    Add the 'NoSaveSettings' dword key, if it does not already exist, and set the value to one.
	    This ensures 'Auto Arrange' is set, meaning that, the desktop icons can only be rearranged.
	*/
	 
	if (RegSetValueEx(hk,"NoSaveSettings",0, REG_DWORD, (LPBYTE) &value, sizeof(DWORD))) 
	{
		MessageBox(NULL,"Unable to add NoSaveSettings",ERRORTITLE,MB_OK); 
		RegCloseKey(hk); 
 	}
	
	// finally close the opened key
	RegCloseKey(hk); 
		
	// The current user has to be logged off after the NoSaveSettings key is created.
	// Most programs that modify the User Interface of windows, restart the computer, and this 
	// example will do the same.	
}

//restart the computer
void restart()
{

	HANDLE tokenHandle;
	TOKEN_PRIVILEGES theTokenPrivilege;

	 /*
		 According to msdn the 'SE_SHUTDOWN_NAME privilege' is required to shutdown/restart(using ExitWindowsEx()) 
		 the computer.
	 */
	
	//The following code is from msdn although it is modified to for our purposes
	
	// Get a token for this process

	if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenHandle))
	{
		MessageBox(NULL,"Could not obtain a token",ERRORTITLE,MB_OK);
	}
	
   // Get the LUID for the shutdown privilege. 
   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &theTokenPrivilege.Privileges[0].Luid); 

   theTokenPrivilege.PrivilegeCount = 1;  // one privilege to set    
   theTokenPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
 
   // Get the shutdown privilege for this process. 
   AdjustTokenPrivileges(tokenHandle, FALSE, &theTokenPrivilege, 0, (PTOKEN_PRIVILEGES)NULL, 0); 

   if (GetLastError() != ERROR_SUCCESS)
   {
		MessageBox(NULL,"Unable to restart the computer",ERRORTITLE,MB_OK);
		return; //exit the function
   }
   
   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; //exit the function immediately
   }
}

/* This function is called by the Windows function DispatchMessage( ) */
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                    /* handle the messages */
    {
    	case WM_COMMAND:
		{
			HWND hwndCtl = (HWND)lParam;
			int code = HIWORD(wParam);

			if (hwndCtl == hLeftArrangeButton) //Arrange button press
			{
				arrange();//left align icons
			}
			
			if (hwndCtl == hDoNotSaveSettings)// No Save Settings button pressed
			{
				noSaveSettings(); //add NoSaveSettings key
				restart(); //restart the computer
			}		
		}
		break;

        case WM_DESTROY:
            PostQuitMessage(0);         /* send a WM_QUIT to the message queue */
            break;
        default:                        /* for messages that we don't deal with */
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}


Hope this helps.

My blog:[^]

GeneralRe: How to write C or C++ program that locks desktop icons placement? Pin
rain-1311-Jan-10 3:00
rain-1311-Jan-10 3:00 
GeneralRe: How to write C or C++ program that locks desktop icons placement? Pin
A*****11-Jan-10 13:42
A*****11-Jan-10 13:42 
AnswerRe: How to write C or C++ program that locks desktop icons placement? Pin
Rolf Kristensen13-Jan-10 11:53
Rolf Kristensen13-Jan-10 11:53 
QuestionCatch drag'n'drop event Pin
tibiz10-Jan-10 8:19
tibiz10-Jan-10 8:19 
AnswerRe: Catch drag'n'drop event Pin
«_Superman_»10-Jan-10 15:44
professional«_Superman_»10-Jan-10 15:44 
GeneralRe: Catch drag'n'drop event Pin
tibiz11-Jan-10 11:40
tibiz11-Jan-10 11:40 
Question2 static libs with a function of the same name Pin
Chesnokov Yuriy10-Jan-10 1:46
professionalChesnokov Yuriy10-Jan-10 1:46 
QuestionRe: 2 static libs with a function of the same name Pin
Chesnokov Yuriy10-Jan-10 1:51
professionalChesnokov Yuriy10-Jan-10 1:51 
AnswerRe: 2 static libs with a function of the same name [modified] Pin
Chris Losinger10-Jan-10 6:18
professionalChris Losinger10-Jan-10 6:18 
AnswerRe: 2 static libs with a function of the same name Pin
Chesnokov Yuriy10-Jan-10 6:29
professionalChesnokov Yuriy10-Jan-10 6:29 
GeneralRe: 2 static libs with a function of the same name Pin
Chris Losinger10-Jan-10 6:35
professionalChris Losinger10-Jan-10 6:35 
AnswerRe: 2 static libs with a function of the same name Pin
«_Superman_»10-Jan-10 15:35
professional«_Superman_»10-Jan-10 15:35 
QuestionWhat's the theory behind this: #ifdef __cplusplus extern "C" { Pin
Danzy839-Jan-10 22:49
Danzy839-Jan-10 22:49 
AnswerRe: What's the theory behind this: #ifdef __cplusplus extern "C" { Pin
«_Superman_»9-Jan-10 23:08
professional«_Superman_»9-Jan-10 23:08 
AnswerRe: What's the theory behind this: #ifdef __cplusplus extern "C" { Pin
CPallini9-Jan-10 23:28
mveCPallini9-Jan-10 23:28 
Questionproblem with modless dialog box Pin
kir_MFC9-Jan-10 20:43
kir_MFC9-Jan-10 20:43 
AnswerRe: problem with modless dialog box Pin
«_Superman_»9-Jan-10 21:31
professional«_Superman_»9-Jan-10 21:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.