Click here to Skip to main content
15,896,207 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
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 
For some reason GetStockObject() is not defined. You need to add the following lines to windows.h

void * GetStockObject(int value);


If that does not work then replace this line
wincl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH)
with
wincl.hbrBackground = NULL;
and the window should turn transparent.

Here is some code that partially accomplishes what you want.
It does lock the icons on the desktop, although you will not
be able to click on them(A nasty trick you could play on someone Big Grin | :-D ).

Press the 'Arrange Icons' button to lock the desktop icons.

To reenable the desktop icons uncomment this line
EnableWindow(childOfChildWindow,TRUE);

in the 'void arrange()' function and recompile.

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 = NULL;

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

       // create the NoSaveSettings button
		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);

   EnableWindow(childOfChildWindow,FALSE); //disable the desktop icons
   //EnableWindow(childOfChildWindow,TRUE); //uncomment to reenable the desktop icons 
   //ShowWindow(childOfChildWindow,SW_HIDE);//uncomment to hide desktop icons'
//ShowWindow(childOfChildWindow,SW_SHOW);//uncomment to show desktop icons'

}

void noSaveSettings()
{
	HKEY hk;

	DWORD dwDisposition;
	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, &dwDisposition))
	{
		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, it is modified 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;
}


My blog:[^]

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 
GeneralRe: problem with modless dialog box Pin
kir_MFC9-Jan-10 21:43
kir_MFC9-Jan-10 21:43 
GeneralRe: problem with modless dialog box Pin
«_Superman_»9-Jan-10 23:11
professional«_Superman_»9-Jan-10 23:11 

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.