Merge pull request #147 from go-vgo/dev

add min and max window support
This commit is contained in:
vz 2018-07-30 00:59:36 +08:00 committed by GitHub
commit 50b4b5c734
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 126 additions and 28 deletions

View File

@ -1286,6 +1286,40 @@ func GetActive() C.MData {
return mdata
}
// MinWindow set the window min
func MinWindow(pid int32, args ...interface{}) {
var (
state = true
hwnd int
)
if len(args) > 0 {
state = args[0].(bool)
}
if len(args) > 1 {
hwnd = args[1].(int)
}
C.min_window(C.uintptr(pid), C.bool(state), C.uintptr(hwnd))
}
// MaxWindow set the window max
func MaxWindow(pid int32, args ...interface{}) {
var (
state = true
hwnd int
)
if len(args) > 0 {
state = args[0].(bool)
}
if len(args) > 1 {
hwnd = args[1].(int)
}
C.max_window(C.uintptr(pid), C.bool(state), C.uintptr(hwnd))
}
// CloseWindow close the window
func CloseWindow() {
C.close_window()

View File

@ -14,3 +14,10 @@ func FindWindow(str string) win.HWND {
return hwnd
}
// GetHWND get foreground window hwnd
func GetHWND() win.HWND {
hwnd := win.GetForegroundWindow()
return hwnd
}

View File

@ -10,6 +10,7 @@
#include "alert_c.h"
#include "window.h"
#include "win32.h"
int show_alert(const char *title, const char *msg,
const char *defaultButton, const char *cancelButton){
@ -57,6 +58,44 @@ bool is_valid(){
// return z;
// }
void min_window(uintptr pid, bool state, uintptr isHwnd){
#if defined(IS_MACOSX)
// return 0;
AXUIElementRef axID = AXUIElementCreateApplication(pid);
AXUIElementSetAttributeValue(axID, kAXMinimizedAttribute,
state ? kCFBooleanTrue : kCFBooleanFalse);
#elif defined(USE_X11)
// Ignore X errors
XDismissErrors();
// SetState((Window)pid, STATE_MINIMIZE, state);
#elif defined(IS_WINDOWS)
if (isHwnd == 0) {
HWND hwnd = GetHwndByPId(pid);
win_min(hwnd, state);
} else {
win_min((HWND)pid, state);
}
#endif
}
void max_window(uintptr pid, bool state, uintptr isHwnd){
#if defined(IS_MACOSX)
// return 0;
#elif defined(USE_X11)
XDismissErrors();
// SetState((Window)pid, STATE_MINIMIZE, false);
// SetState((Window)pid, STATE_MAXIMIZE, state);
#elif defined(IS_WINDOWS)
if (isHwnd == 0) {
HWND hwnd = GetHwndByPId(pid);
win_max(hwnd, state);
} else {
win_max((HWND)pid, state);
}
#endif
}
void close_window(void){
CloseWin();
}
@ -86,34 +125,6 @@ void set_active(const MData win){
SetActive(win);
}
#if defined(IS_WINDOWS)
typedef struct{
HWND hWnd;
DWORD dwPid;
}WNDINFO;
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam){
WNDINFO* pInfo = (WNDINFO*)lParam;
DWORD dwProcessId = 0;
GetWindowThreadProcessId(hWnd, &dwProcessId);
if (dwProcessId == pInfo->dwPid) {
pInfo->hWnd = hWnd;
return FALSE;
}
return TRUE;
}
HWND GetHwndByPId(DWORD dwProcessId){
WNDINFO info = {0};
info.hWnd = NULL;
info.dwPid = dwProcessId;
EnumWindows(EnumWindowsProc, (LPARAM)&info);
// printf("%d\n", info.hWnd);
return info.hWnd;
}
#endif
void active_PID(uintptr pid, uintptr isHwnd){
MData win;
#if defined(IS_MACOSX)

46
window/win32.h Normal file
View File

@ -0,0 +1,46 @@
// #include "../base/os.h"
#if defined(IS_WINDOWS)
typedef struct{
HWND hWnd;
DWORD dwPid;
}WNDINFO;
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam){
WNDINFO* pInfo = (WNDINFO*)lParam;
DWORD dwProcessId = 0;
GetWindowThreadProcessId(hWnd, &dwProcessId);
if (dwProcessId == pInfo->dwPid) {
pInfo->hWnd = hWnd;
return FALSE;
}
return TRUE;
}
HWND GetHwndByPId(DWORD dwProcessId){
WNDINFO info = {0};
info.hWnd = NULL;
info.dwPid = dwProcessId;
EnumWindows(EnumWindowsProc, (LPARAM)&info);
// printf("%d\n", info.hWnd);
return info.hWnd;
}
// window
void win_min(HWND hwnd, bool state){
if (state) {
ShowWindow(hwnd, SW_MINIMIZE);
} else {
ShowWindow(hwnd, SW_RESTORE);
}
}
void win_max(HWND hwnd, bool state){
if (state) {
ShowWindow(hwnd, SW_MAXIMIZE);
} else {
ShowWindow(hwnd, SW_RESTORE);
}
}
#endif