diff --git a/robotgo.go b/robotgo.go index b57e25c..417ed68 100644 --- a/robotgo.go +++ b/robotgo.go @@ -1385,10 +1385,23 @@ func GetBHandle() int { } // GetTitle get the window title -func GetTitle() string { - title := C.get_title() +func GetTitle(args ...int32) string { + if len(args) <= 0 { + title := C.get_main_title() + gtittle := C.GoString(title) + return gtittle + } + + var hwnd, isHwnd int32 + if len(args) > 0 { + hwnd = args[0] + } + if len(args) > 1 { + isHwnd = args[1] + } + title := C.get_title(C.uintptr(hwnd), C.uintptr(isHwnd)) gtittle := C.GoString(title) - // fmt.Println("title...", gtittle) + return gtittle } diff --git a/window/goWindow.h b/window/goWindow.h index 8f111e6..526feb5 100644 --- a/window/goWindow.h +++ b/window/goWindow.h @@ -10,7 +10,6 @@ #include "alert_c.h" #include "window.h" -#include "win32.h" #include "win_sys.h" int show_alert(const char *title, const char *msg, @@ -87,6 +86,7 @@ bool set_handle(uintptr handle){ uintptr get_handle(){ MData mData = GetActive(); + #if defined(IS_MACOSX) return (uintptr)mData.CgID; #elif defined(USE_X11) @@ -106,21 +106,7 @@ void set_active(const MData win){ } void active_PID(uintptr pid, uintptr isHwnd){ - MData win; - #if defined(IS_MACOSX) - // Handle to a AXUIElementRef - win.AxID = AXUIElementCreateApplication(pid); - #elif defined(USE_X11) - win.XWin = (Window)pid; // Handle to an X11 window - #elif defined(IS_WINDOWS) - // win.HWnd = (HWND)pid; // Handle to a window HWND - if (isHwnd == 0) { - win.HWnd = GetHwndByPId(pid); - } else { - win.HWnd = (HWND)pid; - } - #endif - + MData win = set_hand_pid(pid, isHwnd); SetActive(win); } @@ -129,8 +115,8 @@ MData get_active(){ return mdata; } -char* get_title(){ - char* title = GetTitle(); +char* get_title(uintptr pid, uintptr isHwnd){ + char* title = get_title_by_pid(pid, isHwnd); // printf("title::::%s\n", title ); return title; } diff --git a/window/window.h b/window/window.h index a7569ca..12951c6 100644 --- a/window/window.h +++ b/window/window.h @@ -12,12 +12,14 @@ // #include #include "process.h" #include "pub.h" +#include "win32.h" bool setHandle(uintptr handle); bool IsValid(); bool IsAxEnabled(bool options); MData GetActive(void); void initWindow(); +char* get_title_by_hand(MData m_data); //int findwindow() @@ -46,6 +48,26 @@ void initWindow(uintptr handle){ setHandle(handle); } +MData set_hand_pid(uintptr pid, uintptr isHwnd){ + MData win; + + #if defined(IS_MACOSX) + // Handle to a AXUIElementRef + win.AxID = AXUIElementCreateApplication(pid); + #elif defined(USE_X11) + win.XWin = (Window)pid; // Handle to an X11 window + #elif defined(IS_WINDOWS) + // win.HWnd = (HWND)pid; // Handle to a window HWND + if (isHwnd == 0) { + win.HWnd = GetHwndByPId(pid); + } else { + win.HWnd = (HWND)pid; + } + #endif + + return win; +} + bool IsValid(){ initWindow(initHandle); if (!IsAxEnabled(true)) { @@ -545,11 +567,22 @@ void CloseWin(void){ #elif defined(IS_WINDOWS) PostMessage(mData.HWnd, WM_CLOSE, 0, 0); - #endif } -char *GetTitle(){ +char* get_main_title(){ + // Check if the window is valid + if (!IsValid()) {return "IsValid failed.";} + + return get_title_by_hand(mData); +} + +char* get_title_by_pid(uintptr pid, uintptr isHwnd){ + MData win = set_hand_pid(pid, isHwnd); + return get_title_by_hand(win); +} + +char* get_title_by_hand(MData m_data){ // Check if the window is valid if (!IsValid()) {return "IsValid failed.";} @@ -558,7 +591,7 @@ char *GetTitle(){ CFStringRef data = NULL; // Determine the current title of the window - if (AXUIElementCopyAttributeValue(mData.AxID, + if (AXUIElementCopyAttributeValue(m_data.AxID, kAXTitleAttribute, (CFTypeRef*) &data) == kAXErrorSuccess && data != NULL) { char conv[512]; @@ -581,7 +614,7 @@ char *GetTitle(){ XDismissErrors(); // Get window title (UTF-8) - result = GetWindowProperty(mData, WM_NAME,NULL); + result = GetWindowProperty(m_data, WM_NAME,NULL); // Check result value if (result != NULL) { @@ -593,7 +626,7 @@ char *GetTitle(){ } // Get window title (ASCII) - result = GetWindowProperty(mData, XA_WM_NAME,NULL); + result = GetWindowProperty(m_data, XA_WM_NAME,NULL); // Check result value if (result != NULL) { @@ -608,8 +641,8 @@ char *GetTitle(){ #elif defined(IS_WINDOWS) return GetWindowText( - mData.HWnd, mData.Title, 512) > 0 ? - mData.Title : ""; + m_data.HWnd, m_data.Title, 512) > 0 ? + m_data.Title : ""; // return GetWindowText // (mData.HWnd, name, 512) > 0 ? // _UTF8Encode(name) : "null";