chikuchikugonzalezの雑記帳

趣味とか日記とかメモとか(∩゚д゚)

ウィンドウを表示するだけのコード

なんとなくメモ的ななにか。

Java

Javaの場合
public class WindowJava {
    public static void main(String[] args) {
        javax.swing.JFrame win = new javax.swing.JFrame();
        win.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        win.setVisible(true);
    }
}
Groovy
win = new javax.swing.JFrame()
win.defaultCloseOperation = javax.swing.WindowConstants.EXIT_ON_CLOSE
win.visible = true

(2011/07/12 追記) コメント欄でもっとGroovyぽいのを教えていただきました

new groovy.swing.SwingBuilder().frame(defaultCloseOperation: javax.swing.WindowConstants.EXIT_ON_CLOSE).show()
Scala
new scala.swing.MainFrame().visible = true

Microsoft .NETを使う場合

C#
class WindowCS {
	static void Main() {
		System.Windows.Forms.Application.Run(new System.Windows.Forms.Form());
	}
}
Visual Basic .NET
Class Hello
    Shared Sub Main()
        System.Windows.Forms.Application.Run(New System.Windows.Forms.Form())
    End Sub
End Class

(2011/07/12 追記)
new→Newの指摘を受け修正しました *1

C++/CLI
#using <system.dll>
#using <System.Windows.Forms.dll>
int main() {
	System::Windows::Forms::Application::Run(gcnew System::Windows::Forms::Form());
	return 0;
}
Tk使うとこんなん

Perl/Tk

use Tk;
MainLoop();

Python/Tk

if __name__ == '__main__':
    import Tkinter
    Tkinter.Tk().mainloop()

Ruby/Tk

if $0 == __FILE__
  require 'tk'
  Tk.mainloop()
end
ruby -r'tk' -e 'Tk.mainloop()'
おまけ:Win32API版
#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK WindowProcess(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
	switch (msg) {
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hWnd, msg, wp, lp);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR pCmdLine, int showCmd) {
	WNDCLASSEX wndClass;
	HWND hWnd;
	MSG msg;
	wndClass.cbSize = sizeof(wndClass);
	wndClass.style = CS_HREDRAW | CS_VREDRAW;
	wndClass.lpfnWndProc = WindowProcess;
	wndClass.hInstance = NULL;	//hInst;
	wndClass.lpszClassName = _T("Sample");
	wndClass.hIcon = NULL;	//(HICON) LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
	wndClass.hIconSm = NULL;	// wndClass.hIcon;
	wndClass.hCursor = NULL;	//(HCURSOR) LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
	wndClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	if (RegisterClassEx(&wndClass) == 0) {
		return 1;
	}
	hWnd = CreateWindow(
			wndClass.lpszClassName,
			_T("Sample"),
			WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			NULL,
			NULL,
			NULL,	// hInst
			NULL
	);
	if (hWnd == NULL) {
		return 2;
	}
	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);
	while (1) {
		BOOL ret = GetMessage(&msg, NULL, 0, 0);
		if (ret == 0 || ret == -1) {
			break;
		} else {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return 0;
}

*1:でもBasic自身は大文字/小文字の区別ないんですけどね。IDE勝手に補正してくるけど