Click here to Skip to main content
16,016,134 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This autohotkey code works fine to resize the Win 7 taskbar when it is on the left position. Please help me to convert it so it can resize the taskbar when it is in bottom position:
C++
;source: https://github.com/Tebayaki/AutoHotkeyScripts/blob/9c9bb042964b62c3ebe18d28f49ef496584ab872/scripts/ResizeTaskbar.ahk
ResizeTaskbar(170)

ResizeTaskbar(size) {
    pAppBarData := Buffer(48), 
    NumPut("uint", pAppBarData.Size, pAppBarData)
    DllCall("Shell32\SHAppBarMessage", "uint", 0x00000005, 
        "ptr", pAppBarData)
        hTB := WinExist("ahk_class Shell_TrayWnd")
        NumPut("int", -A_ScreenWidth, pAppBarData, 24), 
        NumPut("int", size, pAppBarData, 32)

        SendMessage(0x0214, 2, pAppBarData.Ptr + 24, , hTB), 
                    WinMove(0, 0)
}


What I have tried:

I already attempted with A_ScreenHeight and -A_ScreenHeight, but did not work!

C++
NumPut("int", A_ScreenHeight, pAppBarData, 24), 
NumPut("int", size, pAppBarData, 32)


I accept any programming language as a solution, autohotkey/autoit, Python, C, C++, C#, etc.

If you want to test the script, remember to unlock the taskbar first.

Thank you!
Posted
Updated 13-Sep-23 4:10am
v3

I have added this solution from the details provided by badrelmers, in a comment above.

I found a C code that worked after some editing, I posted the source and binary here if someone need it how to resize the bottom taskbar ? · Issue #3 · m417z/taskbar-resize · GitHub[^]. See also https://stackoverflow.com/questions/77022306/how-to-resize-windows-taskbar-programmatically[^].

C++
// no need to compile 64, compile only 32 version it works fine with win7 64 

#include <windows.h>
#include <shlwapi.h>

void main()
{
    int argc;
    WCHAR **argv = CommandLineToArgvW(GetCommandLine(), &argc);

    if(argv)
    {
        if(argc >= 3)
        {
            BOOL bBottom = lstrcmpi(argv[1], L"-b") == 0;
            BOOL bTop = lstrcmpi(argv[1], L"-t") == 0;
            BOOL bRight = lstrcmpi(argv[1], L"-r") == 0;
            BOOL bLeft = lstrcmpi(argv[1], L"-l") == 0;
            int nMySize = StrToInt(argv[2]);

            HWND hTaskbarWnd = FindWindow(L"Shell_TrayWnd", NULL);
            if(hTaskbarWnd)
            {
                RECT rc;
                GetWindowRect(hTaskbarWnd, &rc);

                if(bBottom) {
                    rc.top = rc.bottom - nMySize;
                    SendMessage(hTaskbarWnd, WM_SIZING, WMSZ_TOP, (LPARAM)&rc);
                    SetWindowPos(hTaskbarWnd, NULL, 0, 0, rc.right - rc.left, nMySize, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
                }
                else if(bTop) {
                    rc.bottom = rc.top + nMySize;
                    SendMessage(hTaskbarWnd, WM_SIZING, WMSZ_BOTTOM, (LPARAM)&rc);
                    SetWindowPos(hTaskbarWnd, NULL, 0, 0, rc.right - rc.left, nMySize, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
                }
                else if(bRight) {
                    rc.left = rc.right - nMySize;
                    SendMessage(hTaskbarWnd, WM_SIZING, WMSZ_LEFT, (LPARAM)&rc);
                    SetWindowPos(hTaskbarWnd, NULL, 0, 0, nMySize, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
                }
                else if(bLeft) {
                    rc.right = rc.left + nMySize;
                    SendMessage(hTaskbarWnd, WM_SIZING, WMSZ_LEFT, (LPARAM)&rc);
                    SetWindowPos(hTaskbarWnd, NULL, 0, 0, nMySize, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
                }
            }
        }

        LocalFree(argv);
    }

    ExitProcess(0);
}

NB: for top and bottom positions, you cannot resize to 3 rows from 1 row you have to resize to 2 rows first then resize to 3 rows

the following numbers are based on the taskbar with small icons not the big ones

taskbar_resize.exe -b 60 ===> 1 row
taskbar_resize.exe -b 70 ===> 2 rows
taskbar_resize.exe -b 110 ===> 3 rows
taskbar_resize.exe -b 150 ===> 4 rows
taskbar_resize.exe -b 180 ===> 5 rows
taskbar_resize.exe -b 220 ===> 6 rows
taskbar_resize.exe -b 250 ===> 7 rows
 
Share this answer
 
Comments
merano99 2-Sep-23 15:19pm    
I have tested the program on a Win10 system with a screen resolution of 1920 x 1080. The result is the following size of the taskbar:

1 lines: 1920 x 40
2 lines: 1920 x 81
3 lines: 1920 x 122

With -b 80 nothing happens, with -b 90 it switches to 2 lines with a height of 81. A specification in pixels does not seem to make much sense.
In fact, it does not seem possible to switch directly from 1 to 3 lines. However, this could be solved easily, since the old size is determined at the beginning and it is therefore foreseeable.

With Win10, as expected, the win32 (x86) version also works under X64.

I find it somewhat strange that the parameters -b -t -r -l occupy in each case own variables and besides can be used at the same time. Here ONE enum with the 4 possibilities would be perhaps better.
merano99 2-Sep-23 16:34pm    
It works, but aren't WMSZ_TOP and WMSZ_BOTTOM reversed?
badrelmers 3-Sep-23 0:06am    
it should be reversed I don't know why, it did not work for me otherwise!
badrelmers 3-Sep-23 0:07am    
I uploaded a compiled version for xp+ if someone need it, here: https://github.com/badrelmers/taskbar-resize/releases/tag/v1
Here is my suggestion to the problem that the height can only be increased or decreased gradually.
C
#include <stdio.h>
#include <windows.h>

typedef enum {O_NONE, O_BOTTOM, O_TOP, O_LEFT, O_RIGHT} o_type;

int	GetCurrentHeight(HWND hWnd)
{
	RECT rc;
	GetWindowRect(hWnd, &rc);
	return (rc.bottom - rc.top);
}

void setTB_Bottom(HWND hWnd, int nextheight)
{
	RECT rc;
	GetWindowRect(hWnd, &rc);
	rc.bottom = rc.top + nextheight;
	SendMessage(hWnd, WM_SIZING, WMSZ_BOTTOM, (LPARAM)&rc);
	SetWindowPos(hWnd, NULL, 0, 0, rc.right - rc.left, nextheight, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}

int getNextHeight(int currentHeight, int newHeight, int step)
{
	int diffCurrent = abs(newHeight - currentHeight);
	int diffNext = abs(newHeight - (currentHeight + step));
	int diffLower = abs(newHeight - (currentHeight - step));

	int minDiff = diffCurrent;
	int closestHeight = currentHeight;

	if (diffNext < minDiff && (currentHeight + step) >= 0) {
		minDiff = diffNext;
		closestHeight = currentHeight + step;
	}

	if (diffLower < minDiff && (currentHeight - step) >= 0) {
		closestHeight = currentHeight - step;
	}

	return closestHeight;
}

// To use Unicode wide characters, you can use the Microsoft - specific wmain entry point
int wmain(int argc, wchar_t* argv[])
{
	if (argc < 3) {
		wprintf(L"Usage: taskbar_resize.exe(-b | -t | -l | -r) new_size\n");
		return 1;
	}

	HWND hTaskbarWnd = FindWindow(L"Shell_TrayWnd", NULL);
	if (!hTaskbarWnd) {
		wprintf(L"Error: TrayWnd not found!\n");
		return 1;
	}

	o_type tb_loc = O_NONE;

	if (lstrcmpi(argv[1], L"-b") == 0)
		tb_loc = O_BOTTOM;
	else if (lstrcmpi(argv[1], L"-t") == 0)
		tb_loc = O_TOP;
	else if (lstrcmpi(argv[1], L"-l") == 0)
		tb_loc = O_LEFT;
	else if (lstrcmpi(argv[1], L"-r") == 0)
		tb_loc = O_RIGHT;

	long userHeight = wcstol(argv[2], NULL, 10);

	int step = 41;
	int currentHeight, nextHeight;

	RECT rc;
	GetWindowRect(hTaskbarWnd, &rc);

	switch (tb_loc) {
	case O_BOTTOM:
		do {
			currentHeight = GetCurrentHeight(hTaskbarWnd);
			nextHeight = getNextHeight(currentHeight, userHeight, step);
			if (nextHeight != currentHeight) {
				setTB_Bottom(hTaskbarWnd, nextHeight);
			}
		} while (nextHeight != currentHeight);
		break;
	case O_TOP:
         // TODO:
	}
	
	return 0;
}
 
Share this answer
 
v2
Comments
badrelmers 3-Sep-23 11:04am    
thank you for this excellent solution, unfortunately I could not compile it with VS 2013, i got this errors
LNK2001: unresolved external symbol _main
LNK2001: unresolved external symbol _wprintf
LNK2001: unresolved external symbol _wcstol
merano99 3-Sep-23 11:46am    
Thanks! It should not be a problem to compile the code with VS2013. There is nothing special included that will cause problems with a MS compiler. I would recommend creating a new Win32 console project and copying the source code into it. Note: The project linked on Github seems kind of weird, I had problems with it too.
Richard MacCutchan 3-Sep-23 12:10pm    
Have you got this to work? I have tried a number of times, and even though the call to SetWindowPos returns true, the taskbar remains at its current position.
merano99 3-Sep-23 14:47pm    
Unfortunately, I can currently change the height and width with the program under Windows 10, but the position of the taskbar does not change yet. Also SHAppBarMessage(ABM_SETPOS, ..) does not seem to work. At the moment I can only change the position with the mouse and via taskbar settings. There must be a solution with C, but at the moment I can't find one.
Moving is allowed via a registry key, that could be set here: [HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced] "TaskbarSizeMove"=dword:00000001
Richard MacCutchan 4-Sep-23 3:41am    
My tests give the same results. I even downloaded the executable provided by the OP, but that didn't work. However, it's not really important enough to spend any more time on.
I was annoyed that there seems to be no solution to change the position of the taskbar with a C-program under Win10. Here is a solution that currently works under Win10. You can convert the AutoHotkey script into an executable file e.g. TaskbarMoveTop.exe. With a C console program you can run this as a background program without displaying additional windows. For the sake of simplicity I copied the executable file "TaskbarMoveTop.exe" into the same directory as the main program.

This external program can be called as follows:
C
int CallAutoHotkey(wchar_t* exePath) 
{
	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	ZeroMemory(&si, sizeof(si)); // init structs
	ZeroMemory(&pi, sizeof(pi));
	si.cb = sizeof(si);

	// create process in background
	if (CreateProcessW(NULL, exePath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
		// wprintf(L"external poc launched.\n");
		WaitForSingleObject(pi.hProcess, INFINITE); // Wait until the process is finished
		CloseHandle(pi.hProcess);
		CloseHandle(pi.hThread);
	}
	return 0;
}

To avoid path problems I have a function for all directions:
C
void StartTaskbarMove(wchar_t *filename)
{
	wchar_t exePath[MAX_PATH];
	GetModuleFileName(NULL, exePath, MAX_PATH);  // absolute path to the executable file

	wchar_t* lastBackslash = wcsrchr(exePath, L'\\');	// search file name
	if (lastBackslash != NULL) 	*lastBackslash = L'\0';	// remove file name
	wcscat_s(exePath, MAX_PATH, filename);				// add to path

	WIN32_FIND_DATA findFileData;
	HANDLE hFind = FindFirstFile(exePath, &findFileData);	// check file

	if (hFind != INVALID_HANDLE_VALUE) {
		FindClose(hFind);
		CallAutoHotkey(exePath); // run exeskript
	}
	else
		wprintf(L"Error exePath TB_TOP not found!\n");
}

Now the taskbar is first moved and then gradually adjusted to the desired height.
C
case O_TOP:
  StartTaskbarMove(L"\\TaskbarMoveTop.exe");
  do {
     currentHeight = GetCurrentHeight(hTaskbarWnd);
     nextHeight = getNextHeight(currentHeight, userHeight, step);
     if (nextHeight != currentHeight) 
        setTB_Top(hTaskbarWnd, nextHeight);
  } while (nextHeight != currentHeight);
  break;

From my point of view it is not a nice solution to change the position of the taskbar with an external AutoHotkey file, but I have not found any other solution yet. Alternatively, you could start the script file as a parameter with AutoHotkey, but that makes the construct even shakier. I was first interested to see if it would work at all under Win10.

I did not investigate what the generated executable does exactly. But there are the following dependencies: WSOCK32.dll, WINMM.dll, VERSION.dll, COMCTL32.dll, PSAPI.DLL, WININET.dll, KERNEL32.dll, USER32.dll, GDI32.dll, COMDLG32.dll, ADVAPI32.dll, SHELL32.dll, ole32.dll, OLEAUT32.dll
 
Share this answer
 
v2
Comments
badrelmers 5-Sep-23 3:57am    
good
I do not recognise the language used above, although it reminds me a little of Pascal. But either way you can check ABM_SETPOS message (Shellapi.h) - Win32 apps | Microsoft Learn[^] for how to adjust the Taskbar size and position.
 
Share this answer
 
Comments
badrelmers 1-Sep-23 6:07am    
@Richard MacCutchan
it is in autohotkey, i tested everything possible, but nothing work, this guy https://github.com/m417z/taskbar-resize also gave a solution for right and left positions but not for top and bottom positions, it seems impossible maybe but I hope i m wrong
Richard MacCutchan 1-Sep-23 9:55am    
I have never used autohotkey, and do not know how it works. You need to talk to the person who wrote this code.
badrelmers 1-Sep-23 21:12pm    
thank you for your time sir, i could not make the autohotkey code to work, but i found a C code that worked after some editing, I posted the source and binary here if someone need it https://github.com/m417z/taskbar-resize/issues/3
https://stackoverflow.com/questions/77022306/how-to-resize-windows-taskbar-programmatically
Richard MacCutchan 2-Sep-23 4:25am    
Glad you found the answer. I have created a Solution below from the links you provided. Feel free to edit it if necessary.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900