这个搜索菜单如何对齐下面的搜索框

如图所示,这个偏右用起来是真难受
(补充:任务栏为居中对齐)

预览版:

你这个位置搜索我有一些版本复现不了。是有相关 Windhawk 模组的,我只能居中给你测试,你用这一个:
Shell Flyout Positions - Windhawk
在设置下面:Start menu/开始菜单 - Horizontal alignment/水平对齐
假设后续任务栏居中,再搭配这个 开始按钮始终在左侧 - Windhawk,图标跟着过去,其他不变

你要不再扩展一点吧,可以试一下替换掉它 win 搜索开始菜单中的 Everything 与 Power Tools - Windhawk

搜索为按钮和中等宽度的搜索框时 弹出的搜索菜单也是能够居中的(和开始菜单一样的位置)
但是变成最大的那种搜索框时 弹出的搜索菜单会变成偏右边

而且似乎这个mod不管怎么设置
最大那种搜索框 弹出的搜索菜单都没有变化

让开始菜单回归win10居左(

任务栏左对齐 搜索菜单确实是没有偏移问题的
但是 实际使用还是需要任务栏居中对齐的
因为任务栏左对齐模式下,点击左边图标鼠标移动实路径太长不顺手

那就隐藏搜索框,只保留搜索图标

歪个楼哈,其实我一直没明白这个搜索和直接按win键后弹出来的菜单里的搜索框有啥区别,如果一样的话改下操作习惯,直接按win键唤出这个框也很方便吧。

好吧看错了,需求是点左边的图标路径过长,不只是点这个搜索图标

@PandaFiredoge @chengsuibian
这个最大的搜索框 能够在里面直接显示搜索文字,有时还会显示节日图标
对比其他的好看很多
小的搜索框做不到这个效果

明白了

大佬 这个有办法解决吗?

我修改这一个其他都没问题,就搜索框图标会跟着走,这个想不到解决了

展开
// ==WindhawkMod==
// @id              search-menu-position-align
// @name            搜索菜单位置与对齐调整
// @description     Windows搜索菜单左侧,支持水平对齐与自定义位移调整
// @version         1.0
// @author          Custom
// @include         explorer.exe
// @include         SearchHost.exe
// @include         SearchApp.exe
// @architecture    x86-64
// @compilerOptions -luser32
// ==/WindhawkMod==

// ==WindhawkModSettings==
/*
- searchMenu:
  - alignment: left
    $name: 水平对齐
    $options:
      - default: Windows 默认
      - left: 靠左对齐
      - center: 居中对齐
      - right: 靠右对齐
  - horizontalShift: 0
    $name: 水平位移
    $description: 正值会将元素向右移动,负值会将其向左移动
*/
// ==/WindhawkModSettings==

#include <windows.h>
#include <windhawk_utils.h>
#include <wchar.h>

enum Alignment {
    ALIGN_DEFAULT,
    ALIGN_LEFT,
    ALIGN_CENTER,
    ALIGN_RIGHT
};

struct {
    Alignment alignment;
    int horizontalShift;
} g_settings;

void LoadSettings() {
    PCWSTR alignStr = Wh_GetStringSetting(L"searchMenu.alignment");
    if (wcscmp(alignStr, L"left") == 0) {
        g_settings.alignment = ALIGN_LEFT;
    } else if (wcscmp(alignStr, L"center") == 0) {
        g_settings.alignment = ALIGN_CENTER;
    } else if (wcscmp(alignStr, L"right") == 0) {
        g_settings.alignment = ALIGN_RIGHT;
    } else {
        g_settings.alignment = ALIGN_DEFAULT;
    }
    Wh_FreeStringSetting(alignStr);

    g_settings.horizontalShift = Wh_GetIntSetting(L"searchMenu.horizontalShift");
}

bool IsSearchTopWindow(HWND hWnd) {
    if (!hWnd || !IsWindow(hWnd)) {
        return false;
    }

    WCHAR className[256] = {0};
    if (!GetClassNameW(hWnd, className, ARRAYSIZE(className))) {
        return false;
    }

    if (wcscmp(className, L"Windows.UI.Core.CoreWindow") != 0) {
        return false;
    }

    WCHAR exePath[MAX_PATH] = {0};
    GetModuleFileNameW(NULL, exePath, ARRAYSIZE(exePath));
    if (wcsstr(exePath, L"SearchHost.exe") != NULL ||
        wcsstr(exePath, L"SearchApp.exe") != NULL) {
        return true;
    }

    return false;
}

int CalculateNewX(HWND hWnd, int currentX, int width) {
    HMONITOR hMon = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
    MONITORINFO mi = { sizeof(mi) };
    if (!GetMonitorInfoW(hMon, &mi)) {
        return currentX + g_settings.horizontalShift;
    }

    UINT dpi = GetDpiForWindow(hWnd);
    if (dpi == 0) {
        dpi = 96;
    }
    int scaledShift = MulDiv(g_settings.horizontalShift, dpi, 96);

    int workLeft = mi.rcWork.left;
    int workRight = mi.rcWork.right;
    int workWidth = workRight - workLeft;

    if (g_settings.alignment == ALIGN_LEFT) {
        return workLeft + scaledShift;
    } else if (g_settings.alignment == ALIGN_CENTER) {
        return workLeft + (workWidth - width) / 2 + scaledShift;
    } else if (g_settings.alignment == ALIGN_RIGHT) {
        return workRight - width + scaledShift;
    }

    return currentX + scaledShift;
}

using SetWindowPos_t = BOOL(WINAPI*)(HWND, HWND, int, int, int, int, UINT);
SetWindowPos_t SetWindowPos_Original;

int AdjustX(HWND hWnd, int fallbackX, int width) {
    RECT rc;
    if (!GetWindowRect(hWnd, &rc)) {
        return fallbackX;
    }

    int targetScreenX = CalculateNewX(hWnd, rc.left, width);
    HWND hParent = GetParent(hWnd);
    if (hParent) {
        POINT pt = { targetScreenX, rc.top };
        if (ScreenToClient(hParent, &pt)) {
            return pt.x;
        }
    }

    return targetScreenX;
}

void ApplyWindow(HWND hWnd) {
    if (!IsSearchTopWindow(hWnd)) {
        return;
    }

    RECT rc;
    if (!GetWindowRect(hWnd, &rc)) {
        return;
    }

    int width = rc.right - rc.left;
    int height = rc.bottom - rc.top;
    if (width <= 300) {
        return;
    }

    int newX = AdjustX(hWnd, rc.left, width);
    int newY = rc.top;

    HWND hParent = GetParent(hWnd);
    if (hParent) {
        POINT pt = { rc.left, rc.top };
        if (ScreenToClient(hParent, &pt)) {
            newY = pt.y;
        }
    }

    SetWindowPos_Original(hWnd, NULL, newX, newY, width, height,
                          SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE);
}

BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam) {
    (void)lParam;
    ApplyWindow(hWnd);
    return TRUE;
}

BOOL CALLBACK EnumTopProc(HWND hWnd, LPARAM lParam) {
    (void)lParam;

    DWORD pid = 0;
    GetWindowThreadProcessId(hWnd, &pid);
    if (pid == GetCurrentProcessId()) {
        ApplyWindow(hWnd);
        EnumChildWindows(hWnd, EnumChildProc, 0);
    }

    return TRUE;
}

BOOL WINAPI SetWindowPos_Hook(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags) {
    if (!(uFlags & SWP_NOMOVE) && IsSearchTopWindow(hWnd)) {
        int width = cx;
        if ((uFlags & SWP_NOSIZE) || width <= 0) {
            RECT rc;
            if (GetWindowRect(hWnd, &rc)) {
                width = rc.right - rc.left;
            }
        }
        if (width > 300) {
            X = AdjustX(hWnd, X, width);
        }
    }

    return SetWindowPos_Original(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags);
}

using MoveWindow_t = BOOL(WINAPI*)(HWND, int, int, int, int, BOOL);
MoveWindow_t MoveWindow_Original;

BOOL WINAPI MoveWindow_Hook(HWND hWnd, int X, int Y, int nWidth, int nHeight, BOOL bRepaint) {
    if (IsSearchTopWindow(hWnd) && nWidth > 300) {
        X = AdjustX(hWnd, X, nWidth);
    }

    return MoveWindow_Original(hWnd, X, Y, nWidth, nHeight, bRepaint);
}

using DeferWindowPos_t = HDWP(WINAPI*)(HDWP, HWND, HWND, int, int, int, int, UINT);
DeferWindowPos_t DeferWindowPos_Original;

HDWP WINAPI DeferWindowPos_Hook(HDWP hWinPosInfo, HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT uFlags) {
    if (!(uFlags & SWP_NOMOVE) && IsSearchTopWindow(hWnd)) {
        int width = cx;
        if ((uFlags & SWP_NOSIZE) || width <= 0) {
            RECT rc;
            if (GetWindowRect(hWnd, &rc)) {
                width = rc.right - rc.left;
            }
        }
        if (width > 300) {
            x = AdjustX(hWnd, x, width);
        }
    }

    return DeferWindowPos_Original(hWinPosInfo, hWnd, hWndInsertAfter, x, y, cx, cy, uFlags);
}

void ApplyToExistingWindows() {
    EnumWindows(EnumTopProc, 0);
}

BOOL Wh_ModInit() {
    LoadSettings();

    HMODULE hUser32 = GetModuleHandleW(L"user32.dll");
    if (hUser32) {
        void* pSetWindowPos = (void*)GetProcAddress(hUser32, "SetWindowPos");
        if (pSetWindowPos) {
            Wh_SetFunctionHook(pSetWindowPos, (void*)SetWindowPos_Hook, (void**)&SetWindowPos_Original);
        }

        void* pMoveWindow = (void*)GetProcAddress(hUser32, "MoveWindow");
        if (pMoveWindow) {
            Wh_SetFunctionHook(pMoveWindow, (void*)MoveWindow_Hook, (void**)&MoveWindow_Original);
        }

        void* pDeferWindowPos = (void*)GetProcAddress(hUser32, "DeferWindowPos");
        if (pDeferWindowPos) {
            Wh_SetFunctionHook(pDeferWindowPos, (void*)DeferWindowPos_Hook, (void**)&DeferWindowPos_Original);
        }
    }

    ApplyToExistingWindows();
    return TRUE;
}

void Wh_ModSettingsChanged() {
    LoadSettings();
    ApplyToExistingWindows();
}

能兼容搜索框的,如果用上面我说的2,3个一起模组搜索面板调左它就没有间隙

确实搜索菜单确实居中了 但是搜索框变偏左了

能兼容搜索框的,如果用上面我说的2,3个一起模组搜索面板调左它就没有间隙

意思是用之前另外的那个Mod能修复?
——————————————————————

而且似乎稳定性似乎有点问题,过一会儿搜索菜单又不是居中了

用我上面的不能就搜索框做不到,如果你就用下列这两个就可以做到,只是你能不能接受第二个模组
开始按钮始终在左侧 - Windhawk
开始菜单中的 Everything 与 Power Tools - Windhawk

开始按钮始终在左侧模组,你在这模组设置全部选项都开启。目前我测试都没问题


如果你觉得这个面板不好看,有个 Windows 11 Start Menu Styler 模组能改变到这个搜索面板颜色这些