Jump to content

Adding a Button to any window titlebar


Recommended Posts

Hi, I'm looking for a way to add a custom Button to any window titlebar (not only in my own gui), for example the Notepad window. I already used the forumsearch but didn't found anything.

~baldwin

I think you need to do subclassing with the help of dll.

Link to comment
Share on other sites

  • 3 months later...

Tricky (and bugie?) method:

#include <WindowsConstants.au3>

Opt("GUICloseOnESC", 0)
Opt("GUIOnEventMode", 1)
Opt("WinWaitDelay", 0)

Global $hButton_GUI, $nButton, $ContextMenu
Global $MenuItem1, $MenuItem2, $MenuItem3, $Exit_MenuItem
Global $aOld_ParentPos = -1, $hParent, $hOld_Parent

Global $sMain_Title = "[CLASS:Notepad]"

Run(@WindowsDir & "\Notepad.exe")
WinWait($sMain_Title)
$hParent = WinGetHandle($sMain_Title)

_ExternalButtonCreate_Proc()
AdlibEnable("_ExternalButtonFollow_Proc", 1)

While 1
    Sleep(100)
WEnd

Func _ExternalButtonCreate_Proc()
    $hButton_GUI = GUICreate("", 65, 18, -1, -1, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    $nButton = GUICtrlCreateButton("Button", 0, 0, 65, 18)
    GUICtrlSetOnEvent(-1, "_Button_Event")
    
    Local $DummyMenu = GUICtrlCreateDummy()
    $ContextMenu = GUICtrlCreateContextMenu($DummyMenu)
    $MenuItem1 = GUICtrlCreateMenuItem("Some Item1", $ContextMenu)
    $MenuItem2 = GUICtrlCreateMenuItem("Some Item2", $ContextMenu)
    $MenuItem3 = GUICtrlCreateMenuItem("Some Item3", $ContextMenu)
    GUICtrlCreateMenuItem("", $ContextMenu)
    $Exit_MenuItem = GUICtrlCreateMenuItem("Exit", $ContextMenu)
    GUICtrlSetOnEvent(-1, "_Exit")
    
    GUISetState(@SW_SHOW, $hButton_GUI)
EndFunc

Func _ExternalButtonFollow_Proc()
    If $hParent <> -1 And Not WinExists($hParent) Then
        GUISetState(@SW_HIDE, $hButton_GUI)
        $hParent = -1
    ElseIf $hParent = -1 And WinExists($sMain_Title) Then
        GUISetState(@SW_SHOW, $hButton_GUI)
        $hParent = WinGetHandle($sMain_Title)
    ElseIf $hParent = -1 Then
        Return
    EndIf
    
    $aParentPos = WinGetPos($hParent)
    $aParentClientSize = WinGetClientSize($hParent)
    
    If Not IsArray($aParentPos) Then Return
    
    If Not WinActive($hParent) And Not WinActive($hButton_GUI) Then
        $aParentPos[0] = -3200
        $aParentPos[1] = -3200
    EndIf
    
    If $aOld_ParentPos = -1 Or $aOld_ParentPos[0] <> $aParentPos[0] Or $aOld_ParentPos[1] <> $aParentPos[1] Or _
        $aOld_ParentPos[2] <> $aParentPos[2] Then
        
        $aOld_ParentPos = $aParentPos
        
        $iBorder_Size = ($aParentPos[2] - $aParentClientSize[0]) / 2
        
        $iX = ($aParentPos[0] + $aParentPos[2]) - 170
        $iY = $aParentPos[1] + $iBorder_Size
        
        If $aParentPos[2] < 170 Then $iX = $aParentPos[0] + $iBorder_Size
        
        WinMove($hButton_GUI, "", $iX, $iY)
    EndIf
EndFunc

Func _Button_Event()
    ConsoleWrite("Button clicked" & @CRLF)
    
    _GUICtrlMenu_Show($hButton_GUI, $ContextMenu, $nButton)
EndFunc

Func _GUICtrlMenu_Show($hWnd, $nContextID, $nContextControlID, $iMouse=0)
    Local $hMenu = GUICtrlGetHandle($nContextID)
    Local $iCtrlPos = ControlGetPos($hWnd, "", $nContextControlID)
    
    Local $iX = $iCtrlPos[0]
    Local $iY = $iCtrlPos[1] + $iCtrlPos[3]
    
    ; Convert the client (GUI) coordinates to screen (desktop) coordinates
    ;ClientToScreen
    Local $stPoint = DllStructCreate("int;int")
    
    DllStructSetData($stPoint, 1, $iX)
    DllStructSetData($stPoint, 2, $iY)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))
    
    $iX = DllStructGetData($stPoint, 1)
    $iY = DllStructGetData($stPoint, 2)
    ;ClientToScreen
    
    If $iMouse Then
        $iX = MouseGetPos(0)
        $iY = MouseGetPos(1)
    EndIf
    
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $iX, "int", $iY, "hwnd", $hWnd, "ptr", 0)
EndFunc

Func _Exit()
    Exit
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

This is a brutal way and the main window will lose focus.

Agree :) . But is there any other way?

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

WinTray

Only for take the Window hook?

AnyGui

And whats there? :)

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • Moderators

MrCreatoR,

Thank you for posting a way to do this - brutal it might be, but it works!. I managed to get over the "losing focus" problem by resetting the focus to the parent once the button action was terminated.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...