Hello World是最简单的入门程序。这里将演示多个HelloWorld
程序。1、使用
Basic
构建2、使用
VC++构建3、使用
C#构建:
1、使用BASIC
构建HelloWorld
的控制台程序
新建/项目/其它语言/
Module Module1
Sub
Main()
Console.Write("hello
world")
End Sub
End Module
2、使用BASIC
创建窗体应用程序helloworld
Public Class
Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles
Button1.Click
Label1.Text = "hello
world"
Button1.Hide()
End Sub
End Class
3、使用C#创建控制台程序helloworld
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace helloWorld
{
class Program
{
static void Main(string[] args)
{
Console.Write("hello world");
}
}
}
4、使用C#创建窗口应用程序helloworld
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace helloWorld
{
public partial class
Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "hello
world";
}
}
}
5、使用C++的CLR
创建控制台程序helloworld
// helloWorld.cpp:
主项目文件。
#include "stdafx.h"
using namespace
System;
int main(array<System::String
^> ^args)
{
Console::WriteLine(L"Hello
World");
return 0;
}
6、使用C++创建Win32
控制台程序helloworld
// helloWorld.cpp :
定义控制台应用程序的入口点。
//
#include "stdafx.h"
int _tmain(int argc,
_TCHAR* argv[])
{
printf("hello
world");
return 0;
}
7、使用C++创建CLR
窗体程序helloworld
#pragma once
namespace helloWorld {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1
摘要
///
///
警告:
如果更改此类的名称,则需要更改
///
与此类所依赖的所有.resx
文件关联的托管资源编译器工具的
///
“资源文件名”属性。否则,
///
设计器将不能与此窗体的关联
///
本地化资源正确交互。
/// </summary>
public ref class
Form1 : public
System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO:
在此处添加构造函数代码
//
}
protected:
/// <summary>
///
清理所有正在使用的资源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
System::Windows::Forms::Button^ button1;
protected:
private:
System::Windows::Forms::Label^ label1;
private:
/// <summary>
///
必需的设计器变量。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows
Form Designer generated code
/// <summary>
///
设计器支持所需的方法-
不要
///
使用代码编辑器修改此方法的内容。
/// </summary>
void
InitializeComponent(void)
{
this->button1 =
(gcnew
System::Windows::Forms::Button());
this->label1 =
(gcnew
System::Windows::Forms::Label());
this->SuspendLayout();
//
// button1
//
this->button1->Location
= System::Drawing::Point(104, 102);
this->button1->Name
= L"button1";
this->button1->Size
= System::Drawing::Size(75, 23);
this->button1->TabIndex
= 0;
this->button1->Text
= L"button1";
this->button1->UseVisualStyleBackColor
= true;
this->button1->Click
+= gcnew
System::EventHandler(this,
&Form1::button1_Click);
//
// label1
//
this->label1->AutoSize
= true;
this->label1->Font
= (gcnew
System::Drawing::Font(L"宋体", 14.25F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(134)));
this->label1->Location
= System::Drawing::Point(104, 51);
this->label1->Name
= L"label1";
this->label1->Size
= System::Drawing::Size(69, 19);
this->label1->TabIndex
= 1;
this->label1->Text
= L"label1";
//
// Form1
//
this->AutoScaleDimensions =
System::Drawing::SizeF(6, 12);
this->AutoScaleMode
= System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize =
System::Drawing::Size(292, 273);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Name =
L"Form1";
this->Text =
L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void
button1_Click(System::Object^ sender,
System::EventArgs^ e) {
//button1->Teat="Hello
World";
label1->Text="Hello
World";
}
};
}
8、使用C++创建win32
控制台程序helloworld
// helloWorld.cpp :
定义应用程序的入口点。
//
#include "stdafx.h"
#include "helloWorld.h"
#define MAX_LOADSTRING 100
//
全局变量:
HINSTANCE hInst;
//
当前实例
TCHAR szTitle[MAX_LOADSTRING];
//
标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];
//
主窗口类名
//
此代码模块中包含的函数的前向声明:
ATOM
MyRegisterClass(HINSTANCE hInstance);
BOOL
InitInstance(HINSTANCE, int);
LRESULT CALLBACK
WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK
About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR
lpCmdLine,
int
nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO:
在此放置代码。
MSG msg;
HACCEL hAccelTable;
//
初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle,
MAX_LOADSTRING);
LoadString(hInstance, IDC_HELLOWORLD_VCWIN32WIN32, szWindowClass,
MAX_LOADSTRING);
MyRegisterClass(hInstance);
//
执行应用程序初始化:
if (!InitInstance (hInstance,
nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance,
MAKEINTRESOURCE(IDC_HELLOWORLD_VCWIN32WIN32));
//
主消息循环:
while
(GetMessage(&msg, NULL, 0, 0))
{
if
(!TranslateAccelerator(msg.hwnd, hAccelTable,
&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
//
函数:
MyRegisterClass()
//
//
目的: 注册窗口类。
//
//
注释:
//
//
仅当希望
//
此代码与添加到Windows 95
中的“RegisterClassEx”
//
函数之前的Win32
系统兼容时,才需要此函数及其用法。调用此函数十分重要,
//
这样应用程序就可以获得关联的
//
“格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE
hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style
= CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc =
WndProc;
wcex.cbClsExtra
= 0;
wcex.cbWndExtra
= 0;
wcex.hInstance
= hInstance;
wcex.hIcon
= LoadIcon(hInstance,
MAKEINTRESOURCE(IDI_HELLOWORLD_VCWIN32WIN32));
wcex.hCursor
= LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName =
MAKEINTRESOURCE(IDC_HELLOWORLD_VCWIN32WIN32);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm
= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return
RegisterClassEx(&wcex);
}
//
//
函数: InitInstance(HINSTANCE,
int)
//
//
目的:
保存实例句柄并创建主窗口
//
//
注释:
//
//
在此函数中,我们在全局变量中保存实例句柄并
//
创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance,
int nCmdShow)
{
HWND hWnd;
hInst = hInstance; //
将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance,
NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
//
函数: WndProc(HWND, UINT, WPARAM,
LPARAM)
//
//
目的: 处理主窗口的消息。
//
//
WM_COMMAND
-
处理应用程序菜单
// WM_PAINT -
绘制主窗口
//
WM_DESTROY
-
发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId
= LOWORD(wParam);
wmEvent = HIWORD(wParam);
//
分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd,
About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd,
message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO:
在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,
message, wParam, lParam);
}
return 0;
}
//
“关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK ||
LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
使用 Win32
创建窗体程序,比较复杂,需要创建类,变量,并且作相关的链接。这里不做
详细表述。
加载中,请稍候......