javascript - xul:多个显示器上的面板位置

标签 javascript firefox xul

我的 Firefox 扩展上有一个 xul:panel。我根据 screen.width 和 screen.height 给出显示位置。当我有多个显示器并且我在第一个显示器上启动浏览器,它出现在第二个显示器上并且 xul:panel 是根据第二个显示器上的第一个屏幕的分辨率绘制时,就会出现我的问题。有没有办法根据第二个屏幕的分辨率进行绘制?

最佳答案

背景:

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

XULRunner 确实正确地给了我:

  • 完整多显示器显示的几何形状(宽度、高度)
  • 多显示器显示屏上给定窗口的窗口位置
  • 给定窗口的窗口状态(最大化、最小化,两者都不是)
  • 能够(取消)最大化窗口

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

这让我需要以某种方式执行以下操作:

  • 相对于多显示器显示定位窗口,以及
    (因为移动窗口有时会失去窗口焦点)
  • 聚焦窗口。

借助 js-ctypes 加载/使用的外部 DLL,我能够实现这两个目标。


Win32 示例:

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

共有 3 个部分:

  1. 用于加载/绑定(bind) Win32 API 的特权 JavaScript 代码
  2. 外部 DLL 的 CPP 头文件
  3. 外部 DLL 的 CPP 源文件

我使用后两个文件构建了一个简单的 GUI DLL 项目,并根据 msvcr100.dll 编译了 wmctrl.dll,并使用了 Dependency Walker查找由 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);

wmctrldll.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);

wmctrldll.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);
}

关于javascript - xul:多个显示器上的面板位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10070456/

相关文章:

javascript - xul/xpcom 将图像从字符串复制到剪贴板

javascript - View 和JS之间传递变量; Django

javascript - 在 Firefox 中将对象转换为字符串

canvas - 我可以从页内 JavaScript 访问 Firefox 插件元素吗?

javascript - Firefox 无法创建使用 TypeScript 生成的类的实例

javascript - JS 和 type.match 作为文件 mime 类型 - 需要建议

javascript - 如何使用 XUL 打开网络文件夹

javascript - 如何启动 jQuery 切换折叠?

javascript - (js) 使用 jquery 或类似工具平滑地 move div x 像素?

javascript - 网站的管理部分?