Hello world/Grafici
Aspetto
Di seguito sono illustrati esempi di Hello world eseguiti su interfacce grafiche.
display dialog "hello, world"
MsgBox(0, "hello, world", "hello, world")
C per GTK+
[modifica | modifica sorgente]#include <gtk/gtk.h>
void hello(GtkWidget* widget, gpointer data)
{
g_print("Hello, World!\n");
}
int main(int argc, char** argv)
{
GtkWidget* window;
GtkWidget* button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
button = gtk_button_new_with_label("Hello, World!");
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(hello), NULL);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show(window);
gtk_widget_show(button);
gtk_main();
return 0;
}
C++ per FOX
[modifica | modifica sorgente]#include "fx.h"
int main(int argc,char **argv)
{
FXApp app("MyApp","Me");
app.init(argc,argv);
app.create();
FXMessageBox::information(&app,MBOX_OK,"Message","hello, world");
return app.run();
}
C++ per GTK+
[modifica | modifica sorgente]#include <iostream>
#include <gtkmm/main.h>
#include <gtkmm/button.h>
#include <gtkmm/window.h>
using namespace std;
class HelloWorld : public Gtk::Window {
public:
HelloWorld();
virtual ~HelloWorld();
protected:
Gtk::Button m_button;
virtual void on_button_clicked();
};
HelloWorld::HelloWorld()
: m_button("hello, world") {
set_border_width(10);
m_button.signal_clicked().connect(sigc::mem_fun(*this,
&HelloWorld::on_button_clicked));
add(m_button);
m_button.show();
}
HelloWorld::~HelloWorld() {}
void HelloWorld::on_button_clicked() {
cout << "hello, world" << endl;
}
int main (int argc, char *argv[]) {
Gtk::Main kit(argc, argv);
HelloWorld HelloWorld;
Gtk::Main::run(HelloWorld);
return 0;
}
C++ con Qt
[modifica | modifica sorgente]#include <qapplication.h>
#include <qpushbutton.h>
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QPushButton Hello( "hello, world", 0 );
Hello.resize( 100, 30 );
a.setMainWidget( &Hello );
Hello.show();
return a.exec();
}
--
Versione con una MessageBox:
namespace Hello_World
{
using System;
using System.Windows.Forms;
public class HelloWorld
{
public static void Main()
{
MessageBox.Show("Hello, World!");
}
}
}
Versione con una finestra generica:
namespace Hello_World
{
using System;
using System.Windows.Forms;
using System.Drawing;
public class HelloWorld : Form
{
public static void Main()
{
new HelloWorld();
}
public HelloWorld()
{
this.Bounds = new Rectangle(300,300,300,300);
Label l1 = new Label("Hello World !");
l1.Visible = !false;
this.Controls.Add(l1);
}
}
}
program window WINDOW('Hello World'),AT(,,300,200),STATUS,SYSTEM,GRAY,DOUBLE,AUTO END code open(window) show(10,10,'Hello World') accept end close(window)
program HelloWorld;
uses Dialogs;
begin
ShowMessage('hello, world');
end.
program UnSaludo;
{$mode objfpc}{$H+}
uses
Interfaces, Forms;
begin
application.MessageBox('Hola mundo', '', 0) ;
end.
Nella Variante VDP:
module helloworld procedure Main Message("hello, world") endproc
o pure
edit1.text:='Hello World';
PUBLIC SUB Form_Open() Message.Info("hello, world") END
- AWT:
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class HelloWorldFenster extends Frame {
public HelloWorldFenster() {
super("hello, world");
Label HelloWorldLabel = new Label("hello, world");
add(HelloWorldLabel);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(false);
setLocation(350, 320);
setSize(160, 60);
setVisible(true);
}
public static void main(String[] args) {
new HelloWorldFenster();
}
}
import javax.swing.JFrame;
import javax.swing.JLabel;
public class HelloWorld extends JFrame {
public HelloWorld() {
super("hello, world");
JLabel HelloWorldLabel = new JLabel("hello, world");
getContentPane().add(HelloWorldLabel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocation(350, 320);
setSize(160, 60);
setVisible(true);
}
public static void main(String[] args) {
new HelloWorld();
}
}
(alert "hello, world")
MessageRequester("","Hello World")
plot.new() text(x = 0.5, y = 0.5, labels = "hello, world")
R, library tcltk
[modifica | modifica sorgente]library(tcltk) tt <- tktoplevel() label.widget <- tklabel(tt, text="Hello, World!") tkpack(label.widget)
label .label1 -text "Hello World" pack .label1
Versione corta:
pack [label .label1 -text "Hello World"]
Turbo Pascal 7.0 (con Turbo Vision), per DOS
[modifica | modifica sorgente]Versione con MessageBox
program Hello; uses App, MsgBox; type THelloApplication = object(TApplication) constructor Init; procedure DoAboutBox; end; constructor THelloApplication.Init; begin inherited Init; DoAboutBox; end; procedure THelloApplication.DoAboutBox; var Res: Word; begin Res := MessageBox('hello, world', nil, mfInformation); end; var HelloApplication: THelloApplication; begin HelloApplication.Init; HelloApplication.Run; HelloApplication.Done; end.
Versione con Window ed ereditarietà.
program Hello; uses App, Dialogs, Objects, Views; type PHelloWindow = ^THelloWindow; THelloWindow = object(TWindow) constructor Init(var Bounds: TRect; ATitle: TTitleStr; ANumber: Integer); end; THelloApplication = object(TApplication) constructor Init; end; constructor THelloWindow.Init(var Bounds: TRect; ATitle: TTitleStr; ANumber: Integer); var R: TRect; begin inherited Init(Bounds, ATitle, ANumber); R.Assign(2, 2, 37, 3); Insert(New(PStaticText, Init(R, 'hello, world'))); end; constructor THelloApplication.Init; var R: TRect; begin inherited Init; R.Assign(3, 3, 50, 14); InsertWindow(New(PHelloWindow, Init(R, 'Hello world program', 1))); end; var HelloApplication: THelloApplication; begin HelloApplication.Init; HelloApplication.Run; HelloApplication.Done; end.
MessageBox.Show("hello, world")
MsgBox "hello, world"
import waba.ui.*;
import waba.fx.*;
public class HelloWorld extends MainWindow
{
public void onPaint(Graphics g)
{
g.setColor(0, 0, 0);
g.drawText("hello, world", 0, 0);
}
}
Windows API (in Borland Pascal)
[modifica | modifica sorgente]{Compilabile con "BPC -CW HELLO.PAS"; il programma eseguibile è per Windows 16 bit e pesa 2048 byte.}
program Hello;
{$M 8192, 8192}
uses WinTypes, WinProcs;
const
ClassName = 'PASCLASS32';
var
Message : TMsg;
Paint : TPaintStruct;
Window : HWnd;
WindowClass : TWndClass;
function WndProc(Window: HWnd; Message, WParam: Word; LParam: Longint): Longint; export;
var
DC : HDC;
begin
WndProc := 0;
case Message of
WM_DESTROY:
begin
PostQuitMessage(0);
Exit;
end;
WM_PAINT:
begin
DC := BeginPaint(Window, Paint);
TextOut(DC, 5, 5, 'hello, world', 12);
EndPaint(Window, Paint);
end;
end;
WndProc := DefWindowProc(Window, Message, WParam, LParam);
end;
begin
WindowClass.style := CS_HREDRAW + CS_VREDRAW + CS_GLOBALCLASS;
WindowClass.lpfnWndProc := @WndProc;
WindowClass.cbClsExtra := 0;
WindowClass.cbWndExtra := 0;
WindowClass.hInstance := HInstance;
WindowClass.hIcon := LoadIcon(0, IDI_APPLICATION);
WindowClass.hCursor := LoadCursor(0, IDC_ARROW);
WindowClass.hbrBackground := GetStockObject(WHITE_BRUSH);
WindowClass.lpszMenuName := ClassName;
WindowClass.lpszClassName := ClassName;
RegisterClass(WindowClass);
Window := CreateWindowEx(
0, { extra style }
ClassName, { Class name }
'Win32 Pascal Program', { Title string }
WS_OVERLAPPEDWINDOW, { Style }
CW_USEDEFAULT, { x }
CW_USEDEFAULT, { y }
CW_USEDEFAULT, { width }
CW_USEDEFAULT, { height }
0, { parent hwnd }
0, { menu }
HInstance, { HInstance }
nil { lpParam }
);
ShowWindow(Window, SW_SHOWNORMAL);
UpdateWindow(Window);
while GetMessage(Message, 0, 0, 0) do
begin
TranslateMessage(Message);
DispatchMessage(Message);
end;
Halt(Message.WParam);
end.
Windows API (in Borland Turbo Assembler)
[modifica | modifica sorgente]; Modalità MASM-compatibile, compilabile con i comandi
; TASM32 /ml HELLO.ASM
; TLINK32 /Tpe /aa /c /x HELLO.OBJ, HELLO.EXE, , IMPORT32.LIB, HELLO.DEF
; il file risultante è per Windows 32 bit e pesa 8192 byte.
.386
LOCALS
JUMPS
.MODEL FLAT, STDCALL
INCLUDE WIN32.INC
EXTRN BeginPaint:PROC
EXTRN CreateWindowExA:PROC
EXTRN DefWindowProcA:PROC
EXTRN DispatchMessageA:PROC
EXTRN EndPaint:PROC
EXTRN ExitProcess:PROC
EXTRN GetMessageA:PROC
EXTRN GetModuleHandleA:PROC
EXTRN GetStockObject:PROC
EXTRN InvalidateRect:PROC
EXTRN LoadCursorA:PROC
EXTRN LoadIconA:PROC
EXTRN PostQuitMessage:PROC
EXTRN RegisterClassA:PROC
EXTRN ShowWindow:PROC
EXTRN TextOutA:PROC
EXTRN TranslateMessage:PROC
EXTRN UpdateWindow:PROC
CreateWindowEx EQU <CreateWindowExA>
DefWindowProc EQU <DefWindowProcA>
DispatchMessage EQU <DispatchMessageA>
GetMessage EQU <GetMessageA>
GetModuleHandle EQU <GetModuleHandleA>
LoadCursor EQU <LoadCursorA>
LoadIcon EQU <LoadIconA>
RegisterClass EQU <RegisterClassA>
TextOut EQU <TextOutA>
.DATA
TitleName DB "Win32 Assembly Program", 0
ClassName DB "ASMCLASS32", 0
Hello DB "hello, world"
HelloLength EQU 12
.DATA?
HInstance DD ?
Message MSGSTRUCT <?>
Paint PAINTSTRUCT <?>
Window DD ?
WindowClass WNDCLASS <?>
.CODE
WndProc PROC USES EBX EDI ESI, @@Window:DWORD, @@Message:DWORD, @@WParam:DWORD, @@LParam:DWORD
LOCAL @@DC:DWORD
CMP @@Message, WM_DESTROY
JE @@Destroy
CMP @@Message, WM_PAINT
JE @@Paint
JMP @@DefWndProc
@@Destroy:
PUSH 0
CALL PostQuitMessage
MOV EAX, 0
JMP @@Finish
@@Paint:
PUSH OFFSET Paint
PUSH @@Window
CALL BeginPaint
MOV @@DC, EAX
PUSH HelloLength
PUSH OFFSET Hello
PUSH 5
PUSH 5
PUSH @@DC
CALL TextOut
PUSH OFFSET Paint
PUSH @@Window
CALL EndPaint
MOV EAX, 0
JMP @@Finish
@@DefWndProc:
PUSH @@LParam
PUSH @@WParam
PUSH @@Message
PUSH @@Window
CALL DefWindowProc
JMP @@Finish
@@Finish:
RET
WndProc ENDP
PUBLIC WndProc
Begin:
PUSH 0
CALL GetModuleHandle
MOV HInstance, EAX
MOV WindowClass.clsStyle, CS_HREDRAW + CS_VREDRAW + CS_GLOBALCLASS
MOV WindowClass.clsLpfnWndProc, OFFSET WndProc
MOV WindowClass.clsCbClsExtra, 0
MOV WindowClass.clsCbWndExtra, 0
MOV EAX, HInstance
MOV WindowClass.clsHInstance, EAX
PUSH IDI_APPLICATION
PUSH 0
CALL LoadIcon
MOV WindowClass.clsHIcon, EAX
PUSH IDC_ARROW
PUSH 0
CALL LoadCursor
MOV WindowClass.clsHCursor, EAX
PUSH WHITE_BRUSH
CALL GetStockObject
MOV WindowClass.clsHbrBackground, EAX
MOV WindowClass.clsLpszMenuName, OFFSET ClassName
MOV WindowClass.clsLpszClassName, OFFSET ClassName
PUSH OFFSET WindowClass
CALL RegisterClass
PUSH 0
PUSH HInstance
PUSH 0
PUSH 0
PUSH CW_USEDEFAULT
PUSH CW_USEDEFAULT
PUSH CW_USEDEFAULT
PUSH CW_USEDEFAULT
PUSH WS_OVERLAPPEDWINDOW
PUSH OFFSET TitleName
PUSH OFFSET ClassName
PUSH 0
CALL CreateWindowEx
MOV Window, EAX
PUSH SW_SHOWNORMAL
PUSH Window
CALL ShowWindow
PUSH Window
CALL UpdateWindow
MessageLoop:
PUSH 0
PUSH 0
PUSH 0
PUSH OFFSET Message
CALL GetMessage
CMP AX, 0
JE EndLoop
PUSH OFFSET Message
CALL TranslateMessage
PUSH OFFSET Message
CALL DispatchMessage
JMP MessageLoop
EndLoop:
PUSH Message.msWPARAM
CALL ExitProcess
ENDS
END Begin
file HELLO.DEF:
NAME HELLO
DESCRIPTION 'Assembly Win32 Program'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
EXETYPE WINDOWS
HEAPSIZE 8192
STACKSIZE 8192
EXPORTS WndProc
Windows API (in Borland Turbo Assembler IDEAL mode syntax)
[modifica | modifica sorgente]COMMENT $
Modalità MASM-compatibile, compilabile con i comandi
TASM32 /ml HELLO.ASM
TLINK32 /Tpe /aa /c /x HELLO.OBJ, HELLO.EXE, , IMPORT32.LIB, HELLO.DEF
il file risultante è per Windows 32 bit e pesa 8192 byte.
IMPORTANTE: la versione 5.0 del TASM32.EXE produce un errore in fase di compilazione,
è necessaria una versione aggiornata
$
IDEAL
P386
LOCALS
JUMPS
MODEL FLAT, STDCALL
EXTRN BeginPaint:PROC
EXTRN CreateWindowExA:PROC
EXTRN DefWindowProcA:PROC
EXTRN DispatchMessageA:PROC
EXTRN EndPaint:PROC
EXTRN ExitProcess:PROC
EXTRN GetMessageA:PROC
EXTRN GetModuleHandleA:PROC
EXTRN GetStockObject:PROC
EXTRN InvalidateRect:PROC
EXTRN LoadCursorA:PROC
EXTRN LoadIconA:PROC
EXTRN PostQuitMessage:PROC
EXTRN RegisterClassA:PROC
EXTRN ShowWindow:PROC
EXTRN TextOutA:PROC
EXTRN TranslateMessage:PROC
EXTRN UpdateWindow:PROC
CreateWindowEx EQU <CreateWindowExA>
DefWindowProc EQU <DefWindowProcA>
DispatchMessage EQU <DispatchMessageA>
GetMessage EQU <GetMessageA>
GetModuleHandle EQU <GetModuleHandleA>
LoadCursor EQU <LoadCursorA>
LoadIcon EQU <LoadIconA>
RegisterClass EQU <RegisterClassA>
TextOut EQU <TextOutA>
WHITE_BRUSH = 0
CS_VREDRAW = 0001h
CS_HREDRAW = 0002h
CS_GLOBALCLASS = 4000h
CW_USEDEFAULT = 8000h
IDC_ARROW = 32512
IDI_APPLICATION = 32512
SW_SHOWNORMAL = 1
WM_DESTROY = 0002h
WM_PAINT = 000Fh
WS_OVERLAPPED = 000000000h
WS_CAPTION = 000C00000h
WS_SYSMENU = 000080000h
WS_THICKFRAME = 000040000h
WS_MINIMIZEBOX = 000020000h
WS_MAXIMIZEBOX = 000010000h
WS_OVERLAPPEDWINDOW = WS_OVERLAPPED OR WS_CAPTION OR WS_SYSMENU OR WS_THICKFRAME OR WS_MINIMIZEBOX OR WS_MAXIMIZEBOX
STRUC TPoint
x DW ?
y DW ?
ENDS TPoint
STRUC TRect
left DW ?
top DW ?
right DW ?
bottom DW ?
ENDS TRect
STRUC TMsg
hwnd DD ?
message DD ?
wParam DD ?
lParam DD ?
time DD ?
pt TPoint <?>
ENDS TMsg
STRUC TPaintStruct
hdc DD ?
fErase DD ?
rcPaint TRect <?>
fRestore DD ?
fIncUpdate DD ?
rgbReserved DB 16 dup(?)
ENDS TPaintStruct
STRUC TWndClass
style DD ?
lpfnWndProc DD ?
cbClsExtra DD ?
cbWndExtra DD ?
hInstance DD ?
hIcon DD ?
hCursor DD ?
hbrBackground DD ?
lpszMenuName DD ?
lpszClassName DD ?
ENDS TWndClass
DATASEG
TitleName DB "Win32 Assembly Program", 0
ClassName DB "ASMCLASS32", 0
Hello DB "hello, world"
HelloLength EQU 12
UDATASEG
HInstance DD ?
Message TMsg <?>
Paint TPaintStruct <?>
Window DD ?
WindowClass TWndClass <?>
CODESEG
PROC WndProc
ARG @@Window:DWORD, @@Message:DWORD, @@WParam:DWORD, @@LParam:DWORD
USES EBX, EDI, ESI
LOCAL @@DC:DWORD
CMP [@@Message], WM_DESTROY
JE @@Destroy
CMP [@@Message], WM_PAINT
JE @@Paint
JMP @@DefWndProc
@@Destroy:
PUSH 0
CALL PostQuitMessage
MOV EAX, 0
JMP @@Finish
@@Paint:
PUSH OFFSET Paint
PUSH [@@Window]
CALL BeginPaint
MOV [@@DC], EAX
PUSH HelloLength
PUSH OFFSET Hello
PUSH 5
PUSH 5
PUSH [@@DC]
CALL TextOut
PUSH OFFSET Paint
PUSH [@@Window]
CALL EndPaint
MOV EAX, 0
JMP @@Finish
@@DefWndProc:
PUSH [@@LParam]
PUSH [@@WParam]
PUSH [@@Message]
PUSH [@@Window]
CALL DefWindowProc
JMP @@Finish
@@Finish:
RET
ENDP WndProc
PUBLIC WndProc
Begin:
PUSH 0
CALL GetModuleHandle
MOV [HInstance], EAX
MOV [WindowClass.style], CS_HREDRAW + CS_VREDRAW + CS_GLOBALCLASS
MOV [WindowClass.lpfnWndProc], OFFSET WndProc
MOV [WindowClass.cbClsExtra], 0
MOV [WindowClass.cbWndExtra], 0
MOV EAX, [HInstance]
MOV [WindowClass.hInstance], EAX
PUSH IDI_APPLICATION
PUSH 0
CALL LoadIcon
MOV [WindowClass.hIcon], EAX
PUSH IDC_ARROW
PUSH 0
CALL LoadCursor
MOV [WindowClass.hCursor], EAX
PUSH WHITE_BRUSH
CALL GetStockObject
MOV [WindowClass.hbrBackground], EAX
MOV [WindowClass.lpszMenuName], OFFSET ClassName
MOV [WindowClass.lpszClassName], OFFSET ClassName
PUSH OFFSET WindowClass
CALL RegisterClass
PUSH 0 ; lpParam
PUSH [HInstance] ; HInstance
PUSH 0 ; menu
PUSH 0 ; parent hwnd
PUSH CW_USEDEFAULT ; height
PUSH CW_USEDEFAULT ; width
PUSH CW_USEDEFAULT ; y
PUSH CW_USEDEFAULT ; x
PUSH WS_OVERLAPPEDWINDOW ; Style
PUSH OFFSET TitleName ; Title string
PUSH OFFSET ClassName ; Class name
PUSH 0 ; extra style
CALL CreateWindowEx
MOV [Window], EAX
PUSH SW_SHOWNORMAL
PUSH [Window]
CALL ShowWindow
PUSH [Window]
CALL UpdateWindow
MessageLoop:
PUSH 0
PUSH 0
PUSH 0
PUSH OFFSET Message
CALL GetMessage
CMP AX, 0
JE EndLoop
PUSH OFFSET Message
CALL TranslateMessage
PUSH OFFSET Message
CALL DispatchMessage
JMP MessageLoop
EndLoop:
PUSH [Message.wParam]
CALL ExitProcess
ENDS
END Begin
File HELLO.DEF:
NAME HELLO
DESCRIPTION 'Assembly Win32 Program'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
EXETYPE WINDOWS
HEAPSIZE 8192
STACKSIZE 8192
EXPORTS WndProc
Windows API (in C)
[modifica | modifica sorgente] #include <windows.h>
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
char szClassName[] = "CCLASS32";
HINSTANCE hInstance;
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wincl;
hInstance = hInst;
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.style = 0;
wincl.hInstance = hInstance;
wincl.lpszClassName = szClassName;
wincl.lpszMenuName = NULL; //No menu
wincl.lpfnWndProc = WindowProcedure;
wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); //Color of the window
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION); //EXE icon
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //Small program icon
wincl.hCursor = LoadCursor(NULL, IDC_ARROW); //Cursor
if (!RegisterClassEx(&wincl))
return 0;
hwnd = CreateWindowEx(0, //No extended window styles
szClassName, //Class name
"Win32 C Program", //Window caption
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, //Let Windows decide the left and top positions of the window
120, 50, //Width and height of the window,
NULL, NULL, hInstance, NULL);
//Make the window visible on the screen
ShowWindow(hwnd, nCmdShow);
//Run the message loop
BOOL bRet;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 15, 3, "hello, world", 13);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
X Window
[modifica | modifica sorgente]xmessage hello, world!
Zenity
[modifica | modifica sorgente]zenity --info --text='hello, world'