Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shortcut to make window available on all desktops #55

Closed
fanlushuai opened this issue May 6, 2020 · 14 comments
Closed

Add shortcut to make window available on all desktops #55

fanlushuai opened this issue May 6, 2020 · 14 comments

Comments

@fanlushuai
Copy link

This is very useful for me ,thanks author!

I use strokens plus work with this ahk script,which can make mouse move to control the virtual desktop switch. left right top button of mouse move to switch four virtual desktops. which is really wonderful.

but ,I have a new idea that pin a application on the top of any virtual desktop nomatter I switch it.
whether can make it ?

@Elijas
Copy link
Collaborator

Elijas commented May 6, 2020

Hi, glad you've found it useful!

Solution

Yes, I have added this functionality to my private version of the script, all you need is to add these lines and you'll be able to use Capslock + h for example, to pin the window on all desktops

global IsPinnedWindowProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "IsPinnedWindow", "Ptr")
global PinWindowProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "PinWindow", "Ptr")
global UnPinWindowProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "UnPinWindow", "Ptr")
global IsPinnedAppProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "IsPinnedApp", "Ptr")
global PinAppProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "PinApp", "Ptr")
global UnPinAppProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "UnPinApp", "Ptr")

capslock & g::OnTogglePinOnTopPress()
capslock & h::OnTogglePinAppPress()
capslock & j::OnTogglePinWindowPress()

_GetCurrentWindowID() {
    WinGet, activeHwnd, ID, A
    return activeHwnd
}

_GetCurrentWindowTitle() {
    WinGetTitle, activeHwnd, A
    return activeHwnd
}

OnTogglePinOnTopPress() {
        _notif(_TruncateString(_GetCurrentWindowTitle(), 100), "Toggled 'Pin On Top'")
	Winset, Alwaysontop, , A
}

OnTogglePinWindowPress() {
    windowID := _GetCurrentWindowID()
    windowTitle := _GetCurrentWindowTitle()
    if (_GetIsWindowPinned(windowID)) {
        _UnpinWindow(windowID)
        _ShowTooltipForUnpinnedWindow(windowTitle)
    }
    else {
        _PinWindow(windowID)
        _ShowTooltipForPinnedWindow(windowTitle)
    }
}

OnTogglePinAppPress() {
    windowID := _GetCurrentWindowID()
    windowTitle := _GetCurrentWindowTitle()
    if (_GetIsAppPinned(windowID)) {
        _UnpinApp(windowID)
        _ShowTooltipForUnpinnedApp(windowTitle)
    }
    else {
        _PinApp(windowID)
        _ShowTooltipForPinnedApp(windowTitle)
    }
}

_PinWindow(windowID:="") {
    _CallWindowProc(PinWindowProc, windowID)
}

_UnpinWindow(windowID:="") {
    _CallWindowProc(UnpinWindowProc, windowID)
}

_GetIsWindowPinned(windowID:="") {
    return _CallWindowProc(IsPinnedWindowProc, windowID)
}

_PinApp(windowID:="") {
    _CallWindowProc(PinAppProc, windowID)
}

_UnpinApp(windowID:="") {
    _CallWindowProc(UnpinAppProc, windowID)
}

_GetIsAppPinned(windowID:="") {
    return _CallWindowProc(IsPinnedAppProc, windowID)
}

_CallWindowProc(proc, window:="") {
    if (window == "") {
        window := _GetCurrentWindowID()
    }
    return DllCall(proc, UInt, window)
}

_notif(txt, title:="") {
    HideTrayTip()
    TrayTip, %title%, %txt%, 1
}

HideTrayTip() {
    TrayTip  ; Attempt to hide it the normal way.
    if SubStr(A_OSVersion,1,3) = "10." {
        Menu Tray, NoIcon
        Sleep 200  ; It may be necessary to adjust this sleep.
        Menu Tray, Icon
    }
}
_ShowTooltipForPinnedWindow(windowTitle) {
    _notif(_TruncateString(windowTitle, 100), "Pinned Window")
}

_ShowTooltipForUnpinnedWindow(windowTitle) {
    _notif(_TruncateString(windowTitle, 100), "Unpinned Window")
}

_ShowTooltipForPinnedApp(windowTitle) {
    _notif(_TruncateString(windowTitle, 100), "Pinned App")
}

_ShowTooltipForUnpinnedApp(windowTitle) {
    _notif(_TruncateString(windowTitle, 100), "Unpinned App")
}

_TruncateString(string:="", n:=10) {
    return (StrLen(string) > n ? SubStr(string, 1, n-3) . "..." : string)
}

@fanlushuai
Copy link
Author

Well done!

I have test it,but met the error:

Error: Call to nonexistent function
Specifically:_GetCurrentWindowID()
……
……

@Elijas
Copy link
Collaborator

Elijas commented May 6, 2020

Thanks!
I added the missing function to the previous comment.
I also removed this line windowTitle := _GetCurrentWindowTitle()

@fanlushuai
Copy link
Author

fanlushuai commented May 6, 2020

Error: Call to nonexistent function _CallWindowProc
Add this
_CallWindowProc(proc, window:="") {
if (window == "") {
window := _GetCurrentWindowID()
}
return DllCall(proc, UInt, window)
}

@fanlushuai
Copy link
Author

Yeach. Popup notification can make the status more clean. It is really a good idea !

Very happy for your sharing ,if you does not matter

@Elijas
Copy link
Collaborator

Elijas commented May 7, 2020

Added it:)

@fanlushuai
Copy link
Author

_GetCurrentWindowTitle() {
WinGetTitle, activeHwnd, A
return activeHwnd
}

@Elijas
Copy link
Collaborator

Elijas commented May 8, 2020

_GetCurrentWindowTitle() {
WinGetTitle, activeHwnd, A
return activeHwnd
}

You're right)

Does everything work now ?

@Elijas Elijas changed the title suggestion Add shortcut to make window available on all desktops May 8, 2020
@fanlushuai
Copy link
Author

Yes. everything goes well. Thanks again.

@Elijas Elijas closed this as completed May 8, 2020
@fanlushuai
Copy link
Author

Use proces name as pop notification for pin or unpin seem more clear than window tile.

_GetCurrentWindowProcess() {
WinGet, ProcessName, ProcessName, A
; MsgBox, %ProcessName%
return ProcessName
}

@fanlushuai
Copy link
Author

Except for pop notification for pin or unpin operation, switch backgroud with desktop can make switch more clear .

AutoAssociateBackgroundWithDesktop := false
BackgroundPicPaths := ["C:\Users\A\Pictures\VDPic\day.jpg", "C:\Users\A\Pictures\VDPic\snow.jpg", "C:\Users\A\Pictures\VDPic\sex.jpg", "C:\Users\A\Pictures\VDPic\cool.jpg"]


……

changeBackgroundWithDesktopId()
{
    global CurrentDesktop, BackgroundPicPaths, AutoAssociateBackgroundWithDesktop
    filePath := BackgroundPicPaths[CurrentDesktop]
    if (AutoAssociateBackgroundWithDesktop and filePath and FileExist(filePath)) {
        DllCall("SystemParametersInfo", UInt, 0x14, UInt, 0, Str, filePath, UInt, 1)
   }
}

@marium0505
Copy link

marium0505 commented Jun 8, 2021

@Elijas
This is awesome, but I some of them doesn't seem to work.
The OnTogglePinOnTopPress function works fine, but neitherOnTogglePinAppPress nor OnTogglePinWindowPress seem to work for me.

Does anyone else have issues with the two functions as well? And does someone know a resolution? I would really love to be able to use those two lasts functions.

@javjim
Copy link

javjim commented Jul 8, 2021

@Elijas
This is awesome, but I some of them doesn't seem to work.
The OnTogglePinOnTopPress function works fine, but neitherOnTogglePinAppPress nor OnTogglePinWindowPress seem to work for me.

Does anyone else have issues with the two functions as well? And does someone know a resolution? I would really love to be able to use those two lasts functions.

Agree with you, only togglePin works for me, the other 2 don't work, and would love the functionality...

@ib4
Copy link

ib4 commented May 28, 2022

@Elijas This is awesome, but I some of them doesn't seem to work. The OnTogglePinOnTopPress function works fine, but neitherOnTogglePinAppPress nor OnTogglePinWindowPress seem to work for me.

Does anyone else have issues with the two functions as well? And does someone know a resolution? I would really love to be able to use those two lasts functions.

I know it's an old issue, but I got it to work when I moved the six "global" statements up to the "DLL" section where the other "global" statements are.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants