通过Firefox JS-ctypes创建Win32窗口

时间:2015-04-14 00:17:15

标签: javascript winapi firefox

我正在尝试根据本教程创建一个Win32窗口。

http://www.winprog.org/tutorial/simple_window.html

Firefox JS-ctypes代码如下:

"use strict";
Components.utils.import("resource://gre/modules/ctypes.jsm");

var user32 = ctypes.open("user32.dll");
var kernel32 = ctypes.open("kernel32.dll");

const HWND_MESSAGE = -3; 
const WM_COPYDATA = 74;

var lpszClassName = ctypes.char.array()("Modus RP Class");    
var lpszWindowName = ctypes.char.array()("Modus RP");

const COPYDATASTRUCT = ctypes.StructType("COPYDATASTRUCT",
    [ 
      {dwData: ctypes.uintptr_t},
      {cbData: ctypes.uint32_t},
      {lpData: ctypes.voidptr_t}
    ]);    

var DefWindowProc = user32.declare("DefWindowProcA", ctypes.winapi_abi, ctypes.int,
    ctypes.voidptr_t, ctypes.int32_t, ctypes.int32_t, ctypes.int32_t);

var ShowWindow = user32.declare("ShowWindow", 
    ctypes.winapi_abi, ctypes.bool, ctypes.voidptr_t, ctypes.int32_t);

var UpdateWindow = user32.declare("UpdateWindow", 
    ctypes.winapi_abi, ctypes.bool, ctypes.voidptr_t);          

function windowProcJSCallback(hWnd, uMsg, wParam, lParam) {
  if (uMsg == WM_COPYDATA) {
    var CD = COPYDATASTRUCT.ptr(lParam).contents;
    var s = ctypes.cast(CD.lpData, ctypes.jschar.ptr).readString();
    alert(s);
    return ctypes.int(0);
  } else 
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

var WindowProcType = ctypes.FunctionType(ctypes.stdcall_abi, ctypes.int,
    [ctypes.voidptr_t, ctypes.int32_t, ctypes.int32_t, ctypes.int32_t]).ptr;
var WindowProcPointer = WindowProcType(windowProcJSCallback);
var WNDCLASS = ctypes.StructType("WNDCLASS",
    [
      { style: ctypes.uint32_t },
      { lpfnWndProc: WindowProcType }, 
      { cbClsExtra: ctypes.int32_t },
      { cbWndExtra: ctypes.int32_t },
      { hInstance: ctypes.voidptr_t },
      { hIcon: ctypes.voidptr_t },
      { hCursor: ctypes.voidptr_t },
      { hbrBackground: ctypes.voidptr_t },
      { lpszMenuName: ctypes.char.ptr },
      { lpszClassName: ctypes.char.ptr }
    ]);

var GetModuleHandle = kernel32.declare("GetModuleHandleA", ctypes.winapi_abi, 
ctypes.voidptr_t, ctypes.char.ptr);    
var hInstance = GetModuleHandle(null);    

var wndclass = WNDCLASS();
wndclass.hInstance = hInstance;
wndclass.lpszClassName = lpszClassName;
wndclass.lpfnWndProc = WindowProcPointer;   

var RegisterClass = user32.declare("RegisterClassA", ctypes.winapi_abi, 
    ctypes.uint32_t, WNDCLASS.ptr);
var UnregisterClass = user32.declare("UnregisterClassA", ctypes.winapi_abi, 
    ctypes.bool, ctypes.char.ptr, ctypes.voidptr_t);
var DestroyWindow = user32.declare("DestroyWindow", ctypes.winapi_abi, 
    ctypes.bool, ctypes.voidptr_t);

RegisterClass(wndclass.address());

var CreateWindowEx = user32.declare("CreateWindowExA", ctypes.winapi_abi, 
    ctypes.voidptr_t, ctypes.int, ctypes.char.ptr, ctypes.char.ptr, ctypes.int, 
    ctypes.int, ctypes.int, ctypes.int, ctypes.int, ctypes.voidptr_t, 
    ctypes.voidptr_t, ctypes.voidptr_t, ctypes.voidptr_t);

var win = CreateWindowEx(0, lpszClassName, lpszWindowName, 0xcf0000, 500, 260, 240, 120, 
    null, null, hInstance, null);  

ShowWindow(win,5);
UpdateWindow(win);

const WPARAM = ctypes.int32_t;
//const LPARAM = ctypes.PointerType(COPYDATASTRUCT);
const LPARAM = ctypes.size_t;

var POINT = ctypes.StructType("POINT",
[
 { x : ctypes.int },
 { y : ctypes.int }
]);

var MSG = ctypes.StructType("MSG",
[
 { hWnd : ctypes.voidptr_t },
 { message : ctypes.int },
 { wParam : WPARAM },
 { lParam : LPARAM },
 { time : ctypes.int },
 { pt : POINT}
]);

var TranslateMessage = user32.declare("TranslateMessage", 
    ctypes.winapi_abi, ctypes.bool, MSG.ptr);   

var DispatchMessage  = user32.declare("DispatchMessageW", 
    ctypes.winapi_abi, ctypes.uint32_t, MSG.ptr);   

var GetMessage = user32.declare("GetMessageW", 
    ctypes.winapi_abi, ctypes.bool, MSG.ptr, ctypes.voidptr_t, ctypes.int32_t, ctypes.int32_t);

var messg= MSG();
while(GetMessage(messg.address(),win,0,0)){
    TranslateMessage(messg.address());
    DispatchMessage(messg.address());
}

窗口已成功创建,但在移动它并等待一段时间后,Firefox无法响应。

我是否编写了错误的代码,或者由于JS-ctypes的性能损失,这个原生窗口创建是否超出了Firefox?

0 个答案:

没有答案