I'm trying to create a custom C++ function which I can call from JavaScript. The function is a simple one to just resize the window.
I have the following bits in the following places:
In appshell_extensions_platform.h:
#if defined(OS_WIN)
void ResizeWindow(CefRefPtr<CefBrowser> browser, int width, int height);
#endif
In appshell_extensions_win.cpp:
void ResizeWindow(CefRefPtr<CefBrowser> browser, int width, int height) {
OutputDebugString(L"ResizeWindow");
CefWindowHandle hWnd = browser->GetHost()->GetWindowHandle();
SetWindowPos(hWnd, 0, 0, 0, width, height, SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
}
In appshell_extensions.js:
/**
* Resize the window to the given size.
*
* @param {number} width
* @param {number} height
*
* @return None. This is an asynchronous call that sends all return information to the callback.
*/
native function ResizeWindow();
appshell.app.resizeWindow = function (width, height) {
ResizeWindow(width, height);
};
In appshell_extensions.cpp:
} else if (message_name == "ResizeWindow") {
// Parameters:
// 0: int32 - width
// 1: int32 - height
int width = argList->GetInt(0);
int height = argList->GetInt(1);
ResizeWindow(browser, width, height);
}
Using Visual Studio 2012, I then Build and Debug on the Debug Win32 release. When I open the console, appshell.app.resizeWindow
is there as expected. I can call it and it works just fine. If I put additional JavaScript code in the function, it also works.
To the function in appshell_extensions.cpp
, I've added OutputDebugString(std::wstring(message_name.begin(), message_name.end()).c_str());
. For functions other than the one I wrote, it will output the message name properly. For the one I wrote, I get nothing.
I don't get the output from the function itself either.
It appears that the message isn't actually getting to the function which processes it, but I have no clue. I'm just using the sln that came with brackets-shell (converted to 2012) to compile. Is there a build step I'm maybe missing or something?
Thanks.
I figured out the problem.
Apparently, in the JavaScript functions, the first parameter MUST be a callback, even if you don't use it.
Changing the JavaScript portion to this fixed the problem:
/**
* Resize the window to the given size.
*
* @param {number} width
* @param {number} height
*
* @return None. This is an asynchronous call that sends all return information to the callback.
*/
native function ResizeWindow();
appshell.app.resizeWindow = function (width, height) {
ResizeWindow(_dummyCallback, width, height);
};
I also had a bit of an issue with the actual resize logic. You need to use the window handle of the browser->getHost()->getWindowHandle()
parent:
`CefWindowHandle hWnd = getParent(browser->GetHost()->GetWindowHandle());