gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:25:01 24 4
gpt4 key购买 nike

我的 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 项目并编译了 wmctrl.dll,依赖于 msvcr100.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com