代码之家  ›  专栏  ›  技术社区  ›  minjang

如何以编程方式移动Windows任务栏?

  •  26
  • minjang  · 技术社区  · 16 年前

    我试图找到Win32 API,但似乎没有人做这项工作。

    编辑 当前位置我对许多人的意见感到惊讶。让我解释一下我为什么想要它。在我的工作场所,我使用双显示器(分辨率不同),任务栏放在左显示器上,主显示器放在右显示器上。然而,我经常通过远程桌面连接到我的工作电脑。远程连接后,将切换任务栏位置。这就是为什么我想做一个简单的程序,可以保存/恢复任务栏的位置。每天我都要重新安排我的任务栏。就这样。我只是想把它送给你 我 .

    5 回复  |  直到 16 年前
        1
  •  3
  •   Bob Moore    16 年前

        2
  •  8
  •   Zusukar    14 年前

    ; This script will try to drag and move the taskbar to where the *current* mouse
    ; cursor is
    
    ; 0x111: WM_COMMAND, 424: lock/unlock taskbar, http://www.codeproject.com/KB/miscctrl/Taskbar_Manipulation.aspx
    RegRead, TaskbarLocked, HKEY_CURRENT_USER, SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced, TaskbarSizeMove
    If TaskbarLocked = 0
      SendMessage 0x111, 424, , , ahk_class Shell_TrayWnd   
    
    WinActivate ahk_class Shell_TrayWnd
    MouseGetPos targetX, targetY
    ControlGetPos x, y, w, h, MSTaskListWClass1, ahk_class Shell_TrayWnd
    MouseMove x+1, y+1
    MouseClickDrag Left, x+1, y+1, targetX, targetY, 10
    
    ; often after dragging the taskbar to left or right side of a monitor, even though
    ; there are enough room to show two columns of icons, it will only show one column,
    ; it seems showing or hiding an icon will fix this
    Menu, Tray, NoIcon
    Menu, Tray, Icon
    
    ; lock the taskbar if it was previously locked
    If TaskbarLocked = 0
      SendMessage 0x111, 424, , , ahk_class Shell_TrayWnd   
    

    我已经用经典的窗口主题在Windows7上测试过了。要使用此功能,请指定一个热键来调用此脚本,然后将鼠标光标定位到要将任务栏拖动到的位置,然后按热键。

        3
  •  5
  •   i_am_jorf    16 年前

    任务栏是一个窗口。使用 SetWindowPos() 移动它。另见 SHAppBarMessage()

    要移动到另一个监视器,请使用 EnumDisplayMonitors() 具有 GetMonitorInfo() . 某些监视器可能具有负坐标。

        4
  •  4
  •   Josh    16 年前

    我在自动热键脚本中完成了这项任务,以防您不关心所使用的语言。它使用模拟的按键和鼠标移动来移动任务栏。我停止了自动解锁/锁定任务栏。

    最困难的部分是让它可靠地工作。许多代码专门用于确保任务栏移动。它仍然无法100%工作。。。从我所看到的情况来看,有10%的时间是失败的。然而,它应该足够好,让你开始!

    F3::
        reload
    return
    
    F5::
        MoveTaskbar(2,"bottom")
    return
    
    F6::
        MoveTaskbar(2,"left")
    return
    
    F7::
        MoveTaskbar(1,"top")
    return
    
    ; Move the taskbar
    ; dspNumber:    number.  device number (primary display is 1, secondary display is 2...)
    ; edge:         string.  Top, Right, Bottom, or Left
    MoveTaskbar(dspNumber, edge)
    {
        Critical 
        OutputDebug MoveTaskbar - called to move taskbar to display #%dspNumber% ("%edge%" edge)
    
        ; absolute coordinate system
        CoordMode, Mouse, Screen
    
        ; error checking for dspNumber
        SysGet, numMonitors, MonitorCount
        if (numMonitors<dspNumber)
        {
            OutputDebug MoveTaskbar - [ERROR] target monitor does not exist (dspNumber = "%dspNumber%")
            return
        }
    
        ; get screen position for target monitor
        SysGet, target, Monitor, %dspNumber%
    
        oX := 7
        oY := 7
    
        ; get coordinates for where to move the taskbar
        if (edge = "Top")
        {
            oX := (targetRight-targetLeft)/2
            trgX := oX+targetLeft
            trgY := targetTop+15
        }
        else if (edge = "Right")
        {
            oY := -(targetBottom-targetTop)/2
            trgX := targetRight-15
            trgY := -oY + targetTop
        }
        else if (edge = "Bottom")
        {
            oX := -(targetRight-targetLeft)/2
            trgX := -oX+targetLeft
            trgY := targetBottom-15
        }
        else if (edge = "Left")
        {
            oY := (targetBottom-targetTop)/2
            trgX := targetLeft+15
            trgY := oY+targetTop
        }
        else
        {
            OutputDebug MoveTaskbar - [ERROR] target edge was improperly specified (edge = "%edge%")
            return
        }
        trgX := round(trgX)
        trgY := round(trgY)
        oX := round(oX)
        oY := round(oY)
    
        OutputDebug MoveTaskbar - target location is (%trgX%,%trgY%)
        MouseGetPos, startX, startY
        OutputDebug MoveTaskbar - mouse is currently at (%startX%,%startY%)
    
        ; request the move mode (via context menu)
        SendInput #b
        SendInput !+{Space}
        SendInput m
    
        ; wait for the move mode to be ready
        Loop 
        {
            if A_Cursor = SizeAll
                break
        }
        OutputDebug MoveTaskbar - move mode is ready
    
        ; start the move mode
        SendInput {Right}   
    
        ; wait for the move mode to become active for mouse control
        Loop 
        {
            if A_Cursor = Arrow
                break
        }
        OutputDebug MoveTaskbar - move mode is active for mouse control
    
        ; move taskbar (and making sure it actually does move)
        offset := 7
        count := 0
        Loop
        {
            ; move the taskbar to the desired location
            OutputDebug MoveTaskbar - attempting to move mouse to (%trgX%,%trgY%)
            MouseMove, %trgX%, %trgY%, 0
            MouseGetPos, mX, mY, win_id
            WinGetClass, win_class, ahk_id %win_id%
    
            count += 1
    
            ; if the mouse didn't get where it was supposed to, try again
            If ((mX != trgX) or (mY != trgY))
            {
                OutputDebug MoveTaskbar - mouse didn't get to its destination (currently at (%mX%,%mY%)).  Trying the move again...
                continue
            }
    
            ; if the taskbar hasn't followed yet, wiggle the mouse!
            if (win_class != "Shell_TrayWnd")
            {
                OutputDebug MoveTaskbar - window with class "%win_class%" is under the mouse... wiggling the mouse until the taskbar gets over here
    
                ;offset := - offset
                trgX -= round(oX/2)
                trgY -= round(oY/2)
                oX := -oX
                oY := -oY
                if count = 50
                {
                    OutputDebug, MoveTaskbar - wiggling isn't working, so I'm giving up.
                    return
                }
            }
            else
                break
        }
    
        OutputDebug MoveTaskbar - taskbar successfully moved
        SendInput {Enter}
    }
    
        5
  •  2
  •   Étienne Bilodeau    6 年前

    谢谢你提出这个问题!

    现在是Windows10,我遇到了同样的问题,我制作了一个脚本,在两屏设置和只播放电影的电视之间切换。切换回2屏设置后,任务栏将回到右侧显示器上,就像您所经历的那样。

    修改注册表项

    这就是所说的关键: HKEY\ U当前\用户\软件\ Microsoft\Windows\CurrentVersion\Explorer\StuckRects3

    打开区域编辑器 当任务栏位于正确的位置和大小时 (Win+R,输入regedit),然后导航到上面的关键路径。您应该找到一个名为“Settings”的二进制值,如下所示:

    你的数字可能会有所不同,但这并不重要。只需单击文件>导航到该值后,在菜单中导出操作,然后使用文件->从顶部菜单导出选项,并保存.reg文件 在您的系统文件夹中(C:\Windows\System32) . 确保导出范围设置为“Selected Branch”,因此仅此值受影响。这将创建一个 注册表脚本

    . 要实现这一点,您只需 在noptepad中,如下所示:

    @echo off
    %windir%\system32\regedit.exe /s file.reg
    taskkill /f /im explorer.exe
    start explorer.exe
    end
    

    只需将第2行中的“file.reg”替换为先前创建的.reg脚本的完整文件名,然后将该文件另存为“.bat”。

    运行此脚本 作为管理员 将任务栏重置到它应该位于的位置。 .

    从任务调用此脚本 或 抄近路 这是设置为运行管理员,使它更容易!

        6
  •  0
  •   Anders    16 年前

    SHAppBarMessage(ABM_SETPOS,…)