diff --git a/internal/ad.go b/internal/ad.go index 369769b..d6e73f8 100644 --- a/internal/ad.go +++ b/internal/ad.go @@ -38,10 +38,16 @@ func HidePopupAd() { } func HideMainWindowAd(windowClass string, handle windows.HWND) { + // @deprecated if windowClass == "BannerAdWnd" { winapi.ShowWindow(handle, 0) winapi.SetWindowPos(handle, 0, 0, 0, 0, 0, winapi.SwpNomove) } + if windowClass == "BannerAdContainer" { + parentHandle := winapi.GetParent(handle) + winapi.ShowWindow(parentHandle, 0) + winapi.SetWindowPos(parentHandle, 0, 0, 0, 0, 0, winapi.SwpNomove) + } } func HideLockScreenAdArea(windowText string, rect *winapi.Rect, handle windows.HWND) { diff --git a/internal/app.go b/internal/app.go index 04c9a5c..b425b94 100644 --- a/internal/app.go +++ b/internal/app.go @@ -86,13 +86,32 @@ func removeAd() { rect := new(winapi.Rect) winapi.GetWindowRect(wnd, rect) + var mainWindowParentHandle windows.HWND + var candidates [][]windows.HWND for _, childHandle := range childHandles { className := winapi.GetClassName(childHandle) windowText := winapi.GetWindowText(childHandle) + parentHandle := winapi.GetParent(childHandle) + if className == "EVA_ChildWindow" { + if windowText == "" { + candidates = append(candidates, []windows.HWND{childHandle, parentHandle}) + } else if strings.HasPrefix(windowText, "OnlineMainView") { + mainWindowParentHandle = parentHandle + } + } HideMainWindowAd(className, childHandle) HideMainViewAdArea(windowText, rect, childHandle) HideLockScreenAdArea(windowText, rect, childHandle) } + if mainWindowParentHandle != 0 && len(candidates) > 0 { + for _, candidate := range candidates { + if candidate[1] == mainWindowParentHandle { + winapi.ShowWindow(candidate[0], 0) + winapi.SetWindowPos(candidate[0], 0, 0, 0, 0, 0, winapi.SwpNomove) + break + } + } + } } HidePopupAd() mutex.Unlock() diff --git a/internal/win/tray_icon.go b/internal/win/tray_icon.go index 5a7d460..998c69f 100644 --- a/internal/win/tray_icon.go +++ b/internal/win/tray_icon.go @@ -1,109 +1,109 @@ -package win - -import ( - "unsafe" - - "golang.org/x/sys/windows" - - "kakaotalkadblock/internal/win/winapi" -) - -var quit *chan struct{} - -func wndProc(hWnd uintptr, msg uint32, wParam, lParam uintptr) uintptr { - switch msg { - case winapi.WmTrayicon: - switch uint16(lParam) { - case winapi.WmLbuttondblclk: - close(*quit) - } - case winapi.WmDestroy: - winapi.PostQuitMessage(0) - default: - return winapi.DefWindowProc(hWnd, msg, wParam, lParam) - } - return 0 -} - -func createMainWindow() (uintptr, error) { - hInstance, err := winapi.GetModuleHandle(nil) - if err != nil { - return 0, err - } - - wndClass, _ := windows.UTF16PtrFromString("KakaoTalkAdBlock") - - var windowClass winapi.WindowClassEx - - windowClass.CbSize = uint32(unsafe.Sizeof(windowClass)) - windowClass.LpfnWndProc = windows.NewCallback(wndProc) - windowClass.HInstance = hInstance - windowClass.LpszClassName = wndClass - if _, err := winapi.RegisterClassEx(&windowClass); err != nil { - return 0, err - } - - handle, err := winapi.CreateWindowEx( - 0, - wndClass, - windows.StringToUTF16Ptr("KakaoTalkAdBlock"), - winapi.WsOverlappedwindow, - winapi.CwUsedefault, - winapi.CwUsedefault, - 1, - 1, - 0, - 0, - hInstance, - nil) - if err != nil { - return 0, err - } - - return handle, nil -} - -type TrayIcon struct { - notifyIconData winapi.NotifyIconData -} - -func NewTrayIcon(quitChan *chan struct{}) *TrayIcon { - var data winapi.NotifyIconData - data.CbSize = uint32(unsafe.Sizeof(data)) - data.UFlags = winapi.NifIcon | winapi.NifMessage | winapi.NifInfo - data.UCallbackMessage = winapi.WmTrayicon - - hInst, err := winapi.GetModuleHandle(nil) - if err != nil { - panic(err) - } - icon, err := winapi.LoadIcon(hInst, winapi.MakeIntResource(1)) - if err != nil { - panic(err) - } - data.HIcon = icon - - quit = quitChan - return &TrayIcon{ - notifyIconData: data, - } -} - -func (t *TrayIcon) Show() { - if t.notifyIconData.HWnd == 0 { - handle, err := createMainWindow() - if err != nil { - panic(err) - } - t.notifyIconData.HWnd = handle - } - if err := winapi.ShellNotifyIcon(winapi.NimAdd, &t.notifyIconData); err != nil { - panic(err) - } -} - -func (t *TrayIcon) Hide() { - if err := winapi.ShellNotifyIcon(winapi.NimDelete, &t.notifyIconData); err != nil { - panic(err) - } -} +package win + +import ( + "unsafe" + + "golang.org/x/sys/windows" + + "kakaotalkadblock/internal/win/winapi" +) + +var quit *chan struct{} + +func wndProc(hWnd uintptr, msg uint32, wParam, lParam uintptr) uintptr { + switch msg { + case winapi.WmTrayicon: + switch uint16(lParam) { + case winapi.WmLbuttondblclk: + *quit <- struct{}{} + } + case winapi.WmDestroy: + winapi.PostQuitMessage(0) + default: + return winapi.DefWindowProc(hWnd, msg, wParam, lParam) + } + return 0 +} + +func createMainWindow() (uintptr, error) { + hInstance, err := winapi.GetModuleHandle(nil) + if err != nil { + return 0, err + } + + wndClass, _ := windows.UTF16PtrFromString("KakaoTalkAdBlock") + + var windowClass winapi.WindowClassEx + + windowClass.CbSize = uint32(unsafe.Sizeof(windowClass)) + windowClass.LpfnWndProc = windows.NewCallback(wndProc) + windowClass.HInstance = hInstance + windowClass.LpszClassName = wndClass + if _, err := winapi.RegisterClassEx(&windowClass); err != nil { + return 0, err + } + + handle, err := winapi.CreateWindowEx( + 0, + wndClass, + windows.StringToUTF16Ptr("KakaoTalkAdBlock"), + winapi.WsOverlappedwindow, + winapi.CwUsedefault, + winapi.CwUsedefault, + 1, + 1, + 0, + 0, + hInstance, + nil) + if err != nil { + return 0, err + } + + return handle, nil +} + +type TrayIcon struct { + notifyIconData winapi.NotifyIconData +} + +func NewTrayIcon(quitChan *chan struct{}) *TrayIcon { + var data winapi.NotifyIconData + data.CbSize = uint32(unsafe.Sizeof(data)) + data.UFlags = winapi.NifIcon | winapi.NifMessage | winapi.NifInfo + data.UCallbackMessage = winapi.WmTrayicon + + hInst, err := winapi.GetModuleHandle(nil) + if err != nil { + panic(err) + } + icon, err := winapi.LoadIcon(hInst, winapi.MakeIntResource(1)) + if err != nil { + panic(err) + } + data.HIcon = icon + + quit = quitChan + return &TrayIcon{ + notifyIconData: data, + } +} + +func (t *TrayIcon) Show() { + if t.notifyIconData.HWnd == 0 { + handle, err := createMainWindow() + if err != nil { + panic(err) + } + t.notifyIconData.HWnd = handle + } + if err := winapi.ShellNotifyIcon(winapi.NimAdd, &t.notifyIconData); err != nil { + panic(err) + } +} + +func (t *TrayIcon) Hide() { + if err := winapi.ShellNotifyIcon(winapi.NimDelete, &t.notifyIconData); err != nil { + panic(err) + } +}