Create a window in ruby extension


#1

My first test in my sketchup extension to call c++ went like this:

Ruby code:

require_relative "test_projects//x64//Debug//test_projects.so"

      if (SUEX_test.test1)
        UI.messagebox("It works")
      else
        UI.messagebox("Fail")
      end

And in the C++ I call it like this:

 VALUE test1(){	
	return Qtrue;
}

// The initialization method for this module
extern "C" CEXT_EXPORT
void Init_test_projects(){
	VALUE MyTest = rb_define_module("SUEX_test");
	rb_define_module_function(MyTest, "test1", VALUEFUNC(test1), 0);
}

And it works, opening a message box in sketchup that displays "It works!"
I get how that works with VALUEFUNC returning a value of true.
The code below is sample code I got from (http://www.winprog.org/tutorial/simple_window.html) that creates a window. I want to create a window in sketchup to add options to but I can’t seem to find any tutorial help on that. All I can do is create a basic toolbar that when clicked it opens up a webpage, but I want it to open up a window. If anyone has any tutorials that might help that would be nice. Below is the code that creates a window in c++. How can I call that in ruby and then in sketchup. I tried creating a ruby window.

I require ‘tk’

        root = TkRoot.new { title "RPC"}
        TkLabel.new(root) do 
        text 'This is the RPC window'
        pack { padx 15; pady 15; side 'left'}
        end

        Tk.mainloop

But i get the error : Error: #LoadError: cannot load such file – Tk

Below is the c++ code I would like to see work.

const TCHAR g_szClassName[] = TEXT("myWindowClass");

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_CLOSE:
		DestroyWindow(hwnd);
	break;
	case WM_DESTROY:
		PostQuitMessage(0);
	break;
	default:
		return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCMDLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	// Registering the Window Class
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = 0;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        TEXT("The title of my window"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, TEXT("Window Creation Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	return Msg.wParam;
}