IT-info > C / C++ > C++ programmering (Code reference & info)

C / C++

C++ code reference (Visual Studio 2015 and later versions)

int WINAPI WinMain

int APIENTRY WinMain

int CALLBACK WinMain

C++ for loop

#include <iostream>
using namespace std;

int main () {
  // for loop execution
  for( int a = 10; a < 20; a = a + 1 ) {
    cout << "value of a: " << a << endl;
  }

  return 0;
}

time loop

#include <chrono>
#include <thread>
#include <iostream>

int main()
{
  std::chrono::seconds interval( 10 ) ; // 10 seconds
  for( int i = 0 ; i < 10 ; ++i )
  {
    std::cout << "tick!\n" << std::flush;
    std::this_thread::sleep_for( interval );
  }
}

Switch codepage in Windows Command Prompt/Console

#include <windows.h>
#include <iostream>
using namespace std;

// Creating wchar (unicode) string variable and assigning some non-latin-1 characters to it
wchar_t s[] = L"Blåbærsyltetøy!";

// Function that prints out the above string variable to the screen in unicode format
void xoutput(){
  wcout << s << "\n\n";
}

int main()
{
  SetConsoleOutputCP(850);
  int oldcp = GetConsoleOutputCP();

  SetConsoleOutputCP(CP_UTF8);
  int newcp = GetConsoleOutputCP();

  cout << "Copepage:" << newcp << " (Unicode (UTF-8))\n";

  xoutput();

  SetConsoleOutputCP(oldcp);

  cout << "Copepage:" << oldcp << " (OEM Multilingual Latin 1; Western European (DOS))\n";

  xoutput();

  return 0;
}

cin with strings - Using getline function

The default behaviour of cin is to store only the first string of characters that is followed by a space character into the string variable.
If you want to display a string including space characters, you have to use cin and the string variable in conjunction with the getline function. The getline function is function built into C++.

// cin with strings

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string usrInput;
    cout << "Type something and press Enter:\n\n";
    getline(cin, usrInput);
    cout << "You wrote: " << usrInput << "\n";

    return 0;
}

Misc.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

/*
int sum(int x, int y)
{
  return x + y;
}
*/

void numCheck()
{
  int i = 5;
  int x;

  cout << "Enter a number\n\n";

  cin >> x;

  if (x == i) {
    cout << "\n\n";
    cout << "You selected the right number.\n";
  } else {
    cout << "\n\n";
    cout << "You selected the wrong number. Try again.\n";
    numCheck();
  }
}

int main()
{
  //string greeting;
  //string name;
  //string greeting("Hojsan");

  //greeting = "Hojsan";

  //cout << "Skriv ditt förnamnn\n\n";
  //cin >> name;
  //cout << "\n" << greeting << " " << name << "\n\n";

  numCheck();

  //int z = sum(5, 3);

  //cout << "The sum is " << z << "\n\n"; //8

  return 0;
}

#include "stdafx.h"
#include <iostream>
#include <string>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;

int addition(int a, int b)
{
  int r;
  r = a + b;
  return r;
}

void myNumber(){
  int thenumber = 8;

  cout << thenumber << "\n\n";
}

/*
int gethostname(
_Out_ char *name,
_In_  int  namelen
);
*/

int main()
{
  char szPath[128] = "";
  WSADATA wsaData;
  WSAStartup(MAKEWORD(2, 2), &wsaData);
  gethostname(szPath, sizeof(szPath));
  printf("%s\n\n", szPath);
  WSACleanup();

  //int z = addition(5, 3);
  //cout << "The result is " << z << "\n\n";
  //myNumber();

  return 0;
}

#include <iostream>
#include <string>
using std::string;

int main()
{
    int x, y, z;
    string progname = "Multiplier";

    std::cout << progname << "\n\n";
    std::cout << "Enter a digit and press Enter key\n";
    std::cin >> x;
    std::cout << "Enter one more digit and press Enter key\n";
    std::cin >> y;
    z = x * y;
    std::cout << "The result is " << z;
}

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

void gamefunc(){
    char myChar[6];
    strcpy_s(myChar, "****#");

    for(int i = 0; i<6; i++){
        if(i != 5){
            cout << myChar[i];
            cout.flush();
            sleep(2000);
        } else {
            cout << myChar[i];
        }
    }

    cout << "\n\n" << "YOU LOSE\n\n";
}

int main(){
    cout << "\n\n" << "SILLY GAME\n";
    cout << "==========\n\n";
    cout << "If you get five identical characters in a row, you've won.\n\n";
    cout.flush();
    _sleep(10000);

    gamefunc();

    return 0;
}

#include <iostream>
using namespace std;
#include <windows.h>
#include <MMsystem.h>
#pragma comment(lib, "winmm.lib")

int main(){
    PlaySound(TEXT("beep.wav"), NULL, SND_SYNC);

    return 0;
}

// Win32Project1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Win32Project1.h"
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
  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_APPLICATION));
  wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  wcex.lpszMenuName = NULL;
  wcex.lpszClassName = szWindowClass;
  wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

  if (!RegisterClassEx(&wcex))
  {
    MessageBox(NULL,
    _T("Call to RegisterClassEx failed!"),
    _T("Win32 Guided Tour"),
    NULL);

    return 1;
  }

  hInst = hInstance; // Store instance handle in our global variable

  // The parameters to CreateWindow explained:
  // szWindowClass: the name of the application
  // szTitle: the text that appears in the title bar
  // WS_OVERLAPPEDWINDOW: the type of window to create
  // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
  // 500, 100: initial size (width, length)
  // NULL: the parent of this window
  // NULL: this application does not have a menu bar
  // hInstance: the first parameter from WinMain
  // NULL: not used in this application
  HWND hWnd = CreateWindow(
  szWindowClass,
  szTitle,
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  500, 100,
  NULL,
  NULL,
  hInstance,
  NULL
  );

  if (!hWnd)
  {
    MessageBox(NULL,
    _T("Call to CreateWindow failed!"),
    _T("Win32 Guided Tour"),
    NULL);

    return 1;
  }

  // The parameters to ShowWindow explained:
  // hWnd: the value returned from CreateWindow
  // nCmdShow: the fourth parameter from WinMain
  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);

  // Main message loop:
  MSG msg;
  while (GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  return (int)msg.wParam;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  PAINTSTRUCT ps;
  HDC hdc;
  TCHAR greeting[] = _T("Hello, World!");

  switch (message)
  {
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);

    // Here your application is laid out.
    // For this introduction, we just print out "Hello, World!"
    // in the top left corner.
    TextOut(hdc, 5, 5, greeting, _tcslen(greeting));

    // TextOut(hdc, 5, 20, moreText, _tcslen(moreText));
    // End application-specific layout section.

    EndPaint(hWnd, &ps);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
    break;
  }

  return 0;
}

// Win32Project5.cpp : Defines the entry point for the application.
//

#include <stdafx.h>
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")

VOID OnPaint(HDC hdc)
{
  Graphics graphics(hdc);
  //Pen pen(Color(255, 0, 0, 255));
  //graphics.DrawLine(&pen, 0, 0, 200, 100);

  Image image(L"C:\\varfiles\\images\\fun\\Img_0922.jpg");
  graphics.DrawImage(&image, 60, 10);
  //graphics.DrawImage(&image, 280, 10);
}

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

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
  HWND hWnd;
  MSG msg;
  WNDCLASS wndClass;
  GdiplusStartupInput gdiplusStartupInput;
  ULONG_PTR gdiplusToken;

  // Initialize GDI+.
  GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

  wndClass.style = CS_HREDRAW | CS_VREDRAW;
  wndClass.lpfnWndProc = WndProc;
  wndClass.cbClsExtra = 0;
  wndClass.cbWndExtra = 0;
  wndClass.hInstance = hInstance;
  wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  wndClass.lpszMenuName = NULL;
  wndClass.lpszClassName = TEXT("GettingStarted");

  RegisterClass(&wndClass);

  hWnd = CreateWindow(
  TEXT("GettingStarted"), // window class name
  TEXT("Getting Started"), // window caption
  WS_OVERLAPPEDWINDOW, // window style
  CW_USEDEFAULT, // initial x position
  CW_USEDEFAULT, // initial y position
  CW_USEDEFAULT, // initial x size
  CW_USEDEFAULT, // initial y size
  NULL, // parent window handle
  NULL, // window menu handle
  hInstance, // program instance handle
  NULL); // creation parameters

  ShowWindow(hWnd, iCmdShow);
  UpdateWindow(hWnd);

  while (GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  GdiplusShutdown(gdiplusToken);
  return msg.wParam;
} // WinMain

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  HDC hdc;
  PAINTSTRUCT ps;

  switch (message)
  {
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    OnPaint(hdc);
    EndPaint(hWnd, &ps);
    return 0;
    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
  }
} // WndProc

// Win32Project8.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Win32Project8.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;    // current instance
WCHAR szTitle[MAX_LOADSTRING];    // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];    // the main window class name

// Forward declarations of functions included in this code module:
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 wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_WIN32PROJECT8, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT8));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW 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_WIN32PROJECT8));
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN32PROJECT8);
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//In this function, we save the instance handle in a global variable and
//create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance; // Store instance handle in our global variable

    HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
}

//
//   FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//   PURPOSE: Processes messages for the main window.
//
//   WM_COMMAND  - process the application menu
//   WM_PAINT    - Paint the main window
//   WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            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:
        {
            PAINTSTRUCT ps;

            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...

            WCHAR szName[MAX_COMPUTERNAME_LENGTH + 1] = L"";
            DWORD dwLen = sizeof(szName) / sizeof(WCHAR);
            if (!GetComputerNameW(szName, &dwLen)) dwLen = 0;
            TextOutW(hdc, 10, 10, szName, dwLen);

            EndPaint(hWnd, &ps);
        }
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
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;
}

// WindowsProject2.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "WindowsProject2.h"

#define MAX_LOADSTRING 100
#define ID_BUTTON1 0x8801
#define ID_BUTTON2 0x8802
#define ID_BUTTON3 0x8803

// Global Variables:
HINSTANCE hInst;   // current instance
WCHAR szTitle[MAX_LOADSTRING];   // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];   // the main window class name

// Forward declarations of functions included in this code module:
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 wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE    hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_WINDOWSPROJECT2, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT2));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW 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_WINDOWSPROJECT2));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT2);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND staticTxt;

    switch (message)
    {
        case WM_CREATE:
        {
            staticTxt = CreateWindow(
            L"STATIC",  // Predefined class; Unicode assumed
            L"Hello",      // static text
            WS_VISIBLE | WS_CHILD,  // Styles
            10,         // x position (horizontal)
            10,         // y position (vertical)
            500,        // window width
            50,         // window height
            hWnd,       // Parent window
            NULL,       // No menu.
            (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
            NULL);      // Pointer not needed.

            HWND hwndButton = CreateWindow(
            L"BUTTON",  // Predefined class; Unicode assumed
            L"TESTBTN",      // Button text
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles
            10,         // x position (horizontal)
            70,         // y position (vertical)
            100,        // Button width
            100,        // Button height
            hWnd,     // Parent window
            (HMENU) ID_BUTTON1,
            (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
            NULL);      // Pointer not needed.

            HWND hwndButton2 = CreateWindow(
            L"BUTTON",  // Predefined class; Unicode assumed
            L"TESTBTN2",      // Button text
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles
            120,         // x position (horizontal)
            70,         // y position (vertical)
            100,        // Button width
            100,        // Button height
            hWnd,     // Parent window
            (HMENU) ID_BUTTON2,
            (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
            NULL);      // Pointer not needed.

            HWND hwndButton3 = CreateWindow(
            L"BUTTON",  // Predefined class; Unicode assumed
            L"TESTBTN3",      // Button text
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles
            230,         // x position (horizontal)
            70,         // y position (vertical)
            100,        // Button width
            100,        // Button height
            hWnd,     // Parent window
            (HMENU) ID_BUTTON3,
            (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
            NULL);      // Pointer not needed.
    }
    break;
    case WM_COMMAND:
    {
            int wmId = LOWORD(wParam);
            //int wmEvent = HIWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case ID_BUTTON1:
                //SetWindowText(staticTxtWin, TEXT("Control string"));
                //MessageBeep(MB_ICONINFORMATION);
                MessageBox(hWnd, L"Hello", L"TestBox", MB_OK);
                //MessageBox(NULL, _T("Push Button"), _T("Title"), MB_OK);
                break;
            case ID_BUTTON2:
                MessageBeep(MB_ICONINFORMATION);
                //SetWindowText(staticTxt, TEXT("Control string"));
                break;
            case ID_BUTTON3:
                //MessageBeep(MB_ICONINFORMATION);
                SetWindowText(staticTxt, TEXT("Control string"));
                break;
            //case BN_CLICKED:
                //  MessageBox(hWnd, L"Hello", L"TestBox", MB_OK);
                //  break;
            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_CTLCOLORSTATIC:
    {
        HDC outputTxtArea = (HDC)wParam;
        SetTextColor(outputTxtArea, RGB(255, 255, 255));
        SetBkColor(outputTxtArea, RGB(0, 0, 0));

        if (hbrBkgnd == NULL)
        {
         hbrBkgnd = CreateSolidBrush(RGB(0, 0, 0));
        }
        return (INT_PTR)hbrBkgnd;
    }
    break;
    */
    case WM_PAINT:
    {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            EndPaint(hWnd, &ps);
    }
    break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
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;
}

// WindowsProject3.cpp : Defines the entry point for the application.
//
// https://msdn.microsoft.com/en-us/library/windows/desktop/hh298433(v=vs.85).aspx
// - How to Create a Multiline Edit Control

#include "stdafx.h"
#include "WindowsProject3.h"

#define MAX_LOADSTRING 100
#define ID_EDITCHILD 100

// Global Variables:
HINSTANCE hInst;  // current instance
WCHAR szTitle[MAX_LOADSTRING];  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];  //the main window class name

// Forward declarations of functions included in this code module:
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 wWinMain(_In_ HINSTANCE hInstance,
                                              _In_opt_ HINSTANCE hPrevInstance,
                                              _In_ LPWSTR lpCmdLine,
                                              _In_ int nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_WINDOWSPROJECT3, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT3));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW 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_WINDOWSPROJECT3));
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT3);
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance; // Store instance handle in our global variable

    HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
}

//
//   FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//   PURPOSE: Processes messages for the main window.
//
//   WM_COMMAND - process the application menu
//   WM_PAINT - Paint the main window
//   WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    static HWND hwndEdit;

    TCHAR lpszLatin[] = L"Lorem ipsum dolor sit amet, consectetur "
        L"adipisicing elit, sed do eiusmod tempor "
        L"incididunt ut labore et dolore magna "
        L"aliqua. Ut enim ad minim veniam, quis "
        L"nostrud exercitation ullamco laboris nisi "
        L"ut aliquip ex ea commodo consequat. Duis "
        L"aute irure dolor in reprehenderit in "
        L"voluptate velit esse cillum dolore eu "
        L"fugiat nulla pariatur. Excepteur sint "
        L"occaecat cupidatat non proident, sunt "
        L"in culpa qui officia deserunt mollit "
        L"anim id est laborum.";

    switch (message)
    {
        case WM_CREATE:
            hwndEdit = CreateWindowEx(
                0, L"EDIT",  // predefined class
                NULL,  // no window title
                WS_CHILD | WS_VISIBLE | WS_VSCROLL |
                ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
                0, 0, 0, 0,  // set size in WM_SIZE message
                hWnd,  // parent window
                (HMENU)ID_EDITCHILD,  // edit control ID
                (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
                NULL);  // pointer not needed
                SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)lpszLatin);  // Add text to the window.
            return 0;

        case WM_COMMAND:
        {
                int wmId = LOWORD(wParam);
                // Parse the menu selections:
                switch (wmId)
                {
                    /*
                    case IDM_EDUNDO:
                        // Send WM_UNDO only if there is something to be undone.

                        if (SendMessage(hwndEdit, EM_CANUNDO, 0, 0))
                            SendMessage(hwndEdit, WM_UNDO, 0, 0);
                        else
                        {
                            MessageBox(hwndEdit,
                                L"Nothing to undo.",
                                L"Undo notification",
                                MB_OK);
                        }
                        break;

                    case IDM_EDCUT:
                        SendMessage(hwndEdit, WM_CUT, 0, 0);
                        break;

                    case IDM_EDCOPY:
                        SendMessage(hwndEdit, WM_COPY, 0, 0);
                        break;

                    case IDM_EDPASTE:
                        SendMessage(hwndEdit, WM_PASTE, 0, 0);
                        break;

                    case IDM_EDDEL:
                        SendMessage(hwndEdit, WM_CLEAR, 0, 0);
                        break;
                    */

                    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_SETFOCUS:
            SetFocus(hwndEdit);
            return 0;

        case WM_SIZE:
            // Make the edit control the size of the window's client area.

            MoveWindow(hwndEdit,
                0, 0,  // starting x- and y-coordinates
                LOWORD(lParam),  // width of client area
                HIWORD(lParam),  // height of client area
                TRUE);  // repaint window
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
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;
}

// Set two timers.

SetTimer(hwnd, // handle to main window
    IDT_TIMER1, // timer identifier
    10000, // 10-second interval
    (TIMERPROC) NULL); // no timer callback

SetTimer(hwnd, // handle to main window
    IDT_TIMER2, // timer identifier
    300000, // five-minute interval
    (TIMERPROC) NULL); // no timer callback

//To process the WM_TIMER messages generated by these timers, add a WM_TIMER case statement to the window procedure
//for the hwnd parameter.

case WM_TIMER:

    switch (wParam)
    {
        case IDT_TIMER1:
            // process the 10-second timer

            return 0;

        case IDT_TIMER2:
            // process the five-minute timer

            return 0;
    }

//Destroying a Timer
//Applications should use the KillTimer function to destroy timers that are no longer necessary.
//The following example destroys the timers identified by the constants IDT_TIMER1, IDT_TIMER2, and IDT_TIMER3.

// Destroy the timers.

KillTimer(hwnd, IDT_TIMER1);
KillTimer(hwnd, IDT_TIMER2);
KillTimer(hwnd, IDT_TIMER3);

HICON hIcon1; // icon handle
POINT ptOld; // previous cursor location
UINT uResult; // SetTimer's return value
HINSTANCE hinstance; // handle to current instance

//
// Perform application initialization here.
//

wc.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(400));
wc.hCursor = LoadCursor(hinstance, MAKEINTRESOURCE(200));

// Record the initial cursor position.

GetCursorPos(&ptOld);

// Set the timer for the mousetrap.

uResult = SetTimer(hwnd, // handle to main window
    IDT_MOUSETRAP, // timer identifier
    10000, // 10-second interval
    (TIMERPROC) NULL); // no timer callback

if (uResult == 0)
{
    ErrorHandler("No timer is available.");
}

LONG APIENTRY MainWndProc(
    HWND hwnd, // handle to main window
    UINT message, // type of message
    WPARAM wParam, // additional information
    LPARAM lParam) // additional information
{

    HDC hdc; // handle to device context
    POINT pt; // current cursor location
    RECT rc; // location of minimized window

    switch (message)
    {
        //
        // Process other messages.
        //

        case WM_TIMER:
        // If the window is minimized, compare the current
        // cursor position with the one from 10 seconds
        // earlier. If the cursor position has not changed,
        // move the cursor to the icon.

            if (IsIconic(hwnd))
            {
                GetCursorPos(&pt);

                if ((pt.x == ptOld.x) && (pt.y == ptOld.y))
                {
                    GetWindowRect(hwnd, &rc);
                    SetCursorPos(rc.left, rc.top);
                }
                else
                {
                    ptOld.x = pt.x;
                    ptOld.y = pt.y;
                }
            }

            return 0;

    case WM_DESTROY:

    // Destroy the timer.

            KillTimer(hwnd, IDT_MOUSETRAP);
            PostQuitMessage(0);
            break;

        //
        // Process other messages.
        //

}

Info and source in external files

Five in a row  (Alpha 1)
Five in a row  (Alpha 2)
Five in a row  (Alpha 4)
Edit window
Pointers1
Pointers1
stoi1
stoi2
Stringstream
Using the Timer: Three Methods
WIN32 fileInfo example code

Hiding a control in Windows

A control in a Window or dialog can be hidden using

ShowWindow(hControlWin, SW_HIDE);

In a dialog you can retrive the controls window handle by calling

GetDlgItem(hDlg, < CtrlID >);

Typically you write something like:

ShowWindow(GetDlgItem(hDlg, 2), SW_HIDE);

Invisible Window

It's still a window, I think having message loops in console programs in impossible as you have to have a window to send the messages too.
To make the window invisible (and not even appear in the running programs list), I used:

ShowWindow(hWnd, SW_NORMAL);

When I wanted to see it again, I used:

SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
ShowWindow(hWnd, SW_NORMAL);
UpdateWindow(hWnd);

Hope that helps anyone who wants to do what I was trying to do