Skip to content

Instantly share code, notes, and snippets.

@7shi
Created December 13, 2010 12:55
Show Gist options
  • Save 7shi/738961 to your computer and use it in GitHub Desktop.
Save 7shi/738961 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign
import Foreign.C.String
type Handle = Int
foreign import ccall "new_Form" new_Form :: IO Handle
foreign import ccall "new_Button" new_Button :: IO Handle
foreign import ccall "setText" setText :: Handle -> CWString -> IO ()
foreign import ccall "addClick" c_addClick :: Handle -> FunPtr (IO ()) -> IO ()
foreign import ccall "addControl" addControl :: Handle -> Handle -> IO ()
foreign import ccall "setClientSize" setClientSize :: Handle -> Int -> Int -> IO ()
foreign import ccall "setBounds" setBounds :: Handle -> Int -> Int -> Int -> Int -> IO ()
foreign import ccall "applicationRun" applicationRun :: Handle -> IO ()
foreign import ccall "closeForm" closeForm :: Handle -> IO()
foreign import ccall "msgbox" c_msgbox :: CWString -> CWString -> IO ()
foreign import ccall "wrapper" wrap :: IO () -> IO (FunPtr (IO ()))
addClick c f = do fp <- wrap f; c_addClick c fp
msgbox a b = withCWString a $ \wa -> withCWString b $ c_msgbox wa
main = do
f <- new_Form
withCWString "あいうえお" $ setText f
setClientSize f 160 160
b1 <- new_Button
b2 <- new_Button
withCWString "挨拶" $ setText b1
withCWString "終了" $ setText b2
setBounds b1 8 16 144 32
setBounds b2 8 64 144 32
addClick b1 $ msgbox "こんにちは" "世界"
addClick b2 $ closeForm f
addControl f b1
addControl f b2
applicationRun f
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Drawing;
using namespace System::Windows::Forms;
ref class Globals {
public:
static List<Object ^> objs;
};
ref class Handler {
private:
void(*f)();
public:
Handler(void(*f)()) : f(f) {}
void Invoke(Object ^sender, EventArgs ^e) { (*f)(); }
};
template <typename T> T ^_get(int id) {
return dynamic_cast<T ^>(Globals::objs[id]); }
inline int _add(Object ^obj) {
int ret = Globals::objs.Count;
Globals::objs.Add(obj);
return ret; }
inline EventHandler ^_wrap(void(*f)()) {
return gcnew EventHandler(gcnew Handler(f), &Handler::Invoke); }
#define CEXPORT extern "C" __declspec(dllexport)
CEXPORT int new_Form() {
return _add(gcnew Form()); }
CEXPORT int new_Button() {
return _add(gcnew Button()); }
CEXPORT void setText(int c, const wchar_t *text) {
_get<Control>(c)->Text = gcnew String(text); }
CEXPORT void addClick(int c, void(*f)()) {
_get<Control>(c)->Click += _wrap(f); }
CEXPORT void addControl(int c1, int c2) {
_get<Control>(c1)->Controls->Add(_get<Control>(c2)); }
CEXPORT void setClientSize(int c, int w, int h) {
_get<Control>(c)->ClientSize = Size(w, h); }
CEXPORT void setBounds(int c, int x, int y, int w, int h) {
_get<Control>(c)->Bounds = Rectangle(x, y, w, h); }
CEXPORT void closeForm(int f) {
_get<Form>(f)->Close(); }
CEXPORT void applicationRun(int f) {
Application::Run(_get<Form>(f)); }
CEXPORT void msgbox(const wchar_t *text, const wchar_t *title) {
MessageBox::Show(gcnew String(text), gcnew String(title)); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment