challenge ウィンドウの表示

画面中央に幅100ピクセル、高さ75ピクセルのウィンドウを表示してください。
タイトルには「こんにちは、GUI!」と表示してください。

使用するGUIライブラリに名前が付いているならば、それをタグでつけてください。(例えば「Swing」など。)

追記:100x75はあまりに小さすぎるようなので、小さすぎるせいでうまく行かない場合は400x300でもいいということにします。 このお題の意図は「小さいウィンドウを出せ」というわけではないので。

Posted feedbacks - Flatten

Nested Hidden
Squeak システムは GUI も自前なので、ホスト OS 向けにウインドウが開くわけではありませんが…(^_^;)。参考まで。
1
2
3
4
| window |
window := SystemWindow labelled: 'こんにちは、GUI!'.
window width: 100; height: 75; center: Display center.
window openAsIs

後々のためにも問題をチュートリアル編とアルゴリズム編に分けたほうがよいと思われますが、隊長、どうでしょう。

XAMLでGUI!
1
2
3
4
5
6
7
8
<Window x:Class="Window.Konnichiwa"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Doukaku" Width="100" Height="75" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow">
  <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock FontSize="8">こんにちは、GUI!</TextBlock>
  </StackPanel>
</Window>

おわ! お題を読み違えてた...。 すんません、書き直します。

お題にもタグをつけられるようにすればいいのかも?

VisualuRuby(仮称)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
require 'vr/vrcontrol'

w, h = 100, 75

x = (VRLocalScreen.width - w) / 2
y = (VRLocalScreen.height - h) / 2
x = x < 0 ? 0 : x
y = y < 0 ? 0 : y

frm = VRLocalScreen.newform
frm.move(x, y, w, h)
frm.caption = "こんにちは、GUI!"
frm.create.show

VRLocalScreen.messageloop

100x75だと「こんにち...」になっちゃうけど許して(笑)。
1
2
3
4
5
<Window x:Class="Window.Konnichiwa"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="こんにちは、GUI!" Width="100" Height="75" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow">
</Window>

IronPythonで参戦。100x75だと幅を指定しても100より大きくなってしまう(^^;) 回避方法あるのだろうか。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# -*- coding: utf-8 -*-
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Form, FormStartPosition, Application
from System.Drawing import Size

class HelloForm(Form):
    def __init__(self):
        self.Text = u'こんにちは、GUI'
        self.StartPosition = FormStartPosition.CenterScreen
        self.Size = Size(100, 75)

if __name__ == '__main__':
    Application.Run(HelloForm())

data スキームを使っているので、 IE では動きません。
1
open('data:text/plain;charset=utf8,\u3053\u3093\u306B\u3061\u306F\u3001GUI', '_blank','height=100,width=75');

data スキームを使わないバージョン
1
2
3
var d = open('about:blank', '_blank', 'height=100,width=75').document;
d.write('\u3053\u3093\u306B\u3061\u306F\u3001GUI');
d.close();

C# WindowsForms版
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Windows.Forms;

namespace Doukaku
{
	static class Konnichiwa
	{
		[STAThread]
		static void Main()
		{
			Form window = new Form();
			window.StartPosition = FormStartPosition.CenterScreen;
			window.FormBorderStyle = FormBorderStyle.FixedToolWindow;
			window.Width = 100;
			window.Height = 75;
			window.Text = "こんにちは、GUI!";
			window.ShowDialog();
		}
	}
}

なるほど。FormBorderStyleを指定すれば幅を100にできるのですね…。


	
1
2
3
4
5
6
7
8
9
import Graphics.UI.Gtk

main = do initGUI
          window <- windowNew
          windowSetTitle window "こんにちは、GUI!"
          windowSetDefaultSize window 100 75
          windowSetPosition window WinPosCenter
          widgetShowAll window
          mainGUI

erl -noshell -s hellogui hello
のように実行します.

Erlangの標準GUIツールのgsは日本語未対応のようです.
文字化けするので,タイトルは英語版で…
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
-module(hellogui).
-export([hello/0]).

hello() -> 
	GS = gs:start(),
	Win = gs:create(window, GS, [{width, 200}, {height, 75}, {title, "Hello, GUI!"}]),
	gs:config(Win, {map, true}),
	event_loop(Win). 

event_loop(Win) ->
	receive
		_Any -> event_loop(Win)
	end.

100x75は無理っぽいので400x300にて
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import javax.swing.JFrame;
import java.awt.Toolkit;
import java.awt.Dimension;

public class Window {
	
	public static void main(String args[]) {
		int width = 400;
		int height = 300;
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width, height);
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension d = toolkit.getScreenSize();
		frame.setLocation(((int)d.getWidth() - width) / 2, ((int)d.getHeight() - height) / 2);
		frame.setVisible(true);
	}
	
}

Scalaで。日本語アレなんでタイトルは英語です。
1
2
3
4
5
6
import javax.swing._
val f = new JFrame("Hello GUI!")
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
f.setSize(400, 300)
f.setLocationRelativeTo(null)
f.setVisible(true)

C++,FLTK
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <FL/Fl.H>
#include <FL/Fl_Window.H>

int main(int argc, char ** argv) {
    const int w = 400, h = 300;
    Fl_Window *window = new Fl_Window(w, h, "こんにちは、GUI!");
    window->end();
    window->resize((Fl::w() - w) / 2, (Fl::h() - h) / 2, w, h);
    window->show(argc, argv);
    return Fl::run();
}

pygtkはGUI楽でいいですよね。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# -*- coding: utf-8 -*-
import gtk

win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.connect("delete-event", gtk.main_quit)
win.set_title("こんにちわ GUI")
win.set_size_request(100, 75)
win.set_position(gtk.WIN_POS_CENTER)
win.show()
gtk.main()

awtでやっつけ
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class WindowOpener {
	public static void main(String args[]) {
		int width = 400;
		int height = 300;
		
		final Frame frame = new Frame("こんにちは、GUI!");
		frame.setSize(width, height);
		frame.setLocation(0, 0);
		frame.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					frame.dispose();
				}
			}
		);
		frame.setVisible(true);
	}
}

参考: http://www.strangelights.com/fsharp/wiki/default.aspx/FSharpWiki/InteractiveWindows.html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
open System.Drawing;;
open System.Windows.Forms;;

let form = new Form();;
form.Visible <- true;;
form.Size <- new Size(100,75);;

let rTextBox = new RichTextBox();;
form.Controls.Add( rTextBox );; 
rTextBox.Dock <- DockStyle.Fill;;
rTextBox.Text <- "こんにちは、GUI!";;

do Application.Run(form);; 

JRubyで。 日本語だせなかった...
1
2
3
4
5
6
require 'java'
f = javax.swing.JFrame.new('Hello,GUI!')
f.set_size(400,300)
d = java.awt.Toolkit.get_default_toolkit.get_screen_size
f.set_location(d.width / 2, d.height / 2)
f.show

Python/Tkinterで。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# coding: shift_jis

import Tkinter as Tk

root = Tk.Tk()
root.title(u"こんにちは、GUI!")

w = 400
h = 300

root.geometry("%dx%d+%d+%d" % (w, h,
    (root.winfo_screenwidth()  - w) / 2,
    (root.winfo_screenheight() - h) / 2))

root.mainloop()

VC++&MFCで。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <afxwin.h>

class CMainWindow : public CFrameWnd
{
public:
    CMainWindow()
    {
        CRect rect(0, 0, 400, 300);
        Create(NULL, _T("こんにちは、GUI!"), WS_OVERLAPPEDWINDOW, rect);
        CenterWindow();
    };
};

class CHelloGUIApp : public CWinApp
{
public:
    virtual BOOL InitInstance()
    {
        m_pMainWnd = new CMainWindow();
        m_pMainWnd->ShowWindow(m_nCmdShow);
        m_pMainWnd->UpdateWindow();
        return TRUE;
    }
};

CHelloGUIApp  helloGUIApp;

対抗してVC++&!MFCで。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <windows.h>
#define WINDCLASSNAME	L"HELLOWORLD2"
int g_nExit = 0;

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){
	switch( msg ){
	case WM_DESTROY:
	case WM_CLOSE:
		g_nExit = 1;
		return 0;
	}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow ){
	WNDCLASSEX hWndClass = {sizeof(WNDCLASSEX), CS_CLASSDC ,MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, WINDCLASSNAME, NULL};
	RegisterClassEx( &hWndClass );
	int w = 400 + GetSystemMetrics(SM_CXEDGE) + GetSystemMetrics(SM_CXBORDER) + GetSystemMetrics(SM_CXDLGFRAME);
	int h = 300 + GetSystemMetrics(SM_CYEDGE) + GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYDLGFRAME) + GetSystemMetrics(SM_CYCAPTION);
	HWND hWnd = CreateWindow( WINDCLASSNAME, L"こんにちわ、GUI!", WS_OVERLAPPEDWINDOW, 100, 100, w, h, GetDesktopWindow(), NULL, hWndClass.hInstance, NULL );
	ShowWindow(hWnd, SW_SHOWDEFAULT);
	UpdateWindow(hWnd);
	MSG msg;
	while(g_nExit==0){
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ){
			TranslateMessage( &msg );
			DispatchMessage( &msg);
		}
	}
	UnregisterClass( WINDCLASSNAME, hWndClass.hInstance );
	return 0;
}

既にあるけど
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Program
{
  static void Main()
  {
    Form dlg = new Form();
    dlg.Text = "こんにちは、GUI!";
    dlg.Size = new Size(400, 300);
    dlg.StartPosition = FormStartPosition.CenterScreen;
    dlg.ShowDialog();
  }
}

Perl/Tkで。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
use encoding 'cp932'; # windows
use Tk;
use constant {
    MW_WIDTH    => 400,
    MW_HEIGHT   => 300
};

my $mw = new Tk::MainWindow(
            -title => 'こんにちは、GUI!',
            -width => MW_WIDTH,
            -height => MW_HEIGHT);
my $x = int(($mw->screenwidth - MW_WIDTH) / 2);
my $y = int(($mw->screenheight - MW_HEIGHT) / 2);

$mw->geometry("+$x+$y");

Tk::MainLoop();

つまらない例ですがGtk+のC++バインドで。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <gtkmm.h>

int main(int argc, char *argv[])
{
	Gtk::Main kit(argc, argv);
	Gtk::Window window;
	window.set_title("こんにちは、GUI!");
	window.set_default_size(100, 175);
	Gtk::Main::run(window);
	
	return 0;
}

Simpleに。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import javax.swing.JFrame;

class HelloGUI extends JFrame {
    public HelloGUI() {
        super("こんにちは、GUI!");
        this.setSize(400, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public static void main(String[] args) {
        new HelloGUI().setVisible(true);
    }
}

単純なフレームだとサイズの指定ができないようです.
中身にダミーのオブジェクトを入れて大きくしています.

「こんにちは、GUI!」と表示させたかったのですが,Mathematicaのフロントエンドで
「、」を入れると自動的に「,」に変換されてしまうので,少し違う文字列になっています.
1
2
3
4
5
6
7
8
Needs["GUIKit`"];
GUIRunModal[ 
    Widget["Frame", {"title" -> "こんにちは, GUI!",
        Widget["TextPanel", {
            "preferredSize" -> Widget["Dimension", {"width" -> 200,
         "height" -> 100}],
            "text" -> ""}] 
        }]];

ウィンドウの表示だけだったら0行で書けるんですけどね。 ゲーム用のスクリプト言語はこのあたりが便利だ。
1
2
screen 0, 400, 300
mes "こんにちは、GUI!"

LTK (Tcl/Tk) ベースの GUI です。
1
2
3
4
5
(use-package :ltk)
(defun hello-1 ()
  (with-ltk ()
    (wm-title *tk* "こんにちは、GUI!")
    (set-geometry-wh *tk* 400 300)))

LispWorks の GUI ライブラリ CAPI です。
1
2
3
4
5
6
7
(capi:define-interface hello-2 ()
  ()
  (:default-initargs
   :width 400
   :height 300
   :title "こんにちは、GUI!"))
(capi:display (make-instance 'hello-2))

AllegroCL の GUI ライブラリ Common Graphics です。
1
2
3
(require :cg)
(cg:initialize-cg)
(cg:make-window :hello-3 :class 'cg:dialog :width 400 :height 300 :title "こんにちは、GUI!")

日本語が化ける問題点がありますが、Xlibを利用したウィンドウの表示です。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <X11/Xlib.h>

int main(int argc, char *argv[])
{
    Display *display;
    Window  window;
    char    *title = "こんにちは、GUI!";
    int     x_size = 400;
    int     y_size = 300;

    display = XOpenDisplay(NULL);
    window = XCreateSimpleWindow(display, DefaultRootWindow(display),
                                 DisplayWidth(display, 0)/2,
                                 DisplayHeight(display, 0)/2,
                                 x_size, y_size, 50,
                                 BlackPixel(display, 0),
                                 WhitePixel(display, 0));
    XSetStandardProperties(display, window, title, NULL,
                           None, argv, argc, NULL);
    XMapWindow(display, window);
    XFlush(display);

    getchar();
    XCloseDisplay(display);

    return 0;
}

Gauche+Gauche-glでやってみました。 でもタイトルはutf-8で渡しても化けるなあ…
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
;; -*- coding: utf-8 -*-
(use gl)
(use gl.glut)

(define (main args)
  (glut-init args)
  (glut-init-display-mode GLUT_SINGLE)
  (glut-init-window-size 100 75)
  (glut-create-window "こんにちは、GUI!")
  (gl-clear-color 0 0 0 0)
  (glut-display-func (lambda () (gl-clear GL_COLOR_BUFFER_BIT) (gl-flush)))
  (glut-main-loop)
  0)

R TclTkを使いました。
1
2
3
4
require(tcltk)
tt <- tktoplevel()
tkwm.geometry(tt, "100x75")
tkwm.title(tt, "こんにちは、GUI!")

wxPython始めました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# -*- coding: utf-8 -*-
import wx

class App(wx.App):
    def OnInit(self):
        frame = wx.Frame(parent=None, title=u'こんにちは、GUI!', size=(400, 300))
        frame.Center()
        frame.Show()
        return True
		
app = App()
app.MainLoop()

Windows版のSWI-prolog/XPCEです。ソースコードはUTF-16で保存して下さい。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
% file encoding: UTF-16
:- use_module(library(pce)).

hello :-
	new(D, dialog('Hello', size(400,100))),
	new(F, font('MS UI Gothic', modern, 20, 'charset(ansi)')),
	send(D, gap, size(400,100)),
	send(D, append, text('こんにちは、GUI!', font := F)),
	send(D, append, button(ok, message(D, return, @nil))),
	get(D, confirm, _),
	send(D,destroy).

【アルゴリズム自体ではなく、Prolog についての感想です】 50へぇ~。Prolog でもメッセージパッシングって、できるんですね。知りませんでした。なんだか、普通の手続き型(=副作用式の)言語のような使い方で、ちょっと不思議な感じもしますね。。。Prolog トラッキング機構って、ただの 逐次処理にも使えるっていうことですね。。。最後に、カット「!」なぞあれば、完全に逐次処理ですよね。。。。Prolog って思ってた以上にに使える言語のように思いました。名前空間(package)の仕組みとか、型チェックの機構とかあれば、もっと大規模なプログラミングにもガンガンつかえそう。。。


	
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
program window;

uses
    Forms;

var
    Form: TForm;

begin
    Application.Initialize;
    Application.CreateForm(TForm, Form);
    Form.Caption := 'こんにちは、GUI!';
    Form.BorderStyle := bsToolWindow;
    Form.SetBounds(0, 0, 100, 75);
    Form.Position := poScreenCenter;
    Application.Run;
end.