Javascript xul:多个监视器上的面板位置

Javascript xul:多个监视器上的面板位置,javascript,firefox,xul,Javascript,Firefox,Xul,我的firefox扩展上有一个xul:panel。我根据screen.width和screen.height给出显示位置。当我有多个监视器并且在第一个监视器上启动浏览器时,我的问题就会出现,它会出现在第二个监视器上,并且xul:面板是根据第一个屏幕在第二个屏幕上的分辨率绘制的。是否有根据第二个屏幕的分辨率绘制的解决方案?背景: 当我在基于XULRunner for work开发多监视器应用程序时,我发现您无法预测在主应用程序/浏览器窗口首次启动后,窗口管理器会将启动的窗口放置在何处 XULRun

我的firefox扩展上有一个xul:panel。我根据screen.width和screen.height给出显示位置。当我有多个监视器并且在第一个监视器上启动浏览器时,我的问题就会出现,它会出现在第二个监视器上,并且xul:面板是根据第一个屏幕在第二个屏幕上的分辨率绘制的。是否有根据第二个屏幕的分辨率绘制的解决方案?

背景:

当我在基于XULRunner for work开发多监视器应用程序时,我发现您无法预测在主应用程序/浏览器窗口首次启动后,窗口管理器会将启动的窗口放置在何处

XULRunner确实正确地给了我:
  • 全多显示器显示的几何图形(宽度、高度)
  • 多监视器显示屏上给定窗口的窗口位置
  • 给定窗口的窗口状态(最大化、最小化、均不)
  • (取消)最大化窗口的能力

当我指定一组窗口坐标将窗口放置在特定监视器上时,它没有正确地尊重多监视器几何图形(而是窗口管理器将新窗口放置在它喜欢的任何位置)

这让我不得不做以下几件事:
  • 相对于多显示器显示器定位窗口,以及
    (因为移动窗口有时会丢失窗口焦点)
  • 聚焦窗口

在js ctypes加载/使用的外部DLL的帮助下,我能够实现这两个目标


Win32的示例:

以下是将外部DLL绑定到JavaScript的基础知识。这个例子只涉及Win32,但我也为Linux和MacOSX做了这件事(与Win32相比,它们分别更容易和更难)

共有3个部分:
  • 加载/绑定Win32 API的特权JavaScript代码
  • 外部DLL的CPP头文件
  • 外部DLL的CPP源文件
  • 我使用后面的两个文件构建了一个简单的GUI DLL项目&compiled
    wmctrl.DLL
    ,具体取决于
    msvcr100.DLL
    ,并用于查找DLL导出的供js ctypes使用的“纯C”符号

    我还围绕API构建了一个JavaScript库,允许在应用程序的多次运行中操作、跟踪和持久化多个窗口的窗口状态/几何体,但这与这个简单的示例并不相关

    在特权JavaScript代码中:

    // get js-ctypes, you do this part a bit differently from browser chrome
    const {Cc,Ci,Cu} = require("chrome"); 
    var file=null, lib=null, ctypes = {};
    Cu.import("resource://gre/modules/ctypes.jsm", ctypes);
    var ctypes = ctypes.ctypes;
    
    // build platform specific library path
    var filename = ctypes.libraryName("wmctrl");
    var comp = "@mozilla.org/file/directory_service;1";
    var file = Cc[comp].getService(Ci.nsIProperties).get("CurProcD", Ci.nsIFile);
    file.append("browser_code");
    file.append(filename);
    
    // get the JavaScript library interface (load the library)
    var lib = ctypes.open(file.path);
    
    // wmctrl_find_window: returing unsigned 32bit (long) "window handle"
    // takes string "window title".
    var find_window = lib.declare("?wmctrl_find_window@@YAKPAD@Z", 
        ctypes.stdcall_abi, ctypes.uint32_t,
        ctypes.char.ptr);
    
    // wmctrl_window_focus: takes unsigned 32bit (long) "window handle".
    var window_focus = lib.declare("?wmctrl_window_focus@@YAXK@Z", 
        ctypes.stdcall_abi, ctypes.void_t,
        ctypes.uint32_t);
    
    // wmctrl_window_move: takes unsigned 32bit (long) "window handle", 
    // and two (x & y) signed 32bit ints. 
    var window_move = lib.declare("?wmctrl_window_move@@YAXKHH@Z", 
        ctypes.stdcall_abi, ctypes.void_t,
        ctypes.uint32_t, ctypes.int32_t, ctypes.int32_t);
    
    WMCTRLDL.h

    #ifdef WMCTRLDLL_EXPORTS
    #define WMCTRLDLL_API __declspec(dllexport)
    #else
    #define WMCTRLDLL_API __declspec(dllimport)
    #endif
    
    WMCTRLDLL_API void wmctrl_window_focus (unsigned long wid);
    WMCTRLDLL_API void wmctrl_window_move (unsigned long wid, int x, int y);
    WMCTRLDLL_API unsigned long wmctrl_find_window(char* find_title);
    
    wmctrldl.cpp

    #include "stdafx.h"
    #include "wmctrldll.h"
    
    typedef struct {
      HWND hWnd;
      char title[255];
    } myWinSpec;
    
    BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
      char String[255];
      myWinSpec* to_find = (myWinSpec*) lParam;
    
      // not a window
      if (!hWnd) return TRUE;                                      
    
      // not visible
      if (!IsWindowVisible(hWnd)) return TRUE;
    
      // no window title                     
      if (!GetWindowTextA(hWnd, (LPSTR)String, 255)) return TRUE;  
    
      // no title match
      if (strcmp(String, to_find->title) != 0) return TRUE;        
    
      to_find->hWnd = hWnd;
      return FALSE;
    }
    
    WMCTRLDLL_API void wmctrl_window_focus(unsigned long wid) {
      SetForegroundWindow((HWND) wid);
    }
    
    WMCTRLDLL_API unsigned long wmctrl_find_window(char* find_title) {
      myWinSpec to_find;
    
      sprintf_s(to_find.title, sizeof(to_find.title), "%s", find_title);
      to_find.hWnd = 0;
    
      EnumWindows(EnumWindowsProc, (LPARAM)&to_find);
      return (unsigned long) to_find.hWnd;
    }
    
    WMCTRLDLL_API void wmctrl_window_move(unsigned long wid, int x, int y) {
      UINT flags = SWP_SHOWWINDOW | SWP_NOSIZE;
    
      SetForegroundWindow((HWND) wid);
      SetWindowPos((HWND) wid, HWND_NOTOPMOST, x, y, NULL, NULL, flags);
    }
    

    真是一团糟!我不得不暂停开发基于XULRunner的应用程序来进行窗口管理。我最终直接从
    js ctypes
    使用Win32或X11 API(取决于平台)。基本上,我会快速改变窗口的标题,以通过窗口枚举从C++代码中识别它,然后将它设置回原来的标题,并保留对所标识窗口的引用,以便以后使用API,我通过“代码> JS CyType < /代码>接触到JavaScript。或者,如果这些都适用于Firefox扩展,我会写一个答案,但也许有人可以加入一个纯JavaScript解决方案:-)好的,这是一个相当多的代码和解释:-/希望这能让你开始,或者有人找到一个更简单的独立于平台的方法。