Windows Hello World Program

In Visual C++ 5.0
1. From the main menu, select File and then New…
2. In the New dialog, make sure that Projects is the current tab.
3. Select Win32 Application, and make the project name mhello
4. Verify that the Location specifies the parent directory of where you want the program, and click OK
5. Again, from the main menu click the File and then New…
6. This time, make sure that Files is the current tab
7. Highlight C++ Source File, and enter mhello.cpp for the filename.
8. Enter the source code for the program
9. Press F7 to create the program, and CTRL-F5 to run it.

In Borland C++ 5.0
1. From the main menu, select File, then New…, and then Project…
2. Make sure that the New Target dialog has the following (replace Project path and name as needed), then click OK:

A) The path is entered for the directory you want to place the file
B) Target Type is 'Application'
C) Platform is Win32
D) Target Model is 'GUI'
E) Frameworks, Controls, and Libraries have nothing checked.

3. In the Project window, double-click the bhello.cpp file name (if it isn't visible, click View, and then Project).
4. Enter the source code for the program
5. In the bhello.def file, add the following:
DESCRIPTION 'BHello'
EXETYPE WINDOWS
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
HEAPSIZE 8192
STACKSIZE 8192
6. Press F9 to make the program
7. Click the Lightening Bolt on the toolbar to run your program


Source Code

#include <windows.h>

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

char szAppName[]= "MHello"; // Application, and Window class name

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nCmdShow )
{
HWND hWnd;
MSG Msg;
WNDCLASS WndClass;

// Define Window Class (not a C++ class)
WndClass.hInstance = hInst;
WndClass.lpszClassName = szAppName;
WndClass.lpfnWndProc = WndProc;
WndClass.style = 0; // No special styles
WndClass.cbClsExtra = 0; // 'Extra' information about this window,
WndClass.cbWndExtra = 0; // our program has none.

// Set resources for the program: Icon, Cursor, Menu (we have none), and background
WndClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
WndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
WndClass.lpszMenuName = NULL;
WndClass.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );

 

// Register the class (this includes the 'WndProc')
if( !RegisterClass( &WndClass ) ) {
MessageBox( NULL, "Error registering class", NULL, MB_ICONEXCLAMATION|MB_OK );
return( 0 );
}

// Create the window, which will be our main window
hWnd = CreateWindow( szAppName, "Hello", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
HWND_DESKTOP, NULL, hInst, NULL );

if( !hWnd ) {
MessageBox( NULL, "Error creating window", NULL, MB_ICONEXCLAMATION|MB_OK );
return( 0 );
}

// Get the window visible, and update it
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );

while( GetMessage( &Msg, NULL, 0, 0 ) ) { // Message Loop, we sit here most of the time.
TranslateMessage( &Msg );
DispatchMessage( &Msg );
}
return( Msg.wParam );
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam )
{
// This is the 'message handler', handling all messages sent to our window
switch( Message ) {
case WM_PAINT:
HDC DC;
PAINTSTRUCT PaintStruct;
DC = BeginPaint( hWnd, &PaintStruct );
TextOut( DC, 0, 0, "Hello World", 11 );
EndPaint( hWnd, &PaintStruct );
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
// Any unhandled messages are passed onto to the default handler.
return( DefWindowProc( hWnd, Message, wParam, lParam ) );
}
return( 0 );
}