Hello World

爱被打了一巴掌 2022-05-25 00:55 482阅读 0赞

Hello World

一、简述

  1. 简单的Hello World程序。(时间久了就会忘,趁着还有印象先记下)
  2. 1C语言: 控制台程序、有窗体的程序(API)
  3. 2C++: 控制台程序、MFC窗体程序
  4. 3QT 无界面程序、有界面程序
  5. 4Java 无界面程序、有界面程序
  6. 5html: html程序、html+js(弹出窗体)程序
  7. 6C\# 控制台程序、winform窗体程序
  8. 7、汇编: 控制台程序、有窗体程序

二、代码

1、C语言

1.1 控制台(无图形界面)

20180504222323690

HelloWorld.c文件:

  1. #include <stdio.h>
  2. int main(int argc, char *argv[])
  3. {
  4. printf("Hello World!\n");
  5. getchar();//等待一下
  6. return 0;
  7. }

1.2有图形界面

20180505205532415

HelloWorld.c文件:

  1. #include <windows.h>
  2. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, PSTR szCmdLine, int iCmdShow)
  3. {
  4. MessageBox(NULL, TEXT("Hello World!"), TEXT("Hello World"), MB_OK);
  5. return 0;
  6. }

或者是

20180505205319417

HelloWorld.c文件

  1. #include <windows.h>
  2. int main(int argc, char *argv[])
  3. {
  4. MessageBox(NULL, TEXT("Hello World!"), TEXT("Hello World"), MB_OK);
  5. return 0;
  6. }

2、C++

2.1控制台

2018050422281660

HelloWorld.cpp文件

  1. #include <iostream>
  2. int main(int argc, char *argv[])
  3. {
  4. std::cout<<"Hello World!\n";
  5. std::cin.get();//等待一下
  6. return 0;
  7. }

2.2MFC界面

20180505111203990

HelloWorld.cpp文件

  1. #include <stdafx.h>
  2. //自定义窗口类
  3. class MyWnd :public CFrameWnd
  4. {
  5. public:
  6. MyWnd();
  7. protected:
  8. void OnPaint();//窗体描绘
  9. DECLARE_MESSAGE_MAP()//消息映射
  10. };
  11. MyWnd::MyWnd()
  12. {
  13. Create(NULL,TEXT("HelloWorld"));//创建一个窗体,标题为HelloWorld
  14. }
  15. void MyWnd::OnPaint()
  16. {
  17. CPaintDC dc(this);
  18. dc.TextOut(10,10,TEXT("Hello World!"));
  19. }
  20. //消息映射
  21. BEGIN_MESSAGE_MAP(MyWnd,CWnd)
  22. ON_WM_PAINT()
  23. END_MESSAGE_MAP()
  24. class Hello :public CWinApp
  25. {
  26. public:
  27. BOOL InitInstance();//重写
  28. };
  29. //唯一的应用程序类实例
  30. Hello hello;
  31. BOOL Hello::InitInstance()//初始化
  32. {
  33. m_pMainWnd = new MyWnd();
  34. m_pMainWnd->ShowWindow(m_nCmdShow);
  35. m_pMainWnd->UpdateWindow();
  36. return true;
  37. }

注:项目是通过Win32创建的,因为直接创建MFC项目,会有很多不需要的代码。

20180505110213342

创建之后要用到MFC,需要设置MFC的使用(右键项目—》属性—》配置属性—》常规—》MFC的使用—》在共享DLL中使用MFC)

20180505110753673

修改stdafx.h文件:将#include 改为#include

3、QT

3.1非图形界面:

2018050422335050

HelloWorld.pro文件

  1. SOURCES += \
  2. main.cpp

main.cpp

  1. #include <QDebug>
  2. int main(int argc,char* argv[])
  3. {
  4. qDebug()<<"Hello World\n";
  5. return 0;
  6. }

3.2图形界面:

20180504225203318

HelloWorld.pro文件

  1. SOURCES += \
  2. main.cpp \
  3. mywindows.cpp
  4. QT += widgets gui
  5. HEADERS += \
  6. mywindows.h

mywindows.h文件

  1. #ifndef MYWINDOWS_H
  2. #define MYWINDOWS_H
  3. #include <QMainWindow>
  4. class MyWindows : public QMainWindow
  5. {
  6. Q_OBJECT
  7. public:
  8. explicit MyWindows(QWidget *parent = 0);
  9. signals:
  10. public slots:
  11. };
  12. #endif // MYWINDOWS_H

main.cpp文件

  1. #include <QApplication>
  2. #include "mywindows.h"
  3. int main(int argc,char *argv[])
  4. {
  5. QApplication app(argc,argv);
  6. MyWindows *w = new MyWindows();
  7. w->show();
  8. return app.exec();
  9. }

mywindows.cpp文件

  1. #include "mywindows.h"
  2. #include <QLabel>
  3. MyWindows::MyWindows(QWidget *parent) :
  4. QMainWindow(parent)
  5. {
  6. QLabel *label = new QLabel(this);//为窗体添加一个标签
  7. label->setText("Hello World!");//设置标签的文本为 Hello World
  8. }

4、Java

4.1无图形界面

20180504230953349

Helloworld.java文件

  1. public class Helloworld {
  2. public static void main(String[] args) {
  3. System.out.println("Hello World!\n");
  4. }
  5. }

4.2有图形界面

20180504232452194

HelloWorld2.java文件

  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.border.EmptyBorder;
  6. import javax.swing.JLabel;
  7. public class HelloWorld2 extends JFrame {
  8. private JPanel contentPane;
  9. public static void main(String[] args) {
  10. EventQueue.invokeLater(new Runnable() {
  11. public void run() {
  12. try {
  13. HelloWorld2 frame = new HelloWorld2();
  14. frame.setVisible(true);
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. });
  20. }
  21. public HelloWorld2() {
  22. setTitle("HelloWorld");
  23. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. setBounds(100, 100, 450, 300);
  25. contentPane = new JPanel();
  26. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  27. contentPane.setLayout(new BorderLayout(0, 0));
  28. setContentPane(contentPane);
  29. JLabel lblHelloWorld = new JLabel("Hello World!");
  30. contentPane.add(lblHelloWorld, BorderLayout.NORTH);
  31. }
  32. }z

注:File—》New—》Java Project—》Other—》WindowsBuilder—》Swing Designer—》JFrame

(若没有WindowsBuilder则添加,)

1)、找到对应版本的windowbuilder:http://www.eclipse.org/windowbuilder/download.php

2)、右键“link”复制下载地址

20180505205812173

3)、打开eclipse—>Help—>Install New Software

20180505205926309

4)、下载

20180504235127577

5、html+js

5.1html

20180505112339848

HelloWorld.html文件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HelloWorld</title>
  5. </head>
  6. <body>
  7. Hello World!
  8. </body>
  9. </html>

5.2 js弹出窗体

20180505113008319

HelloWorld.cpp文件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HelloWorld</title>
  5. </head>
  6. <script type="text/javascript" >
  7. function Hello()
  8. {
  9. alert("Hello World!");
  10. }
  11. </script>
  12. <body>
  13. <input type="button" value="HelloWorld" onclick="Hello()">
  14. </body>
  15. </html>

6、C# (CSharp)

6.1 控制台程序

20180505121505549

Program.cs文件

  1. namespace HelloWorld3
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. System.Console.Write("Hello World!\n");
  8. System.Console.ReadKey();//等待一下
  9. }
  10. }
  11. }

6.2 winform窗体程序

20180505131917684

Program.cs文件

  1. using System;
  2. using System.Windows.Forms;
  3. namespace HelloWorld4
  4. {
  5. static class Program
  6. {
  7. /// 应用程序的主入口点。
  8. [STAThread]
  9. static void Main()
  10. {
  11. Application.EnableVisualStyles();
  12. Application.SetCompatibleTextRenderingDefault(false);
  13. Application.Run(new Form1());
  14. }
  15. }
  16. }

Form1.Designer.cs文件

  1. namespace HelloWorld4
  2. {
  3. partial class Form1
  4. {
  5. private System.ComponentModel.IContainer components = null;
  6. protected override void Dispose(bool disposing)
  7. {
  8. if (disposing && (components != null))
  9. {
  10. components.Dispose();
  11. }
  12. base.Dispose(disposing);
  13. }
  14. #region Windows 窗体设计器生成的代码
  15. private void InitializeComponent()
  16. {
  17. this.SuspendLayout();
  18. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  19. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  20. this.ClientSize = new System.Drawing.Size(284, 262);
  21. this.Name = "Form1";
  22. this.Text = "HelloWorld";//窗体标题
  23. this.ResumeLayout(false);
  24. }
  25. #endregion
  26. }
  27. }

Form1.cs文件

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace HelloWorld4
  7. {
  8. public partial class Form1 : Form
  9. {
  10. public Form1()
  11. {
  12. InitializeComponent();
  13. //实例化一个命令按钮
  14. Label label = new Label();
  15. //设置命令按钮的属性
  16. label.Location = new Point(50, 50);
  17. label.Size = new Size(80, 25);
  18. label.Text = "Hello World!";
  19. this.Controls.Add(label);
  20. }
  21. }
  22. }

7、汇编

7.1 控制台程序

20180505210029421

HelloWorld.asm文件

  1. data segment ;数据段定义开始
  2. str1 db 'Hello World!',13,10,'$'
  3. ;定义了一个字符串变量str1
  4. ;DB 字节数据类型
  5. ;13 CR 回车
  6. ;10 LF 换行
  7. ;$作为字符串的结束符
  8. data ends ;数据段定义结束
  9. code segment ;代码段定义开始
  10. assume CS:code,DS:data ;指定对应的代码段、数据段
  11. start: ;程序开始
  12. mov ax,data ;将数据段地址保存到ds寄存器
  13. mov ds,ax
  14. lea dx,str1 ;lea 获取变量str1的首地址,将其存放到dx寄存器中
  15. mov ah,09h
  16. int 21h ;中断,根据ah寄存的值执行对应功能,09:将dx寄存器指向内容输出到屏幕
  17. mov ah,4ch
  18. int 21h ;调用4c中断功能,正常返回dos
  19. code ends ;代码定义结束
  20. end start ;程序结束

注:分号为注释符

7.2 窗体程序

20180505210142131

Hello.asm文件

  1. .386 ;指令集
  2. .model flat,stdcall ;调用模式
  3. option casemap:none ;是否区分大小写
  4. ;包含相关文件
  5. include windows.inc
  6. include gdi32.inc
  7. includelib gdi32.lib
  8. include user32.inc
  9. includelib user32.lib
  10. include kernel32.inc
  11. includelib kernel32.lib
  12. ;数据段
  13. .data? ;变量声明
  14. hInstance dd ?
  15. hWinMain dd ?
  16. .const;常量声明
  17. szClassName db 'MyClass',0
  18. szCptionMain db 'HelloWorld',0 ;窗口标题
  19. szText db 'Hello World',0 ;内容
  20. ;代码段
  21. .code
  22. ;函数过程
  23. _ProcWinMain proc uses ebx edi esi ,hWnd,uMsg,wParam,lParam
  24. local @stPs:PAINTSTRUCT
  25. local @stRect:RECT
  26. local @hDc
  27. mov eax,uMsg
  28. .if eax == WM_PAINT
  29. invoke BeginPaint,hWnd,addr @stPs
  30. mov @hDc,eax
  31. invoke GetClientRect,hWnd,addr @stRect
  32. invoke DrawText,@hDc,addr szText,-1,\
  33. addr @stRect,\
  34. DT_SINGLELINE or DT_CENTER or DT_VCENTER
  35. invoke EndPaint,hWnd,addr @stPs
  36. .elseif eax == WM_CLOSE
  37. invoke DestroyWindow,hWinMain
  38. invoke PostQuitMessage,NULL
  39. .else
  40. invoke DefWindowProc,hWnd,uMsg,wParam,lParam
  41. ret
  42. .endif
  43. xor eax,eax
  44. ret
  45. ;_ProcWinMain函数结束
  46. _ProcWinMain endp
  47. ; _WinMain子函数
  48. _WinMain proc
  49. local @stWndClass:WNDCLASSEX
  50. local @stMsg:MSG
  51. invoke GetModuleHandle,NULL
  52. mov hInstance,eax
  53. invoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
  54. ;注册窗口类
  55. invoke LoadCursor,0,IDC_ARROW
  56. mov @stWndClass.hCursor,eax
  57. push hInstance
  58. pop @stWndClass.hInstance
  59. mov @stWndClass.cbSize,sizeof WNDCLASSEX
  60. mov @stWndClass.style,CS_HREDRAW or CS_VREDRAW
  61. mov @stWndClass.lpfnWndProc,offset _ProcWinMain
  62. mov @stWndClass.hbrBackground,COLOR_WINDOW+1
  63. mov @stWndClass.lpszClassName,offset szClassName
  64. invoke RegisterClassEx,addr @stWndClass
  65. ;建立并显示窗口
  66. invoke CreateWindowEx,WS_EX_CLIENTEDGE,\
  67. offset szClassName, offset szCptionMain,\
  68. WS_OVERLAPPEDWINDOW,\
  69. 100,100,600,400,\
  70. NULL,NULL,hInstance,NULL
  71. mov hWinMain,eax
  72. invoke ShowWindow,hWinMain,SW_SHOWNORMAL
  73. invoke UpdateWindow,hWinMain
  74. ;消息循环
  75. .while TRUE
  76. invoke GetMessage,addr @stMsg,NULL,0,0
  77. .break .if eax==0
  78. invoke TranslateMessage,addr @stMsg
  79. invoke DispatchMessage,addr @stMsg
  80. .endw
  81. ret
  82. ;函数结束
  83. _WinMain endp
  84. ;程序入口
  85. start:
  86. call _WinMain
  87. invoke ExitProcess,NULL
  88. end start

7.3第二种方式

20180505210304626

Hello.asm文件

  1. .386 ;指令集
  2. .Model Flat, StdCall ;调用模式
  3. Option Casemap :None ;区分大小写
  4. include windows.inc
  5. include user32.inc
  6. include kernel32.inc
  7. includeLib user32.lib
  8. includeLib kernel32.lib
  9. .data
  10. str1 db "Hello World!",0 ;变量str1,用来存放内容,0:字符串结束符
  11. str2 db "HelloWorld",0 ;变量str2,用来存放标题,0:字符串结束符
  12. .CODE
  13. START:
  14. invoke MessageBox,0,offset str1,offset str2,0
  15. invoke ExitProcess,0
  16. END START

三、未完待续!

发表评论

表情:
评论列表 (有 0 条评论,482人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Hello World

    Hello World 一、简述          简单的Hello World程序。(时间久了就会忘,趁着还有印象先记下)     1、C语言:  控制台程序、有窗体

    相关 Hello World

    这是我的第一篇博客,虽然是第一次写博客,但是之前也在微信公众号上写过一些文章(虽然没有阅读量),或多或少对自己产生了一定的帮助。 这些天会考虑将其中一些较为有意义的