在sciter的sdk中,已经对窗口提供了一个基础模板(sciter::window),但是这个基类只是声明了几个方法,并没有实现。为了使用这个基类,得先实现它。
main.cpp
#include
int uimain(std::function run)
{
return run();
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
auto run = []()
{
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return static_cast(msg.wParam);
};
return uimain(run);
}
编译程序,报错。
很明显,我们还需要实现构造函数和on_message方法。我们在main下方追加
namespace sciter
{
window::window(UINT creationFlags, RECT frame)
{
_hwnd = SciterCreateWindow(creationFlags, &frame, &msg_delegate, this, nullptr);
if (_hwnd) {
asset::add_ref();
//setup_callback();
//sciter::attach_dom_event_handler(_hwnd, this);
}
}
LRESULT window::on_message(HWINDOW hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL& pHandled)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default: break;
}
return 0;
}
}
至此,程序是可以编译了,但并没什么卵用,因为没有创建任何窗口。
实现一个窗口类MainWindow(建议窗口都放在sciter命名空间下)
MainWindow.h
#include
namespace sciter
{
class MainWindow: public sciter::window
{
public:
MainWindow();
~MainWindow();
};
}
MainWindow.cpp
namespace sciter
{
MainWindow::MainWindow() : window(SW_MAIN | SW_TITLEBAR | SW_CONTROLS, {100, 100, 500, 500})
{
}
MainWindow::~MainWindow()
{
}
}
回到uimain函数,修改如下
int uimain(std::function run)
{
auto window = new sciter::MainWindow();
window->expand();
return run();
}
上述代码就是在进入消息循环前创建一个窗口并显示。编译,报错。。。
这次要实现expand、msg_delegate。其中msg_delegate实质就是WndProc函数。
namespace sciter
{
window::window(UINT creationFlags, RECT frame)
{
_hwnd = SciterCreateWindow(creationFlags, &frame, &msg_delegate, this, nullptr);
if (_hwnd)
{
asset::add_ref();
//setup_callback();
//sciter::attach_dom_event_handler(_hwnd, this);
}
}
void window::expand(bool maximize)
{
if (_hwnd)
{
ShowWindow(_hwnd, maximize ? SW_MAXIMIZE : SW_NORMAL);
}
}
LRESULT SC_CALLBACK window::msg_delegate(HWINDOW hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LPVOID pParam, BOOL* pHandled)
{
auto bHandled = FALSE;
auto lr = SciterProcND(hwnd, msg, wParam, lParam, &bHandled);
if (bHandled)
return lr;
// 这里的pParam就是SciterCreateWindow的第4个参数(this)
auto w = static_cast(pParam);
return w->on_message(hwnd, msg, wParam, lParam, *pHandled);
}
LRESULT window::on_message(HWINDOW hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL& pHandled)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default: break;
}
return 0;
}
}
这样,程序已经可以创建窗口并运行了,但是此时还没有加载任何UI文件,所以还是看不到窗口。
创建一个MainWindow.html放到程序目录下
Hello World
Hello World!
查看window,发现有load方法,实现它
bool window::load(const WCHAR* url)
{
return FALSE != SciterLoadFile(_hwnd, url);
}
完整的main.cpp
#include
#include "MainWindow.h"
int uimain(std::function run)
{
auto window = new sciter::MainWindow();
if (window->load(L"MainWindow.html"))
{
window->expand();
}
return run();
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
auto run = []()
{
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return static_cast(msg.wParam);
};
return uimain(run);
}
namespace sciter
{
window::window(UINT creationFlags, RECT frame)
{
_hwnd = SciterCreateWindow(creationFlags, &frame, &msg_delegate, this, nullptr);
if (_hwnd)
{
asset::add_ref();
//setup_callback();
//sciter::attach_dom_event_handler(_hwnd, this);
}
}
void window::expand(bool maximize)
{
if (_hwnd)
{
ShowWindow(_hwnd, maximize ? SW_MAXIMIZE : SW_NORMAL);
}
}
LRESULT SC_CALLBACK window::msg_delegate(HWINDOW hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LPVOID pParam, BOOL* pHandled)
{
auto bHandled = FALSE;
auto lr = SciterProcND(hwnd, msg, wParam, lParam, &bHandled);
if (bHandled)
return lr;
// 这里的pParam就是SciterCreateWindow的第4个参数(this)
auto w = static_cast(pParam);
return w->on_message(hwnd, msg, wParam, lParam, *pHandled);
}
bool window::load(const WCHAR* url)
{
return FALSE != SciterLoadFile(_hwnd, url);
}
LRESULT window::on_message(HWINDOW hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL& pHandled)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default: break;
}
return 0;
}
}