Skip to content

Instantly share code, notes, and snippets.

@karronoli
Created April 28, 2012 00:19
Show Gist options
  • Save karronoli/2514508 to your computer and use it in GitHub Desktop.
Save karronoli/2514508 to your computer and use it in GitHub Desktop.
ExecuteExcel4Macro test
@if(0)==(0) @echo off & rem -*- Javascript -*-
CScript.exe //NoLogo //E:JScript "%~f0" %*
exit /b 0
@end
var excel = new ActiveXObject("Excel.Application"),
WshShell = WScript.CreateObject("WScript.Shell"),
BIN_DIR = WScript.ScriptFullName.replace(WScript.ScriptName, "");
WINDOW_CLASS = "HTML Application Host Window Class",
//WINDOW_CLASS = "Internet Explorer_Server",
WINDOW_TITLE = "hoge",
ALPHA = 128,
DEBUG = 1;
// make & wait window
WshShell.Run("mshta.exe " + BIN_DIR + "a.html");
WScript.Sleep(1000);
// win32api constant
var GW_HWNDPREV = 3,
SW_RESTORE = 9,
WS_EX_LAYERED = 0x80000,
GWL_EXSTYLE = -20,
LWA_ALPHA = 2;
var FindWindow = function (classname, caption) {
var tmp = (caption)?
'CALL("user32","FindWindowA","JCC",' +
'"' + classname + '","' + caption + '")' :
'CALL("user32","FindWindowA","JCC",' +
'"' + classname + '",0)' ;
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var SetWindowLong = function (hWnd, index, value) {
var tmp = 'CALL("user32","SetWindowLongA","JJJJ",' +
hWnd + ',' + index + ',' + value + ')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var GetWindowLong = function (hWnd, index) {
var tmp = 'CALL("user32","GetWindowLongA","JJJ",' +
hWnd + ',' + index + ')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var SetLayeredWindowAttributes = function (hWnd, bAlpha, dwFlags) {
var tmp = 'CALL("user32","SetLayeredWindowAttributes","JJJJJ",' +
hWnd + ',0,' + bAlpha + ',' + dwFlags +')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var WindowFromPoint = function (x, y) {
var tmp = 'CALL("user32","WindowFromPoint","JJJ",' +
x + ',' + y + ')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var MoveWindow = function (hWnd, x, y, nWidth, nHeight, bRepaint) {
var tmp = 'CALL("user32","MoveWindow","JJJJJJJ",' +
hWnd + ',' + x + ',' + y + ',' + nWidth + ',' +
nHeight + ',' + bRepaint + ')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
// type error
var SetWindowText = function (hWnd, title) {
//var tmp = 'CALL("user32","SetWindowText","JCA",' +
var tmp = 'CALL("user32","SetWindowText","JCJ",' +
hWnd + ',"' + title + '")';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var GetForegroundWindow = function () {
var tmp = 'CALL("user32","GetForegroundWindow","J")';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var CreateSolidBrush = function (color) {
var tmp = 'CALL("gdi32","CreateSolidBrush","JJ",' + color + ')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var GetDC = function (hWnd) {
var tmp = 'CALL("user32","GetDC","JJ",' + hWnd + ')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var GetBkColor = function (hdc) {
var tmp = 'CALL("gdi32","GetBkColor","JJ",' + hdc + ')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var SetBkColor = function (hdc, color) {
var tmp = 'CALL("gdi32","SetBkColor","JJJ",' +
[hdc, color].join(',') + ')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
var SetBkMode = function (hdc, mode) {
var tmp = 'CALL("gdi32","SetBkMode","JJJ",' +
[hdc, mode].join(',') + ')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
// no work
var Rectangle = function (hdc, left, top, right, bottom) {
var tmp = 'CALL("gdi32","Rectangle","JJJJJJ",' +
[hdc, left, top, right, bottom].join(',') + ')';
var ret = excel.ExecuteExcel4Macro(tmp);
if (DEBUG) WScript.Echo(ret + " // " + tmp);
return ret;
};
// no work.
// var hwnd = FindWindow(WINDOW_CLASS, "");
// var hwnd = WindowFromPoint(500, 500);
// var hwnd = FindWindow(WINDOW_CLASS, WINDOW_TITLE);
var hwnd = GetForegroundWindow();
// transparent window
SetWindowLong(hwnd, GWL_EXSTYLE,
WS_EX_LAYERED | GetWindowLong(hwnd, GWL_EXSTYLE));
SetLayeredWindowAttributes(hwnd, ALPHA, LWA_ALPHA);
MoveWindow(hwnd, 0, 0, 1024, 768, true);
// SetWindowText(hwnd, "abc");
var hdc = GetDC(hwnd);
GetBkColor(hdc);
SetBkColor(hdc, 0);
SetBkMode(hdc, 1);
Rectangle(hdc, 100, 100, 100, 100);;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment