Sign in to follow this  
Followers 0
The_Mega_ZZTer

Manipulating Menus in Windows Forms

2 posts in this topic

#1 ·  Posted (edited)

Hey, I'm trying to use AutoIt to add tabbed functionality to Notepad. My attachment has what I've got so far... "dumb" tabs. It requires the latest AutoIt beta.

To use the script I've attached to see what I've done so far, run it and it will launch notepad and add it's tabbar. It's designed for and was tested on Windows XP Notepad but hopefully it should work on earlier versions too. :)

Of course, Notepad isn't "tab aware" so I have to write my own load/save functions in AutoIt, and then the problem is how to supress Notepad's load/save functions and use my own.

What I want to do is disable/hide Notepad's menus, in whole or in part, and replace it with my own menus which I can then catch events for. If this proves to be impossible I'll probably have to settle for making a toolbar with open/save buttons.

Anyone have any ideas?

notepadtabs.au3

Edited by The_Mega_ZZTer

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#include <Array.au3>
#include <ANYGUI.au3>
#include <guiconstants.au3>
#Include <GuiTab.au3>

Opt ("WinTitleMatchMode", 3)
Opt ("WinWaitDelay", 0)
Opt ("GUIOnEventMode", 1)

Const $MF_BYCOMMAND = 0x0
Const $MF_BYPOSITION = 0x400
Const $MF_REMOVE = 0x1000

$pid = Run("notepad.exe")
WinWait("Untitled - Notepad")
WinWaitActive("Untitled - Notepad")
$hWnd = _GuiTarget ("Untitled - Notepad", 1)

; *************************
; _RemoveSystemMenus($hWnd)
_RemoveMenus($hWnd)
; *************************

$child = _TargetaddChild ("", 0, 0, 5000, 20)
$newtab = GUICtrlCreateButton("+", 0, 0, 20, 20, $BS_ICON + $BS_FLAT)
$closetab = GUICtrlCreateButton("X", 25, 0, 20, 20, $BS_ICON + $BS_FLAT)
$tab = GUICtrlCreateTab(50, 0, 5000, 20)
$tabs = _ArrayCreate (GUICtrlCreateTabItem("Untitled"))
GUICtrlCreateTabItem("")

GUICtrlSetImage($newtab, "shell32.dll", 225, 0)
GUICtrlSetImage($closetab, "shell32.dll", 131, 0)

GUICtrlSetOnEvent($newtab, "onNewTab")
GUICtrlSetOnEvent($closetab, "onCloseTab")

GUISetState(@SW_SHOW)
While WinExists($hWnd)
    $size = WinGetClientSize($hWnd)
    If WinExists($hWnd) Then
        ControlMove($hWnd, "", 15, 0, 20, $size[0], $size[1] - 43)
        ControlMove($hWnd, "", "SysTabControl321", 25, 0, $size[0] - 50, 20)
        ControlMove($hWnd, "", "Button2", $size[0] - 20, 0, 20, 20)
    EndIf
    
    Sleep(100)
WEnd

Func onNewTab()
    _ArrayAdd($tabs, GUICtrlCreateTabItem("Untitled"))
    GUICtrlCreateTabItem("")
EndFunc  ;==>onNewTab

Func onCloseTab()
    $curr = _GUICtrlTabGetCurSel ($tab)
    $num = _GUICtrlTabGetItemCount ($tab)
    
    If $curr < $num - 1 Then
        _GUICtrlTabDeleteItem ($tab, $curr)
        _ArrayDelete($tabs, $curr)
        _GUICtrlTabSetCurSel ($tab, $curr)
    ElseIf $num > 1 Then
        _GUICtrlTabDeleteItem ($tab, $curr)
        _ArrayDelete($tabs, $curr)
        _GUICtrlTabSetCurSel ($tab, $num - 2)
    Else
        _ArrayAdd($tabs, GUICtrlCreateTabItem("Untitled"))
        GUICtrlCreateTabItem("")
        _GUICtrlTabSetCurSel ($tab, 1)
        _GUICtrlTabDeleteItem ($tab, 0)
        _ArrayDelete($tabs, 0)
    EndIf
EndFunc  ;==>onCloseTab


Func _RemoveMenus($hWnd)
    $ret = DllCall("user32.dll", "int", "GetMenu", "hwnd", $hWnd)
    $h_menu = $ret[0]
    $ret = DllCall("user32.dll", "int", "GetMenuItemCount", "int", $h_menu)
    $m_count = $ret[0]
    For $x = $m_count - 1 To 0 Step - 1
        $ret = DllCall("user32.dll", "int", "DeleteMenu", "int", $h_menu, "int", $x, "int", $MF_BYPOSITION)
    Next
    DllCall("user32.dll", "int", "DrawMenuBar", "hwnd", $hWnd)
EndFunc  ;==>_RemoveMenus

Func _RemoveSystemMenus($hWnd)
    $ret = DllCall("user32.dll", "int", "GetSystemMenu", "hwnd", $hWnd, "int", 0)
    $h_menu = $ret[0]
    $ret = DllCall("user32.dll", "int", "GetMenuItemCount", "int", $h_menu)
    $m_count = $ret[0]
    For $x = $m_count - 1 To 0 Step - 1
        $ret = DllCall("user32.dll", "int", "DeleteMenu", "int", $h_menu, "int", $x, "int", $MF_BYPOSITION)
    Next
EndFunc  ;==>_RemoveSystemMenus

Edit: added the DrawMenuBar so the menu items no longer show after being deleted

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0