Sign in to follow this  

Somebody please help with Taskbar repositioning?

This topic is 3493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there again, The below code is working, however, the problem is that the Taskbar is only repositioned temporarily - a few seconds and then it is return back to the bottom of the screen. Can somebody please help as I want it to be permanently repositioned the taskbar at the top of the screen?

#include <stdio.h>
#include <windows.h>
#include <shellapi.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR 
lpCmdLine, int nShowCmd)
{
 RECT rect;
 RECT deskTopRect;
 HWND hDesktopWindow;
 char* strMessage = "Your Taskbar is now refreshed.";
 HWND hWnd = FindWindow("Shell_TrayWnd", NULL);
 APPBARDATA abd;
 ZeroMemory(&abd, sizeof(APPBARDATA));
 abd.cbSize = sizeof(APPBARDATA);

 SHAppBarMessage(ABM_GETTASKBARPOS, &abd);
 GetWindowRect(hWnd, &rect);
 SetWindowPos(hWnd, NULL, rect.left, 0, rect.right, rect.bottom-rect.top, SWP_NOSENDCHANGING);

 ShowWindow(hWnd, SW_SHOW);
 UpdateWindow(hWnd);
 GetWindowRect(hWnd, &rect);

 hDesktopWindow = GetDesktopWindow();

 GetWindowRect(hDesktopWindow, &deskTopRect);
 deskTopRect.top = rect.bottom;
 RedrawWindow(hDesktopWindow, NULL, NULL, 
RDW_FRAME|RDW_INVALIDATE|RDW_UPDATENOW|RDW_ALLCHILDREN);
 SystemParametersInfo(SPI_SETWORKAREA, 0, (LPVOID)&deskTopRect, 
SPIF_SENDCHANGE);

 //SystemParametersInfo(SPI_SETWORKAREA, 0, NULL, SPIF_SENDCHANGE);
 UpdateWindow(hDesktopWindow);

 MessageBox(NULL, strMessage, "Taskbar Refresh", MB_OK);
 return 0;
}


Share this post


Link to post
Share on other sites
This issue is most likely that the Taskbar maintains internal state about where on the screen it is positioned and it updates that state when the user drags it to the new location. When the Taskbar does some sort of update to itself it gets repositioned according to its internal state.

You do not have access to that state information since no one is supposed to be moving it programmatically.

That's my guess anyway. The main point is that what you're doing is not something that you are supposed to be doing so expecting it to work is a bit of a longshot.

Share this post


Link to post
Share on other sites
I agree with Colin for the most part, you shouldn't really be moving the taskbar around. But anyway if you had to the only way is via the registry.

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects2

It's a binary value with the 13th byte set it as follows

00 = left
01 = top
02 = right
03 = bottom

You will need to close the taskbar to update it. Here is some Delphi code I found to do it, should be easy enough to translate to C++

//////////////
//stpPos
//0 = Left
//1 = Top
//2 = Right
//3 = Bottom
//////////////
uses Registry;
procedure SetTrayPosition(stpPos: Integer);
type
TBytes = Array[1..4] Of Byte;
var
Output : Array[1..40] Of Byte;
Top,
Left,
Right,
Bottom,
Index : Integer;
tArray : TBytes;
Reg : TRegistry;
PID : Cardinal;
////////////////////////////////
function itoa(Input: Integer): TBytes;
begin
Result[1] := StrToInt('$' + IntToHex(Byte(Chr(Input)),2));
Result[2] := StrToInt('$' + IntToHex(Byte(Chr(Input div 256)),2));
Result[3] := StrToInt('$' + IntToHex(Byte(Chr(Input div 65536)),2));
Result[4] := StrToInt('$' + IntToHex(Byte(Chr(Input div 16777216)),2));
end;
////////////////////////////////
begin
Case stpPos Of
0:
begin
Left := 0;
Top := 0;
Right := 34;
Bottom := Screen.Height;
end;
1:
begin
Left := 0;
Top := 0;
Right := Screen.Width;
Bottom := 34;
end;
2:
begin
Left := Screen.Width - 34;
Top := 0;
Right := Screen.Width;
Bottom := Screen.Height;
end;
3:
begin
Left := 0;
Top := Screen.Height - 34;
Right := Screen.Width;
Bottom := Screen.Height;
end;
end;
FillChar(Output,40,0);
Output[1] := $28;
Output[2] := $0;
Output[3] := $0;
Output[4] := $0;
Output[5] := $FF;
Output[6] := $FF;
Output[7] := $FF;
Output[8] := $FF;
Output[9] := $2;
Output[10] := $0;
Output[11] := $0;
Output[12] := $0;
Output[13] := stpPos;
Output[14] := $0;
Output[15] := $0;
Output[16] := $0;
Output[17] := $6B;
Output[18] := $0;
Output[19] := $0;
Output[20] := $0;
Output[21] := $22;
Output[22] := $0;
Output[23] := $0;
Output[24] := $0;
tArray := itoa(Left);
For Index := 1 To 4 Do
begin
Output[24 + Index] := tArray[Index];
end;
tArray := itoa(Top);
For Index := 1 To 4 Do
begin
Output[28 + Index] := tArray[Index];
end;
tArray := itoa(Right);
For Index := 1 To 4 Do
begin
Output[32 + Index] := tArray[Index];
end;
tArray := itoa(Bottom);
For Index := 1 To 4 Do
begin
Output[36 + Index] := tArray[Index];
end;
Try
Reg := TRegistry.Create;
Reg.RootKey := HKEY_CURRENT_USER;
If Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects2',False) Then
begin
Reg.WriteBinaryData('Settings',Output,40);
GetWindowThreadProcessID(FindWindow('Shell_TrayWnd',''),PID);
If PID > 0 Then
begin
TerminateProcess(OpenProcess(PROCESS_ALL_ACCESS,False,PID),0);
end;
end;
Finally
Reg.CloseKey;
Reg.Free;
end;
end;

Share this post


Link to post
Share on other sites

This topic is 3493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this