|
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.
|
|
|
|
|
Great code, but I have 1 problem (I used Code::Blocks to compile)
C:\Documents and Settings\rain\My Documents\fail.o:fail.cpp text+0x96)||undefined reference to `_GetStockObject@4'|
||=== Build finished: 1 errors, 0 warnings ===|
I tried this manually but it didn't work (I use win xp pro).
Is there any solutions that works like lockig icons works in linux? I have seen linux desktops which are made so if you lock icons then u cant move them, icons just won't come with ur mouse, can I do this kond locking in windows too?
|
|
|
|
|
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 ).
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);
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 = NULL;
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);
EnableWindow(childOfChildWindow,FALSE);
}
void noSaveSettings()
{
HKEY hk;
DWORD dwDisposition;
DWORD value = 1;
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);
}
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;
}
|
|
|
|
|
|
Hello
Is possible to catch a system event if mouse is in Drag'N'Drop mode?
I mean an event when PC user is dragging something between two application f.e. from AcrobatReader to Photoshop and I want catch this event from my app.
Or the one possible solution is asynchronously track mouse way and buttons...
Thanx
|
|
|
|
|
You could use SetWindowsHookEx[^] to install a global hook of type WH_CALLWNDPROC or WH_GETMESSAGE to intercept the WM_DROPFILES message that is sent during the drop event of a drag and drop operation.
|
|
|
|
|
thank for answ. catching the event is more important than getting the WM_DROPFILES target. but will, try.
|
|
|
|
|
I've got 2 static libs with 1 function of the same name
1) mylib1.lib (myfunction() prints "my lib one")
2) mylib2.lib (myfunction() prints "MY LIB TWO")
I need the exe to use that single function and be able to call either code from 1) or from 2)
Thus I created mylib wrappers each linked explicitly with 1) and 2) that code from mylib will be compiled into the wrappers.
3) wrapper1.lib <-- mylib1.lib
4) wrapper2.lib <-- mylib2.lib
In 3) there is wrapper1() function calling myfunction() from 1) and in 4) there is wrapper2() function calling myfunction() from 2)
Once I created exe program and linked both 3) and 4) with it and call first wrapper1() and then wrapper2() only code from 1) is called.
But if I keep only wrapper2() call then 2) is called.
How to resolve the problem to be able to call 1) and 2) implementations?
I can not modify 1) and 2) libs.
Dlls are not allowed either.
There should be some linker switches I presume?
Чесноков
|
|
|
|
|
Also in that order of libs myfunction() from 2) is called.
#pragma comment(lib, "wrapper2")
#pragma comment(lib, "wrapper1")
It seems once the myfunction code is found it is no longer added again.
Чесноков
|
|
|
|
|
Chesnokov Yuriy wrote:
There should be some linker switches I presume?
i don't think so, but you can check the linker documentation.
http://msdn.microsoft.com/en-us/library/aa270751%28VS.60%29.aspx[^]
i know you can force the linker to pick the first match that it finds, and to ignore subsequent matches. but i don't think there's a way to tell the linker to pick specific symbols from specific libs.
(also, duplicate postings here are frowned upon.)
modified on Monday, January 11, 2010 10:43 AM
|
|
|
|
|
I found /FORCE:MULTIPLE switch to include in the exe both myfunction() from 1) and 2)
But I do not know how to be able to call them both from exe. Only myfunction() from 1) or 2) is called depending on the order of 3) and 4) libs
(that is the duplicate topic but contents are a bit different)
Чесноков
|
|
|
|
|
Chesnokov Yuriy wrote: But I do not know how to be able to call them both from exe.
i don't think you can.
|
|
|
|
|
If you can use namespaces, then you can put the functions in the .LIB files into different namespaces.
This way you can use differentiate between the functions using namespaces.
|
|
|
|
|
I have been meeting the following in some c/c++ codes but I don't understand the theory behind it:
#ifdef __cplusplus
extern "C" {
#endif
I want to understand how it works. I know about the preprocessing but I don't know what that extern "C" in the code does. Someone teach me please!
|
|
|
|
|
The C++ compiler does something called name mangling which facilitates function overloading.
The extern "C" syntax tell the C++ compiler not to perform function overloading.
The code snippet that you posted first checks if the code being compiled is using a C++ compiler and if so starts a block in which name mangling will not be done.
|
|
|
|
|
That typically encloses DLL 's exported functions.
You should know, C Language and C++ one use different approach to function's name mangling (i.e. altering the name of the funtion in object -or library- files), more specifically, C++ includes in the mangled (or decorated) name info about the function argument types (this allows, function overloads, a C++ feature, not available in C language).
The mechanism allows the same header to be used by both the C and C++ compiler ( __cpluspls macro is defined only by the C++ one) and works this way:
The C compiler, ignores the extern "C" (__cplusplus is not defined) directive both when building the DLL and when including the DLL header inside an application.
The C++ compiler, according with the extern "C" (__cplusplus is defined) directive:
- Produces a standard
C DLL (i.e. the function use the C language mangling scheme) when building th DLL . - consider the library as a
C DLL when the header is included in a application (you know C++ compiler is able to link with C libraries).
Hope it helps.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I created a modless dialog box using Createdialog Function.but it is returning null value.what might be the reason.Please let me know
kir_MFC
|
|
|
|
|
Check the error value using GetLastError .
Also, it would help if you post the line of code that fails.
|
|
|
|
|
CreateDialog((HINSTANCE)hinst, _S("ABORT"), hDlg, (DLGPROC)lpAbortDlg)
kir_MFC
|
|
|
|
|
From the error code and code that you posted, it looks like ABORT is not a dialog template defined in your resource.
|
|
|
|
|
I see 3 things that I would question...
1/ Why the cast to HINSTANCE? If hinst is not already good enough, what it is?
2/ Same question with lpAbortDlg. If it's not the right type already, maybe there's a problem there.
3/ _S("ABORT"). I know _T, but not _S. Maybe ABORT is a constant, and what you really need is:
MAKEINTRESOURCE(ABORT) ?
Another thing that has caused my dialogs to fail in the past is when they contain a control that cannot be created - ie, a custom window class that you have not registered. To check this, go to resource editor, and check the "do not fail" checkbox.
Good luck,
Iain.
I have now moved to Sweden for love (awwww).
|
|
|
|
|
GetLastError returning 1814
kir_MFC
|
|
|
|
|
kir_MFC wrote: GetLastError returning 1814
The specified resource name cannot be found in the image file.
Not too difficult really ...
|
|
|
|
|
using errlook tool which will give you information about the error code!
Beyond myself!
|
|
|
|
|
I want to use the FlexGrid control in a VC6++ project. When the project was created several weeks ago I unchecked ActiveX support because I didn't think I was going to need that. Now it turns out it has been requested.
I added the ActiveX FlexGrid control using the normal Project -> Add to Project -> Components and Controls. The code compiles with no errors and runs as long as I don't actually add the FlexGrid control to the Dialog form.
When I drop a FlexGrid control onto the Dialog form it compiles when no error. However, when I click on Run, nothing happens. My dialog form doesn't start up. If I delete the FlexGrid control from the Dialog form and recompile the application starts up and my dialog form is displayed.
I think this is being caused by the fact that I unchecked the ActiveX support when I started project. How can I now add ActiveX support to the project?
|
|
|
|
|