0

I found the question for a single window handling possible with the ClassName.

This is fine with one window. Is it possible to resize the specific external window(When multiple windows open with the same Exe)?

I opened the Notepad

like I opened text1.txt and text2.txt and try to resize both windows.

like text1.txt => 600x600 and

text2.txt => 500x500.

like I put two buttons in the electron App

First and Second

Using First Button I am trying to resize text1.txt

Using Second Button I am trying to resize text2.txt

Is it possible to resize using the file name? or is any other way available?

Does anyone suggest to me how it's possible?

  • So the real question is: How do you *identify* a window. The following pieces of information are available: Window class name, window title, and thread/process ID. It is not clear from the question, whether that is sufficient, but it's all you get. – IInspectable Nov 25 '22 at 22:05
  • You could try to ues [FindWindowA function](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowa?redirectedfrom=MSDN) to retrieve a handle. And then use [SetWindowPos function](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos) and [MoveWindow function](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-movewindow) to the resize the specified window. – Jeaninez - MSFT Nov 28 '22 at 03:21

1 Answers1

0

From your previous post, I assume you want to resize multiple top-level windows of the same class instead of the same process.

To do that, you first need to know the class name of your targeted windows. You can use a tool like Inspect or Spy++.

Then, the easiest way to list all top-level windows with a specific class is to use EnumWindows and GetClassName. For each matched window, you'll want to save its title and handle. This way the user can choose the window based on the title, then you can use the associated handle to resize it.

Here's a complete example:

const Win32 = require('win32-api/promise');
const Win32Sync = require('win32-api');
const ffi = require('ffi-napi');
const W = require('win32-api').DTypes;

const User32 = Win32.User32.load(['EnumWindows', 'FindWindowExW', 'SetWindowPos']);
const User32Sync = Win32Sync.User32.load(['IsWindowVisible', 'GetClassNameW', 'GetWindowTextW']);

const SWP_NOMOVE = 0x0002, SWP_NOACTIVATE = 0x0010;

module.exports =
{
  FindWindows: async function (className)
  {
    var windowsList = [];

    const lpEnumFunc = ffi.Callback(W.BOOL, [W.HWND, W.LPARAM], (hWnd, lParam) => 
    {
      // EnumWindowsProc: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms633498(v=vs.85)
      
      if (!User32Sync.IsWindowVisible(hWnd))
      {
        // Ignore hidden windows
        return true;
      }

      // Compare class names
      const maxLen = 127;
      const buf = Buffer.alloc(maxLen * 2);
      const len = User32Sync.GetClassNameW(hWnd, buf, maxLen);
      const wndClass = buf.toString('ucs2').replace(/\0+$/, '');
      
      if (len && wndClass === className)
      {
        let title = '';
      
        if(User32Sync.GetWindowTextW(hWnd, buf, maxLen))
        {
          title = buf.toString('ucs2').replace(/\0+$/, '');
        }          
              
        windowsList.push({ title: title, hWnd: hWnd });
      }

      return true;
    });

    await User32.EnumWindows(lpEnumFunc, 0);

    return windowsList;
  },

  ResizeWindow: async function (hWnd, width, height)
  {
    await User32.SetWindowPos(hWnd, 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOACTIVATE);
  }
};

However, this code won't work for you (at least as of now), because win32-api still doesn't include GetClassName. But you can easily add it, just edit win32-api/src/lib/user32/api.ts and recompile:

export interface Win32Fns
{
  // ...
  GetWindowTextW: (hWnd: M.HWND, lpString: M.LPCTSTR, nMaxCount: M.INT) => M.INT
  GetClassNameW: (hWnd: M.HWND, lpString: M.LPCTSTR, nMaxCount: M.INT) => M.INT // <-- add this
  // ...
}


export const apiDef: M.DllFuncs<Win32Fns> = {
  // ...
  GetWindowTextW: [W.INT, [W.HWND, W.LPTSTR, W.INT]],
  GetClassNameW: [W.INT, [W.HWND, W.LPTSTR, W.INT]], // <-- add this
  // ...
}

Result (full_example.zip): enter image description here

Osama
  • 324
  • 3
  • 11