Hello World
Hello World
一、简述
简单的Hello World程序。(时间久了就会忘,趁着还有印象先记下)
1、C语言: 控制台程序、有窗体的程序(API)
2、C++: 控制台程序、MFC窗体程序
3、QT: 无界面程序、有界面程序
4、Java: 无界面程序、有界面程序
5、html: 纯html程序、html+js(弹出窗体)程序
6、C\#: 控制台程序、winform窗体程序
7、汇编: 控制台程序、有窗体程序
二、代码
1、C语言
1.1 控制台(无图形界面)
HelloWorld.c文件:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello World!\n");
getchar();//等待一下
return 0;
}
1.2有图形界面
HelloWorld.c文件:
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, PSTR szCmdLine, int iCmdShow)
{
MessageBox(NULL, TEXT("Hello World!"), TEXT("Hello World"), MB_OK);
return 0;
}
或者是
HelloWorld.c文件
#include <windows.h>
int main(int argc, char *argv[])
{
MessageBox(NULL, TEXT("Hello World!"), TEXT("Hello World"), MB_OK);
return 0;
}
2、C++
2.1控制台
HelloWorld.cpp文件
#include <iostream>
int main(int argc, char *argv[])
{
std::cout<<"Hello World!\n";
std::cin.get();//等待一下
return 0;
}
2.2MFC界面
HelloWorld.cpp文件
#include <stdafx.h>
//自定义窗口类
class MyWnd :public CFrameWnd
{
public:
MyWnd();
protected:
void OnPaint();//窗体描绘
DECLARE_MESSAGE_MAP()//消息映射
};
MyWnd::MyWnd()
{
Create(NULL,TEXT("HelloWorld"));//创建一个窗体,标题为HelloWorld
}
void MyWnd::OnPaint()
{
CPaintDC dc(this);
dc.TextOut(10,10,TEXT("Hello World!"));
}
//消息映射
BEGIN_MESSAGE_MAP(MyWnd,CWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
class Hello :public CWinApp
{
public:
BOOL InitInstance();//重写
};
//唯一的应用程序类实例
Hello hello;
BOOL Hello::InitInstance()//初始化
{
m_pMainWnd = new MyWnd();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return true;
}
注:项目是通过Win32创建的,因为直接创建MFC项目,会有很多不需要的代码。
创建之后要用到MFC,需要设置MFC的使用(右键项目—》属性—》配置属性—》常规—》MFC的使用—》在共享DLL中使用MFC)
修改stdafx.h文件:将#include
3、QT
3.1非图形界面:
HelloWorld.pro文件
SOURCES += \
main.cpp
main.cpp
#include <QDebug>
int main(int argc,char* argv[])
{
qDebug()<<"Hello World\n";
return 0;
}
3.2图形界面:
HelloWorld.pro文件
SOURCES += \
main.cpp \
mywindows.cpp
QT += widgets gui
HEADERS += \
mywindows.h
mywindows.h文件
#ifndef MYWINDOWS_H
#define MYWINDOWS_H
#include <QMainWindow>
class MyWindows : public QMainWindow
{
Q_OBJECT
public:
explicit MyWindows(QWidget *parent = 0);
signals:
public slots:
};
#endif // MYWINDOWS_H
main.cpp文件
#include <QApplication>
#include "mywindows.h"
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
MyWindows *w = new MyWindows();
w->show();
return app.exec();
}
mywindows.cpp文件
#include "mywindows.h"
#include <QLabel>
MyWindows::MyWindows(QWidget *parent) :
QMainWindow(parent)
{
QLabel *label = new QLabel(this);//为窗体添加一个标签
label->setText("Hello World!");//设置标签的文本为 Hello World
}
4、Java
4.1无图形界面
Helloworld.java文件
public class Helloworld {
public static void main(String[] args) {
System.out.println("Hello World!\n");
}
}
4.2有图形界面
HelloWorld2.java文件
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class HelloWorld2 extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HelloWorld2 frame = new HelloWorld2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public HelloWorld2() {
setTitle("HelloWorld");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JLabel lblHelloWorld = new JLabel("Hello World!");
contentPane.add(lblHelloWorld, BorderLayout.NORTH);
}
}z
注:File—》New—》Java Project—》Other—》WindowsBuilder—》Swing Designer—》JFrame
(若没有WindowsBuilder则添加,)
1)、找到对应版本的windowbuilder:http://www.eclipse.org/windowbuilder/download.php
2)、右键“link”复制下载地址
3)、打开eclipse—>Help—>Install New Software
4)、下载
5、html+js
5.1html
HelloWorld.html文件
<!DOCTYPE html>
<html>
<head>
<title>HelloWorld</title>
</head>
<body>
Hello World!
</body>
</html>
5.2 js弹出窗体
HelloWorld.cpp文件
<!DOCTYPE html>
<html>
<head>
<title>HelloWorld</title>
</head>
<script type="text/javascript" >
function Hello()
{
alert("Hello World!");
}
</script>
<body>
<input type="button" value="HelloWorld" onclick="Hello()">
</body>
</html>
6、C# (CSharp)
6.1 控制台程序
Program.cs文件
namespace HelloWorld3
{
class Program
{
static void Main(string[] args)
{
System.Console.Write("Hello World!\n");
System.Console.ReadKey();//等待一下
}
}
}
6.2 winform窗体程序
Program.cs文件
using System;
using System.Windows.Forms;
namespace HelloWorld4
{
static class Program
{
/// 应用程序的主入口点。
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1.Designer.cs文件
namespace HelloWorld4
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "Form1";
this.Text = "HelloWorld";//窗体标题
this.ResumeLayout(false);
}
#endregion
}
}
Form1.cs文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace HelloWorld4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//实例化一个命令按钮
Label label = new Label();
//设置命令按钮的属性
label.Location = new Point(50, 50);
label.Size = new Size(80, 25);
label.Text = "Hello World!";
this.Controls.Add(label);
}
}
}
7、汇编
7.1 控制台程序
HelloWorld.asm文件
data segment ;数据段定义开始
str1 db 'Hello World!',13,10,'$'
;定义了一个字符串变量str1
;DB 字节数据类型
;13 CR 回车
;10 LF 换行
;$作为字符串的结束符
data ends ;数据段定义结束
code segment ;代码段定义开始
assume CS:code,DS:data ;指定对应的代码段、数据段
start: ;程序开始
mov ax,data ;将数据段地址保存到ds寄存器
mov ds,ax
lea dx,str1 ;lea 获取变量str1的首地址,将其存放到dx寄存器中
mov ah,09h
int 21h ;中断,根据ah寄存的值执行对应功能,09:将dx寄存器指向内容输出到屏幕
mov ah,4ch
int 21h ;调用4c中断功能,正常返回dos
code ends ;代码定义结束
end start ;程序结束
注:分号为注释符
7.2 窗体程序
Hello.asm文件
.386 ;指令集
.model flat,stdcall ;调用模式
option casemap:none ;是否区分大小写
;包含相关文件
include windows.inc
include gdi32.inc
includelib gdi32.lib
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;数据段
.data? ;变量声明
hInstance dd ?
hWinMain dd ?
.const;常量声明
szClassName db 'MyClass',0
szCptionMain db 'HelloWorld',0 ;窗口标题
szText db 'Hello World',0 ;内容
;代码段
.code
;函数过程
_ProcWinMain proc uses ebx edi esi ,hWnd,uMsg,wParam,lParam
local @stPs:PAINTSTRUCT
local @stRect:RECT
local @hDc
mov eax,uMsg
.if eax == WM_PAINT
invoke BeginPaint,hWnd,addr @stPs
mov @hDc,eax
invoke GetClientRect,hWnd,addr @stRect
invoke DrawText,@hDc,addr szText,-1,\
addr @stRect,\
DT_SINGLELINE or DT_CENTER or DT_VCENTER
invoke EndPaint,hWnd,addr @stPs
.elseif eax == WM_CLOSE
invoke DestroyWindow,hWinMain
invoke PostQuitMessage,NULL
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret
;_ProcWinMain函数结束
_ProcWinMain endp
; _WinMain子函数
_WinMain proc
local @stWndClass:WNDCLASSEX
local @stMsg:MSG
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
;注册窗口类
invoke LoadCursor,0,IDC_ARROW
mov @stWndClass.hCursor,eax
push hInstance
pop @stWndClass.hInstance
mov @stWndClass.cbSize,sizeof WNDCLASSEX
mov @stWndClass.style,CS_HREDRAW or CS_VREDRAW
mov @stWndClass.lpfnWndProc,offset _ProcWinMain
mov @stWndClass.hbrBackground,COLOR_WINDOW+1
mov @stWndClass.lpszClassName,offset szClassName
invoke RegisterClassEx,addr @stWndClass
;建立并显示窗口
invoke CreateWindowEx,WS_EX_CLIENTEDGE,\
offset szClassName, offset szCptionMain,\
WS_OVERLAPPEDWINDOW,\
100,100,600,400,\
NULL,NULL,hInstance,NULL
mov hWinMain,eax
invoke ShowWindow,hWinMain,SW_SHOWNORMAL
invoke UpdateWindow,hWinMain
;消息循环
.while TRUE
invoke GetMessage,addr @stMsg,NULL,0,0
.break .if eax==0
invoke TranslateMessage,addr @stMsg
invoke DispatchMessage,addr @stMsg
.endw
ret
;函数结束
_WinMain endp
;程序入口
start:
call _WinMain
invoke ExitProcess,NULL
end start
7.3第二种方式
Hello.asm文件
.386 ;指令集
.Model Flat, StdCall ;调用模式
Option Casemap :None ;区分大小写
include windows.inc
include user32.inc
include kernel32.inc
includeLib user32.lib
includeLib kernel32.lib
.data
str1 db "Hello World!",0 ;变量str1,用来存放内容,0:字符串结束符
str2 db "HelloWorld",0 ;变量str2,用来存放标题,0:字符串结束符
.CODE
START:
invoke MessageBox,0,offset str1,offset str2,0
invoke ExitProcess,0
END START
三、未完待续!
还没有评论,来说两句吧...