AutoHotkey: Dynamic resize control based on text - controls

Consider the following snippet:
FormatTime, time,
Gui, Add, Text, vcTime,
GuiControl, , cTime, % time
Gui, Show, NoActivate Center AutoSize
AutoSize is based on the initial value from Gui, Add, Text, vcTime and not on the new value set by GuiControl, , cTime, % time. Depending on months etc., %time% length can vary. How can I automatically resize the window to adapt to updated values of %time%?
`

AutoSize is actually based on the current control sizes when Gui Show is called. The issue is that the "blank" subcommand to GuiControl for text controls does not automatically resize the control; it just changes the text and you still have to call GuiControl Move with a new size yourself. So, in your example, if you replace AutoSize with w200 the text will still be cut off at the same point.
As far as I know, there's not really a "built-in" automatic way to resize the text control based on the new text. The closest way is to use AHK's initial size calculation when creating a text control: create a new text control with the desired text, use GuiControlGet to get the size of the new control, and finally set the size of the original control to that size using GuiControl Move. Here's an example function which does this, adapted from here:
SetTextAndResize(controlHwnd, newText, fontOptions := "", fontName := "") {
Gui 9:Font, %fontOptions%, %fontName%
Gui 9:Add, Text, R1, %newText%
GuiControlGet T, 9:Pos, Static1
Gui 9:Destroy
GuiControl,, %controlHwnd%, %newText%
GuiControl Move, %controlHwnd%, % "h" TH " w" TW
}
Which would fit into your example like so:
FormatTime, time,
Gui, Add, Text, HwndTimeHwnd vcTime,
SetTextAndResize(TimeHwnd, time)
Gui, Show, NoActivate Center AutoSize
Now whenever you use SetTextAndResize instead of just setting the text you can use Gui Show, AutoSize to automatically resize the window correctly. Note that if you changed the font with Gui Font before adding the text control you would have to pass those same options to SetTextAndResize.
Alternatively, I looked at how AHK itself calculates the initial size of a text control for Gui Add, Text when none is provided and found it uses the Windows API function DrawText with DT_CALCRECT. Here's another implementation of SetTextAndResize I wrote using this directly:
SetTextAndResize(controlHwnd, newText) {
dc := DllCall("GetDC", "Ptr", controlHwnd)
; 0x31 = WM_GETFONT
SendMessage 0x31,,,, ahk_id %controlHwnd%
hFont := ErrorLevel
oldFont := 0
if (hFont != "FAIL")
oldFont := DllCall("SelectObject", "Ptr", dc, "Ptr", hFont)
VarSetCapacity(rect, 16, 0)
; 0x440 = DT_CALCRECT | DT_EXPANDTABS
h := DllCall("DrawText", "Ptr", dc, "Ptr", &newText, "Int", -1, "Ptr", &rect, "UInt", 0x440)
; width = rect.right - rect.left
w := NumGet(rect, 8, "Int") - NumGet(rect, 0, "Int")
if oldFont
DllCall("SelectObject", "Ptr", dc, "Ptr", oldFont)
DllCall("ReleaseDC", "Ptr", controlHwnd, "Ptr", dc)
GuiControl,, %controlHwnd%, %newText%
GuiControl Move, %controlHwnd%, % "h" h " w" w
}
I'm not sure how it compares to the first method in terms of performance, but one advantage is that it gets the font based on the control itself rather than having to provide it to the function yourself.

Related

Win32 WM_CTLCOLORSTATIC background not completely filled

I am working on a dialog box that is created and controlled by a host program. The host creates the window and then sends me all the messages, but this means I don't have full access to everything it's doing. (I mention this because it could be contributing to my issue.)
I would like to change the color of an LTEXT to red. I am handling the WM_CTLCOLORSTATIC message and it is working where the text is drawn. The problem I'm having is that the rectangle for the LTEXT is slightly wider than the length of the text. For the part of the control that does not contain text it is leaving the background white instead of COLOR_BTNFACE as I have specified.
Here is the code for my handler. I call it by way of HANDLE_WM_CTLCOLORSTATIC.
// color message handler
HBRUSH OnControlColor ( HWND hDlg, twobyte dlgItem, HDC hdcCtrl, int type ) override
{
if ( (dlgItem == ID_MANUAL_EDIT_WARNING) && (type == CTLCOLOR_STATIC) )
{
SetTextColor(hdcCtrl, RGB(204, 0, 0));
SetBkColor(hdcCtrl, GetSysColor(COLOR_BTNFACE));
return (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
}
return NO;
}
It seems like maybe I need to invalidate the entire client rect some way, but I am not sure how to do this. Obviously I could carefully make the rectangle the exact right size in the dialog designer, but this doesn't seem like the safest approach.
What you could do is to explicitly select your desired background brush into the control's device context; thus, when its rectangle is drawn, that brush will be used. You can do it with one additional line of code in your handler:
HBRUSH OnControlColor ( HWND hDlg, twobyte dlgItem, HDC hdcCtrl, int type ) override
{
if ( (dlgItem == ID_MANUAL_EDIT_WARNING) && (type == CTLCOLOR_STATIC) )
{
SetTextColor(hdcCtrl, RGB(204, 0, 0));
SetBkColor(hdcCtrl, GetSysColor(COLOR_BTNFACE));
SelectObject(hdcCtrl, GetSysColorBrush(COLOR_BTNFACE)); // Select the B/G brush
return (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
}
return NO;
}
My one concern here is that you're manipulating object selection in a device context that is 'owned' by another process; this could cause issues if the replaced object in that DC is user-created (but that doesn't seem to be so in this case).

In WinAPI, how to make a stretchable OpenGL window with correct mouse, trapped properly for games?

I have read a lot of Stack Overflow over the years when struggling with making sense of Microsoft Windows' strange world of CreateWindowEx() .. etc. This question, when originally asked was "What is the best way to create a fluidly resizable OpenGL window in WinAPI?"
I've been struggling with getting WinAPI to make a window that:
Has an OpenGL context
Is properly centered on the main monitor (or any monitor determined by command line signal) in both multi-monitor and single-monitor displays when in "Windowed" mode or in "Fullscreen" mode
Has a fixed internal client screen size (viewport 2d)
Doesn't allow you to click outside causing it to lose focus at the wrong times or in special cases for multi-monitor
Can be resized fluidly, but doesn't change internal "client size" (meaning that it stretches the OpenGL content which is a fixed size to the new screen size) ... the idea here is to add a layer of virtualization, so that all pixels are expressed in the same 1920x1080 (1080p) coordinate system. This part is no problem for me.
Correctly handles mouse event translation from screen_size -> client_size equivalent via the screen->client ratio
In my homegrown App framework, I have to set the display size, and even then, Windows doesn't give me the right sized window. (Sometimes the title bar is subtracted, sometimes the scrollbars are subtracted, but the context draws under the title bar, for example.)
Also, recently when moving from 2010 EE (Win32 / Windows 7) to 2015 (win32 / Windows 10), I had to change the parameters to recenter the view because it was off-centered on the main display. Now, only sometimes are these values correct or incorrect. If I go "fullscreen" for example, the same values will draw above the top of the screen such that there is an area at the bottom of the screen that shows the "gl clear color" (in my case, orange)
I can play with these things by providing the following command line parameters:
-bordered (default, and has no effect really, is the default windowed mode with the title bar and such)
-borderless (seems to go into fullscreen mode, with the app off-center where win 0,0 is actually in screen center)
-windowed (or -window)
If I don't provide -window, it defaults to "full screen" resolution-adjusted (but only if supported I assume, otherwise it might throw an error).
Anyway, all of this is very bad because
a) I have to write a bajillion cases for each resolution I'm working in, rather than write everything for 1080p and have it adjust to display size, which is what i want because it handles most new displays on laptops and desktops (this is Windows remember) (and only slightly squishes things in those corner cases)
b) I cannot resize the window fluidly, also i have to trap the mouse at center and recalculate the mouse position, so I record only the deltas -- this is to avoid the mouse leaving the window and clicking the desktop, or floating off the monitor to some other monitor, even when it is hidden. I also have to make the mouse cursor invisible so the user doesn't see this, then show a simulated mouse cursor.
c) Users who don't support specifically 1920x1080 won't be able to use full screen mode
Someone pointed this article out in another question (window border width and height in Win32 - how do I get it?):
https://web.archive.org/web/20120716062211/http://suite101.com/article/client-area-size-with-movewindow-a17846
And I've read through this, learning that AdjustWindowRectEx() has some issues:
AdjustWindowRectEx() and GetWindowRect() give wrong size with WS_OVERLAPPED
I don't use WS_OVERLAPPED, so this was only moderately helpful:
AdjustWindowRectEx() and GetWindowRect() give wrong size with WS_OVERLAPPED
Here's how I do it now:
display.Resized(display.w,display.h);
// Fill in the window class structure for testing display type.
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WinProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
// Save the game instance handle
display.hinstance = game_instance = hinstance;
// Register the window class
if (!RegisterClassEx(&winclass)) return(0);
if (!gl.Init(hinstance, display.bits)) {
return(0);
}
// Detect the display size and create the final display profile
DWORD winStyle=
WS_EX_APPWINDOW |
WS_EX_TOPMOST /*|
WS_EX_ACCEPTFILES*/ ;
// Adjust Window, Account For Window Borders
int xPos = GetSystemMetrics(SM_CXSCREEN) - display.w;
int yPos = GetSystemMetrics(SM_CYSCREEN) - display.h;
RECT windowRect = {0, 0, display.w, display.h}; // Define Our Window Coordinates
AdjustWindowRectEx (&windowRect, WS_POPUP, 0, winStyle );
// Create the window
if (!(hwnd = CreateWindowEx(
winStyle, // extended style
WINDOW_CLASS_NAME, // class
gl.winTitle.c_str(), // title
( gl.borderless || CmdLine.Option("-borderless") ) ? (WS_POPUPWINDOW | WS_VISIBLE)
: (gl.noFullscreen ? ((CmdLine.Option("-bordered") ? WS_BORDER : 0) | WS_VISIBLE)
: (WS_POPUP | WS_VISIBLE)), // use POPUP for full screen
gl.noFullscreen && !CmdLine.Option("-recenter") ? xPos / 2 : 0,
gl.noFullscreen && !CmdLine.Option("-recenter") ? yPos / 2 : 0, // initial game window x,y
display.w, // initial game width
display.h, // initial game height
HWND_DESKTOP, // handle to parent
NULL, // handle to menu
hinstance, // instance of this application
NULL)
) // extra creation parms
) {
OUTPUT("WinAPI ERROR: Could not open window.\n");
return(0);
}
if (gl.borderless || CmdLine.Option("-borderless") ) {
LONG lStyle = GetWindowLong(hwnd, GWL_STYLE);
lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
SetWindowLong(hwnd, GWL_STYLE, lStyle);
LONG lExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
lExStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
SetWindowLong(hwnd, GWL_EXSTYLE, lExStyle);
SetWindowPos(hwnd, NULL, 0, 0, display.w, display.h, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
}
// Temporary change to full screen mode
ZeroMemory(&game_screen, sizeof(game_screen)); // clear out size of DEVMODE struct
game_screen.dmSize = sizeof(game_screen);
game_screen.dmPelsWidth = display.w;
game_screen.dmPelsHeight = display.h;
game_screen.dmBitsPerPel = display.bits;
game_screen.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
ChangeDisplaySettings(&game_screen, CDS_FULLSCREEN);
// save the game window handle
display.hwnd = game_window = hwnd;
display.hdc = game_dc = GetDC(display.hwnd = game_window); // get the GDI device context
// set up the pixel format desc struct
pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of this PFD
1, // version number
PFD_DRAW_TO_WINDOW | // supports window
PFD_SUPPORT_OPENGL | // supports OpenGL
PFD_DOUBLEBUFFER, // support double buff
PFD_TYPE_RGBA, // request RGBA format
(BYTE)display.bits, // select color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buff
0, // shift bit ignored
0, // no accum buff
0, 0, 0, 0, // accum bits ignored
16, // 16-bit Z-buff (depth buff)
0, // no stencil buff
0, // no aux buff
PFD_MAIN_PLANE, // main drawing layer
0, // reserved
0, 0, 0 // layer masks ignored
};
int pf; // pixel format
if (!gl.arbMultisampleSupported) {
if (!(pf = ChoosePixelFormat(game_dc, &pfd))) // match the pixel format
{
MessageBox(game_window, "OpenGL could not be initialized -- ChoosePixelFormat Error ; report this to program authors for help!", "OpenGL Error", MB_OK);
return FALSE; // error returned
}
} else {
pf = gl.arbMultisampleFormat;
}
if (!SetPixelFormat(game_dc, pf, &pfd)) // set the pixel format
{
MessageBox(game_window, "OpenGL could not be initialized -- SetPixelFormat Error ; report this to program authors for help!", "OpenGL Error", MB_OK);
return FALSE; // error returned
}
if (!(game_rc = wglCreateContext(game_dc))) // create the rendering context
{
MessageBox(game_window, "OpenGL could not be initialized -- CreateContext Error ; report this to program authors for help!", "OpenGL Error", MB_OK);
return FALSE; // error returned
}
if (!(upload_rc = wglCreateContext(game_dc))) // create the rendering context
{
MessageBox(game_window, "Multiple OpenGL contexts could not be initialized -- CreateContext Error ; report this to program authors for help!", "OpenGL Error", MB_OK);
return FALSE; // error returned
} else { // Share as much as you can between two contexts
if (!wglShareLists(game_rc, upload_rc)) {
// could use GetLastError here
MessageBox(game_window, "wglShareLists -- Error ; report this to program authors for help!", "OpenGL Error", MB_OK);
return FALSE; // error returned
}
}
if (!wglMakeCurrent(game_dc, display.hglrc = game_rc)) // make it current
{
MessageBox(game_window, "OpenGL could not be initialized -- MakeCurrent Error ; report this to program authors for help!", "OpenGL Error", MB_OK);
return FALSE; // error returned
}
ShowCursor(false);
ShowWindow(game_window, SW_SHOWNORMAL);
SetForegroundWindow(game_window);
In the above code, what I get is a window that has no resize functionality, hides the OS mouse cursor, and can only be exitted with ALT-TAB (or ALT-F4), and when it is exitted appears at the back of the windows Z-order. I always open my window using a parameter that sets display.w to 1920 and display.h to 1080, either in full screen or in Windowed mode. WM_SIZE is then called to adjust it to the client area.
Please note that the following WM_SIZE is called during the WinProc right after the initial time I set display.Resized(w,h):
case WM_SIZE:
{
display.Resized(LOWORD(lparam), HIWORD(lparam));
return (0);
}
break;
This is executed exactly once during app load, and in the first case it looks like the values are: 1918,1078
UPDATE: If I use the result of GetWindowRect() here, or GetClientRect() as shown below, the window mysteriously moves to Center-X,Center-Y of screen! What gives??
// RECT rect;
// if ( GetClientRect(hwnd,&rect) ) {
// display.Resized((int)rect.right,(int)rect.bottom);
// }
//if ( GetWindowRect( hwnd, &rect ) ) {
// display.Resized((int)ADIFF(rect.left,rect.right),(int)ADIFF(rect.top,rect.bottom));
//}
display.Resized(LOWORD(lparam), HIWORD(lparam));
return (0);
What steps do I need to take to make the window stretchable such that the context is resized to the view, and the mouse is properly adjusted based on the screen ratio?
Basically, there are too many edge cases to make sense of all of this. As time has gone on since the 2 years ago that I asked this question, I've had other inconsistencies between full screen and window emerge.
From what I understand there are basically 3 types of windows:
Your normal on-screen moveable/resizable window for windowing GUIs, like this browser window (if you are not on mobile)
One matched to a display's resolution support (including resolutions smaller than its native) -- we call this "Full screen" (or Fullscreen, which isn't even a word)
One that is a normal on-screen window, but lacks a title bar, borders and scroll bars, and appears as large as the screen. Referred to "on the street" as a "Borderless Window"
I want to master all of these but in a way that makes them all accessible and doesn't require special cases. I've basically given up on doing so with WinAPI, but obviously multiple companies do this. Following Microsoft's documentation isn't very helpful, and I've experimented with a lot of different CreateWindow CreateWindowEx -- many of these features are deprecated by the way, or don't work at all.
(Maybe the best question is, WHEN WILL MICROSOFT CLEAN UP THIS CRAP? But I think we all know the answer.) .. any help to get it working would be appreciated.
I'm now working in: C++, Windows API, OpenGL 3.x / 4.x, Windows 10.

Show Windows 10 native clock widget programmatically

So I use a clock replacement program. The problem is it also hijack clicks on the clock. So whenever I click on the clock in the notification area, program's popup menu launch rather than the default windows clock widget.
I also tried AHK ControlClick on TrayClockWClass. I still didn't get original widget. Is there any way to launch the original widget programmatically? I use Windows 10 1607.
I am fine with RunDll, API, SendMessage or any other way whatsoever.
Somebody upvoted my question today. So maybe they are looking for the answer. I posted it on autohotkey.com forums back then, and I get the answer there.
https://autohotkey.com/boards/viewtopic.php?t=21274
ControlGet, hClock, Hwnd,, TrayClockWClass1, ahk_class Shell_TrayWnd ; https://autohotkey.com/board/topic/70770-win7-taskbar-clock-toggle/
if (hClock) {
VarSetCapacity(IID_IAccessible, 16), DllCall("ole32\CLSIDFromString", "WStr", "{618736e0-3c3d-11cf-810c-00aa00389b71}", "Ptr", &IID_IAccessible)
if (DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hClock, "UInt", OBJID_CLIENT := 0xFFFFFFFC, "Ptr", &IID_IAccessible, "Ptr*", accTrayClock))
return
VarSetCapacity(variant, A_PtrSize == 8 ? 24 : 16, 0), NumPut(VT_I4 := 3, variant,, "UShort")
if (A_PtrSize == 4) ; https://autohotkey.com/boards/viewtopic.php?p=111355#p111355
DllCall(NumGet(NumGet(accTrayClock+0)+25*A_PtrSize), "Ptr", accTrayClock, "Int64", NumGet(variant, 0, "Int64"), Int64, NumGet(variant, 8, "Int64")) ; IAccessible::DoDefaultAction
else
DllCall(NumGet(NumGet(accTrayClock+0)+25*A_PtrSize), "Ptr", accTrayClock, "Ptr", &variant) ; IAccessible::DoDefaultAction
ObjRelease(accTrayClock)
}

Can I add a custom paste option to the windows text editing context menu?

I am looking for a way to add an option(s) to the right click context menu when editing text on a WinXP machine. I already do a lot of copy/pasting on it, so the clipboard is always changing, but there is one string I type repeatedly in almost every file I edit.
I've already added some custom option to the context menu for .zip files to batch unzip them, but I'm not having any luck finding a way to add this.
The machine is used for a single purpose and I try to keep it as stable as possible so I'm trying to stay away from any new third-party software that could bloat the system.
I don't think there's an extension point for that sort of thing. You'd have to inject code into every process that has a window with a text box control, which would be complicated and frowned upon by most anti-virus applications.
I know you said you wanted to avoid third-party software, but there really isn't any way around it. A program like AutoIt will allow you to create a custom keyboard shortcut to paste whatever text you like into almost any application. It would probably be much more stable than any custom program written in the short term.
You can even compile the automation script to a standalone executable if you don't want to install the entire AutoIt distribution on the machine.
Assuming you are referring to the Edit control context menu.
You can achieve this by cloning and amending the Edit control context menu, via AutoHotkey. If the context menu is a for a different type of control, the same principle applies but it may be harder to recreate the existing menu item functions.
To 'add' a menu item, the simplest method would be to replace the entire menu with your own custom context menu. With your custom menu item at the top of it, and you would probably want to recreate the Undo/Cut/Copy/Paste/Delete/Select All items that appear on the Edit control. Using ControlGet, vText, Selected to recreate the Copy function for example. You use #IfWinActive to make
the menus only appear if a certain window is the active window, e.g. only if Notepad is the active window.
You would also need to capture right-clicks via the RButton hotkey and/or capture AppsKey presses,
and use ControlGetFocus to check if an Edit control was in focus, and MouseGetPos to check if an Edit control was under the cursor. So there would be a bit of work involved. Regarding capturing right-clicks, see the link below, where you would replace LButton with RButton. Good luck!
Is it possible to catch the close button and minimize the window instead? AutoHotKey
Similar question:
Can I edit the context menu of a text field (not Explorer context menu)?
Note:
- For typing long/repetitive strings, the use of hotstrings in AutoHotkey can really facilitate this. Achievable in literally one line of code.
- For batch jobs involving zip files perhaps try 7-Zip and using command lines parameters in AutoHotkey. This could probably be achieved in around 10 or 20 lines of code.
AutoHotkey is very lightweight, about 1MB, you could try it for a day or two, possibly watch a short 'hello world' tutorial video, it can be quite easy to get started.
The question asks how to edit the context menu for an Edit control,
it it slightly unclear whether this is wanted for renaming or editing files,
the AutoHotkey script below replicates
the Edit control menu when editing files in Explorer and using Notepad.
It adds a button that sends a string to the Edit control.
The script shows a custom context menu,
when an Edit control is right-clicked,
or when an Edit control is focused and the AppsKey is pressed.
Note: The script below is tested on Windows 7,
but the methods should work on Windows XP.
Note: The Explorer address bar also uses an Edit control,
however, this is taken into account by the script.
Note: You requested a method that is lightweight,
AutoHotkey can be run with one exe file (under 2MB in size),
and one script file. Scripts can also be compiled to small exes.
;AutoHotkey script for:
;contextmenu - Can I add a custom paste option to the windows text editing context menu? - Stack Overflow
;http://stackoverflow.com/questions/17370415/can-i-add-a-custom-paste-option-to-the-windows-text-editing-context-menu/41343891#41343891
;see also:
;windows - Can I edit the context menu of a text field (not Explorer context menu)? - Stack Overflow
;http://stackoverflow.com/questions/39827324/can-i-edit-the-context-menu-of-a-text-field-not-explorer-context-menu/41343741#41343741
;tested on Windows 7
GroupAdd, WinGroupFolder, ahk_class CabinetWClass ;explorer
#IfWinActive, ahk_group WinGroupFolder
$RButton Up:: ;explorer - custom Edit control menu
$AppsKey:: ;explorer - custom Edit control menu
#IfWinActive, ahk_class Notepad
$RButton Up:: ;notepad - custom Edit control menu
$AppsKey:: ;notepad - custom Edit control menu
;STAGE - create menu if not already created
if !vIsReady
{
Menu, EditMenu, Add, &My Item, MyItem
Menu, EditMenu, Add ;------------------------------
Menu, EditMenu, Add, &Undo, EditUndo
Menu, EditMenu, Add ;------------------------------
Menu, EditMenu, Add, Cu&t, EditCut
Menu, EditMenu, Add, &Copy, EditCopy
Menu, EditMenu, Add, &Paste, EditPaste
Menu, EditMenu, Add, &Delete, EditDelete
Menu, EditMenu, Add ;------------------------------
Menu, EditMenu, Add, Select &All, EditSelectAll
VarSetCapacity(vPos1, 4), VarSetCapacity(vPos2, 4)
VarSetCapacity(vPos1X, 4), VarSetCapacity(vPos2X, 4)
vIsReady := 1
}
;STAGE - perform certain checks, if any of them fail
;then let hotkeys perform their normal function,
;start by stating that, so far, the checks have not failed
vRet := 1
;check - if active control is an Edit/RichEdit control
if vRet
{
WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, ahk_id %hWnd%
ControlGet, hCtl, Hwnd, , %vCtlClassNN%, ahk_id %hWnd%
WinGetClass, vWinClass, ahk_id %hCtl%
if !(SubStr(vWinClass, 1, 4) = "Edit") && !(SubStr(vWinClass, 1, 8) = RichEdit)
vRet := 0
}
;check - if a right-click was performed, the control
;under the cursor must be the active control
if vRet && InStr(A_ThisHotkey, "RButton")
{
CoordMode, Mouse, Screen
MouseGetPos, vPosX, vPosY, , hCtl2, 3
if !(hCtl2 = hCtl)
vRet := 0
}
;check - the Edit control must be for a file icon and not the address bar
if vRet
{
;hWndParent := DllCall("user32\GetParent", Ptr,hCtl, Ptr)
hWndParent := DllCall("user32\GetAncestor", Ptr,hCtl, UInt,1, Ptr) ;GA_PARENT := 1
WinGetClass, vWinClassParent, ahk_id %hWndParent%
if (vWinClassParent = "ComboBox")
vRet := 0
}
;if a check has failed, then let hotkeys perform their normal function
if !vRet
{
if InStr(A_ThisHotkey, "RButton")
SendInput {Click right}
if InStr(A_ThisHotkey, "AppsKey")
SendInput {AppsKey}
Return
}
;STAGE - if clicked Edit control, menu will appear
;relative to cursor coordinates retrieved earlier,
;if pressed AppsKey, menu will appear in centre of Edit control
if !InStr(A_ThisHotkey, "RButton")
{
WinGetPos, vPosX, vPosY, vPosW, vPosH, ahk_id %hCtl%
vPosX += vPosW/2, vPosY += vPosH/2
}
;STAGE - retrieve information from Edit control
;and disable menu items accordingly
;Undo - check undo status (is undo available)
;Cut - check text selection > 0
;Copy - check text selection > 0
;Paste - check clipboard not empty
;Delete - check text selection > 0
;Select All - always available
SendMessage, 0xC6, 0, 0, , ahk_id %hCtl% ;EM_CANUNDO := 0xC6
vOptU := ErrorLevel ? "En" : "Dis" ;1=undo available/0=undo not available
ControlGet, vText, Selected, , , ahk_id %hCtl%
vOptT := StrLen(vText) ? "En" : "Dis"
vOptC := StrLen(Clipboard) ? "En" : "Dis"
Menu, EditMenu, % vOptU "able", &Undo, EditUndo
Menu, EditMenu, % vOptT "able", Cu&t, EditCut
Menu, EditMenu, % vOptT "able", &Copy, EditCopy
Menu, EditMenu, % vOptC "able", &Paste, EditPaste
Menu, EditMenu, % vOptT "able", &Delete, EditDelete
;STAGE - get Edit control character positions
;(unfortunately showing the custom menu ends the rename mode,
;we get the Edit control character positions in order to restore them later)
SendMessage, 0xB0, &vPos1, &vPos2, , ahk_id %hCtl% ;EM_GETSEL := 0xB0
vPos1 := NumGet(vPos1), vPos2 := NumGet(vPos2)
;STAGE - show menu
CoordMode, Menu, Screen
Menu, EditMenu, Show, %vPosX%, %vPosY%
Return
;==============================
;STAGE - replicate standard Edit control menu items
;(or perform custom menu function)
;(unfortunately showing the custom menu ends the rename mode,
;so the Edit control has to be put into rename again,
;and the character positions restored)
EditUndo:
EditCut:
EditCopy:
EditPaste:
EditDelete:
EditSelectAll:
MyItem:
;STAGE - enter rename mode again
IfWinActive, ahk_group WinGroupFolder
{
SendInput {F2}
Loop, 20
{
ControlGetFocus, vCtlClassNN, ahk_id %hWnd%
if (SubStr(vCtlClassNN, 1, 4) = "Edit")
break
Sleep 50
}
if !(SubStr(vCtlClassNN, 1, 4) = "Edit")
{
MsgBox % "error"
Return
}
ControlGet, hCtl, Hwnd, , % vCtlClassNN, ahk_id %hWnd%
;STAGE - restore character positions
if !InStr(A_ThisLabel, "SelectAll")
{
vRet := 0
Loop, 100
{
SendMessage, 0xB1, vPos1, vPos2, , ahk_id %hCtl% ;EM_SETSEL := 0xB1
SendMessage, 0xB0, &vPos1X, &vPos2X, , ahk_id %hCtl% ;EM_GETSEL := 0xB0
vPos1X := NumGet(vPos1X), vPos2X := NumGet(vPos2X)
if (vPos1 = vPos1X) && (vPos2 = vPos2X)
{
vRet := 1
break
}
Sleep 50
if !vRet
{
MsgBox % "error"
Return
}
}
}
}
;STAGE - perform standard Edit control menu functions
if InStr(A_ThisLabel , "Undo")
SendMessage, 0x304, , , , ahk_id %hCtl% ;WM_UNDO := 0x304
if InStr(A_ThisLabel , "Cut")
SendMessage, 0x300, , , , ahk_id %hCtl% ;WM_CUT := 0x300
if InStr(A_ThisLabel , "Copy")
SendMessage, 0x301, , , , ahk_id %hCtl% ;WM_COPY := 0x301
if InStr(A_ThisLabel , "Paste")
SendMessage, 0x302, , , , ahk_id %hCtl% ;WM_PASTE := 0x302
if InStr(A_ThisLabel , "Delete")
SendMessage, 0x303, , , , ahk_id %hCtl% ;WM_CLEAR := 0x303
if InStr(A_ThisLabel , "SelectAll")
SendMessage, 0xB1, 0, -1, , ahk_id %hCtl% ;EM_SETSEL := 0xB1
;STAGE - actions to take if user chooses custom menu item
if InStr(A_ThisLabel , "MyItem")
{
vText := "My String"
;ControlSend, , % vText, ahk_id %hCtl% ;use SendInput instead since capitalisation can be unreliable
SendInput {Raw}%vText%
}
;STAGE - actions to take if user chooses custom menu item
if 0 ;this comments out the 9 lines below
if InStr(A_ThisLabel , "MyItem") && !(vText = "")
{
MsgBox, 0x40003, , % "Choose 'Yes' to search for:`r`n" vText
IfMsgBox Yes
{
vUrl := "http://www.google.co.uk/search?q=" UriEncode(vText)
Run, "%vUrl%"
}
}
Return
#IfWinActive
;==================================================
;URL encoding - Rosetta Code
;https://www.rosettacode.org/wiki/URL_encoding#AutoHotkey
; Modified from https://autohotkey.com/board/topic/75390-ahk-l-unicode-uri-encode-url-encode-function/?p=480216
UriEncode(Uri)
{
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0)
StrPut(Uri, &Var, "UTF-8")
f := A_FormatInteger
SetFormat, IntegerFast, H
While Code := NumGet(Var, A_Index - 1, "UChar")
If (Code >= 0x30 && Code <= 0x39 ; 0-9
|| Code >= 0x41 && Code <= 0x5A ; A-Z
|| Code >= 0x61 && Code <= 0x7A) ; a-z
Res .= Chr(Code)
Else
Res .= "%" . SubStr(Code + 0x100, -1)
SetFormat, IntegerFast, %f%
Return, Res
}
;==================================================

Autohotkey - How to link a GUI to a window to act the same as its parent

I would like to link a GUI to a certain window, so it could act like it's a part of it.
This is my GUI and I would like it to follow the Calculator (for testing). If the calculator is minimized, the gui would be minimized as well.
Thanks in advance!
#SingleInstance Force
#Persistent
BC = 0
Gui, Color, EEAA99
Gui, Margin , 0, 0
GUI, +AlwaysOnTop -Border -SysMenu -Caption +ToolWindow +Owner
Gui, Font, S48 CDefault Bold CBlue, Verdana
Gui, Add, Text, BackgroundTrans , Units completed:
Gui, Font, S72 CDefault Bold CGreen, Verdana
Gui, Add, Text, BackgroundTrans vBuildCounter, %BC%
WinSet, TransColor, EEAA99
Gui +LastFound +AlwaysOnTop +ToolWindow
WinSet, TransColor, EEAA99
Gui -Caption
Gui, Show, % "x" A_ScreenWidth - 400 " y" A_ScreenHeight / 4
:?*:asd:: ;count up
SoundBeep, 500,500
BC := BC += 1
GuiControl,, BuildCounter, %BC%
Return
:?*:qwe:: ;reset the counter
SoundBeep, 500,500
BC := 0
GuiControl,, BuildCounter, %BC%
Return
Esc::
ExitApp
I ended up with two scripts. Maybe this can be combined later.
One script is for the ToolMenu, the second for the activation.
Since I could not control the GUI, Show/Hide from the Activation script, I "solved" it by using Ctrl+Alt+Win+F1 and Ctrl+Alt+Win+F2.Not the most elegant way, but it works...
ToolMenu.ahk
#SingleInstance Force
#installKeybdHook
#Persistent
Gui, Destroy
Gui,+AlwaysOnTop
Gui,+ToolWindow
Gui,+Border
Gui, Add, Button, y5 w60, &LowBeep
Gui, Add, Button, y5 w60, &HighBeep
Gui, Add, Button, y8 h18, X
Gui, Show, y0, MyToolWindow
Return
ButtonLowBeep:
SoundBeep, 300, 300
Return
ButtonHighBeep:
SoundBeep, 500, 300
Return
ButtonX:
ButtonCancel:
Gui, Destroy
ExitApp
^!#F1::
Gui, Hide
Return
^!#F2::
Gui, Show, y0, MyToolWindow
Return
DetectWindowChange.ahk
#SingleInstance
#installKeybdHook
#Persistent
Global SwitchCounter
Gui +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return
ShellMessage( wParam )
{
If (wParam = 4)
{
WinGetTitle, CurrName, A
If (CurrName = "Calculator" OR CurrName = "MyToolWindow")
{
If ( SwitchCounter = 0)
{
;WinRestore, MyToolWindow
Send, ^!#{F2} ; Send Ctrl+Alt+Win+F2 to trigger GUI Show in GUI script
}
SwitchCounter += 1
}
Else
{
If ( SwitchCounter > 0)
{
;WinMinimize, MyToolWindow
Send, ^!#{F1} ; Send Ctrl+Alt+Win+F1 to trigger GUI Hide in GUI script
}
SwitchCounter := 0
}
}
}
Return
Let me know how this works...
You can (as far as I know) only do this with a settimer.
Pseudo code, not tested!
SetTitleMatchMode := 2
SetTimer, CheckWindow, 200
CheckWindow:
If WinActive("Calculator")
{
Gui, Show, % "x" A_ScreenWidth - 400 " y" A_ScreenHeight / 4, Popup
}
Else If !WinActive("Popup")
{
Gui, Hide
}
Return
Edit: Added a condition to avoid hiding the popup if it is activated.

Resources