Showing posts with label Visual Cpp. Show all posts
Showing posts with label Visual Cpp. Show all posts

Friday, October 12, 2012

Ivor Horton's Beginning Visual C++ 2012



The only book to teach C++ programming with Microsoft Visual Studio!
There's a reason why Ivor Horton's Beginning Visual C++ books dominate the marketplace. Ivor Horton has a loyal following who love his winning approach to teaching programming languages, and in this fully updated new edition, he repeats his successful formula. Offering a comprehensive introduction to both the standard C++ language and to Visual C++, he offers step-by-step programming exercises, examples, and solutions to deftly guide novice programmers through the ins and outs of C++ development.
  • Introduces novice programmers to the current standard, Microsoft Visual C++ 2012, as it is implemented in Microsoft Visual Studio 2012
  • Focuses on teaching both the C++11 standard and Visual C++ 2012, unlike virtually any other book on the market
  • Covers the C++ language and library and the IDE
  • Delves into new features of both the C++11 standard and of the Visual C++ 2012 programming environment
  • Features C++ project templates, code snippets, and more
Even if you have no previous programming experience, you'll soon learn how to build real-world applications using Visual C++ 2012 with this popular guide.


Friday, June 8, 2012

Add UI element in Metro Style application using C++ code

To add UI element in Metro Style application dynamically using C++ code, instead of XAML:

Modify XAML to have a name in the root content, the <Grid> in this exercise:
    <Grid x:Name="root" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

    </Grid>


Modify the C++ code behind:
MainPage::MainPage()
{
 InitializeComponent();
 
 Uri^ imageUri = ref new Uri("http://goo.gl/fHDLv");
 BitmapImage^ bitmapImage = ref new BitmapImage(imageUri);
 Image^ image = ref new Image();
 image->Source = bitmapImage;
 image->Width = 100;
 image->Height = 53;

 root->Children->Append(image);


}


Add UI element in Metro Style application using C++ code


Wednesday, May 23, 2012

Visual C++ for Windows 8



Want to know how to write cool tablet apps using Visual C++? To kick off our day of Metro style programming in VC++, this talk will begin with an overview of how the WinRT type system is projected in Visual C++, then delve into how easy it is to use fast and portable C++, UIs built using XAML or DirectX or both, and powerful parallel computation from std::async and PPL to automatic vectorization and C++ AMP to harness powerful mobile GPUs... and use any or all of those tools together easily in any combination within the same Visual C++ application, delivering beautiful and responsive results on today's mainstream mobile hardware.

Monday, April 16, 2012

Visual C++ language reference (C++/CX)

Visual C++ has a revolutionary new native C++ programming model for creating Windows Metro style apps and components. In the new model, native C++ can interop directly with JavaScript and managed code without requiring intermediate software layers.

By using the new model, you can create:
  • C++ Metro style apps that use XAML to define the user interface and use the native stack.
  • C++ Windows Runtime components that can be consumed by JavaScript-based Metro style apps.
  • Metro style DirectX games and graphics-intensive apps.

[This documentation is for preview only, and is subject to change in later releases.]

Details: http://msdn.microsoft.com/en-us/library/windows/apps/hh699871

Sunday, February 12, 2012

A minimum MFC Windows Application

- In Start-up page of Microsoft Visual Studio 11, click New Project... to call-up application wizard.

- Select template of Visual C++ Win32 Project, with name of MFCminApplication, and click OK.
Create a new Visual C++ Win32 Project

- Click Next on the Application Settings screen.
Application Settings

- Check Empty project option, and click Finish.
Empty project

- After project created, click Project on the top menu, and select the project properties - MFCminApplication Properties...
MFCminApplication Properties...

- Select Configuration Properties -> General tab, select Use MFC in Shared DLL in Use of MFC property.
select Use MFC in Shared DLL

- Right click Source Files in Solution Explorer, -> Add -> Class to craete a new source file.
craete a new source file

- Select Visual C++ Class, and click Add.
Add a new Visual C++ Class

- Enter Class name of CMFCminApplication(both .h and .cpp file name will be generatedautomatically), with Base class of CWinApp, public Access. Finish.
Enter Class info

- You will be complained Bass class 'CWinApp' not found in the project. Click Yes to continue adding the class.
Bass class 'CWinApp' not found in the project.

- Modify the generated MFCminApplication.h and MFCminApplication.cpp:
MFCminApplication.h
#pragma once
#include <afxwin.h>
class CMFCminApplication :
 public CWinApp
{
public:
 virtual BOOL InitInstance();
};


MFCminApplication.cpp
#include "MFCminApplication.h"

class CMFCminFrameWnd :
 public CFrameWnd
{
public:
 CMFCminFrameWnd(){
  Create(0, L"Hello, Minimal MFC Application");
 }
};

BOOL CMFCminApplication::InitInstance(){
 m_pMainWnd = new CMFCminFrameWnd;
 m_pMainWnd->ShowWindow(m_nCmdShow);
 return TRUE;
}

CMFCminApplication myApplication;


- Finally, you can build and run the application.
The finalized project

- The minimal application change the title only!
The minimal MFC Application

Saturday, February 4, 2012

Create Visual C++ CLR Windows Forms Application on Microsoft Visual Studio 11 Developer Preview

- Click New Project... on the front page of Microsoft Visual Studio 11 Developer Preview.

- Select template of Visual C++ CLR, Windows Forms Application to to create application with a Windows user interface, name WindowsFormsApplication1. And click OK.
New a Visual C++ CLR Windows Forms Application

- The application wizard will generate a Windows application with a empty form for you.
Windows Forms Application

- Click on the Toolbox tab, and click the Auto Hide icon to keep it open.
Open Toolbox

- The Design window will re-align after the Toolbox pane ecpended.


- Drag a Label control from Toolbox to the form in Design window.
Add a Label

- Enter "Hello" in the Text field in Properties window, text of the Label in the form will change accordingly after press ENTER.
Change property

- Build and Run.
Demo program of Visual C++ CLR Windows Forms Application

Create Visual C++ MFC Application using Microsoft Visual Studio 11 Developer Preview's Application Wizard

- Click New Project... in Microsoft Visual Studio 11 Developer Preview

- Select template of Visual C++, MFC Application to create an application that uses the Microsoft Foundation Class Library, name MFCApplication1. Click OK.
Select template of Visual C++ MFC Application

- Review current project settings, and click Next.
project settings

- Accept the default setting and click Next.
Application Type

- Accept the default setting, Compound document support of None, and click Next.
Compound document support

- Click Next.
Document Template Properties

- Accept Database support of None, click Next.
Database support

- Accept the default User Interface Features.
User Interface Features

- Accept the default Advanced Features.
Advanced Features

- Finish.
Finish

- The application wizard has created this MFCApplication1 application for you. This application not only demonstrates the basics of using the Microsoft Foundation Classes but is also a starting point for writing your application.

- Double Click to open ReadMe.txt in Solution Explorer. This file contains a summary of what you will find in each of the files that make up your MFCApplication1 application.
ReadMe.txt

- Build and Run.
Demo sample of Visual C++ MFC Application

Friday, February 3, 2012

Creating a Win32 Windows Application using Win32 Application Wizard

- Click New Project... in Microsoft Visual Studio 11 Developer Preview

- Select Template of Visual C++ Win32, Win32 Project, enter Name of the project, ex. Win32Project1, and click Enter.
Win32 Application Wizard

- Walk through the steps of Application Wizard, by clicking of Next.
Next

- Review and accept the default setting by clicking of Finish.
Finish

- The Application Wizard will generate the default Win32 Project for you.
auto-generated project

- Where Win32Project1.cpp is the main application source file.
// Win32Project1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Win32Project1.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;        // current instance
TCHAR szTitle[MAX_LOADSTRING];     // The title bar text
TCHAR 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 _tWinMain(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPTSTR    lpCmdLine,
                    int       nCmdShow)
{
 UNREFERENCED_PARAMETER(hPrevInstance);
 UNREFERENCED_PARAMETER(lpCmdLine);

 // TODO: Place code here.
 MSG msg;
 HACCEL hAccelTable;

 // Initialize global strings
 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 LoadString(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
 MyRegisterClass(hInstance);

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

 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));

 // Main message loop:
 while (GetMessage(&msg, NULL, 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)
{
 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_WIN32PROJECT1));
 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT1);
 wcex.lpszClassName = szWindowClass;
 wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

 return RegisterClassEx(&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)
{
  HWND hWnd;

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

  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;
}

//
//  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)
{
 int wmId, wmEvent;
 PAINTSTRUCT ps;
 HDC hdc;

 switch (message)
 {
 case WM_COMMAND:
  wmId    = LOWORD(wParam);
  wmEvent = HIWORD(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:
  hdc = BeginPaint(hWnd, &ps);
  // TODO: Add any drawing code 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;
}


- Build and Run the project. It's a default Win32 Project.
Win32 Windows Application

Monday, January 23, 2012

^ (Handle to Object on Managed Heap)

In last post "Get user input from Console, ReadLine()", the returned oject from Console::ReadLine() is in data type of String^. ^ indicate that it is a handle to an object.

A handle to an object on the managed heap points to the "whole" object, and not to a member of the object.

See gcnew for information on how to create an object on the managed heap.

In Visual C++ 2002 and Visual C++ 2003, __gc * was used to declare an object on the managed heap. The ^ replaces __gc * in the new syntax.

The common language runtime maintains a separate heap on which it implements a precise, asynchronous, compacting garbage collection scheme. To work correctly, it must track all storage locations that can point into this heap at runtime. ^ provides a handle through which the garbage collector can track a reference to an object on the managed heap, thereby being able to update it whenever that object is moved.

Because native C++ pointers (*) and references (&) cannot be tracked precisely, a handle-to object declarator is used.

Member selection through a handle (^) uses the pointer-to-member operator (->).


Read more: http://msdn.microsoft.com/en-us/library/yk97tc08.aspx

Monday, January 16, 2012

Hello Visual C++ CLR Console Application using Microsoft Visual Studio 11 Developer Preview

- Click New project.. in the start-up page.
New Project...

- Select Visual C++ CLR Console Application, name it CLRConsole, and click OK.
Select template of Visual C++ CLR Console Application

- It's easy to notice on the auto-generated source code, CLRConsole.cpp:
: The parameters passed to main() is (array<System::String ^> ^args). To know more about "^", refer to the post ^ (Handle to Object on Managed Heap).
: The output using Console::, not cout.
: The 'L' in front of "Hello World" indicate that it's wide-character string.

Auto-generated Visual C++ CLR Console Application

- Modify to add following code before return, to make the program pause before exit.
Console::ReadLine();

// CLRConsole.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
   Console::WriteLine(L"Hello World");
   Console::ReadLine();
   return 0;
}


- Save and Run.
Hello Visual C++ CLR Console Application

Wednesday, January 4, 2012

Create Win32 C++ Console application using Microsoft Vistal Studio 11 Developer Preview

- Start Microsoft Vistal Studio 11 Developer Preview

- Click New Project... in the start-up page.
New Project...

- Select template of Visuall C++ Win32 Console Application, enter Name, and optionally enter Location, and click OK.
New Project Wizard: Visuall C++ Win32 Console Application

- Review the project settings and click Next.
Project Settings

- Review and accept the default application settings, click Finish.
Application Settings

- The Wizard will generate a default project:
Auto-generated project

- Modify the code:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
 std::cout << "Hello World!" << std::endl;
 system("PAUSE");
 return 0;
}

Add the code

- Finally, Save, Build and Run the first Visuall C++ Win32 Console Application:
Run

Tuesday, December 20, 2011

ISO/IEC standard C++, CLR, CLI and C++/CLI

C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell Labs as an enhancement to the C language. Originally named C with Classes, the language was later renamed C++ in 1983.

C++ is one of the most popular programming languages with application domains including systems software (such as Microsoft Windows), application software, device drivers, embedded software, high-performance server and client applications, and entertainment software such as video games. Several groups provide both free and proprietary C++ compiler software, including the GNU Project, Microsoft, Intel and Embarcadero Technologies. C++ has greatly influenced many other popular programming languages, most notably C# and Java.

C++ is also used for hardware design, where the design is initially described in C++, then analyzed, architecturally constrained, and scheduled to create a register-transfer level hardware description language via high-level synthesis.

The language began as enhancements to C, first adding classes, then virtual functions, operator overloading, multiple inheritance, templates, and exception handling among other features. After years of development, the C++ programming language standard was ratified in 1998 as ISO/IEC 14882:1998. The standard was amended by the 2003 technical corrigendum, ISO/IEC 14882:2003. The current standard extending C++ with new features was ratified and published by ISO in September 2011 as ISO/IEC 14882:2011 (informally known as C++11).

-- Reference:


CLR(Common Language Runtime) is the virtual machine component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs. In a process known as just-in-time (JIT) compilation, the CLR compiles the intermediate language code known as CIL into the machine instructions that in turn are executed by the computer's CPU. The CLR provides additional services including memory management, type safety and exception handling. All programs written for the .NET framework, regardless of programming language, are executed by the CLR.
The CLR is Microsoft's implementation of the Common Language Infrastructure (CLI) standard.

-- Reference:


CLI(Common Language Infrastructure) is a standardized environment in which applications written in multiple high-level languages can be executed in different system environments without the need to rewrite those applications to take into consideration the unique characteristics of those environments. It's now standardized as ECMA-335 Common Language Infrastructure (CLI) and also in the equivalent ISO standard, ISO/IEC 23271.

-- Reference:


C++/CLI is simple C++ (mainly developed by Microsoft) for the Common Language Infrastructure.



Monday, December 19, 2011

HelloMFC: Create Visual C++ MFC Application using Microsoft Visual Studio 11 Developer Preview

The FREE Microsoft Visual C++ 2010 Express have limited feature only: it can create applications using limited template of CLR, Win32 and General! Alternatively, you can using Microsoft Visual Studio 11 Developer Preview to develope MFC Application using Visual C++ for FREE, till June 30, 2012.

- Start Microsoft Visual Studio 11 Developer Preview.

- Click New Project...
New Project...

- Select Visual C++ MFC Application template, with name of HelloMFC.
Select Visual C++ MFC Application template

- The current project settings show the default selection, click Next to modify the setting.
Project Setting

- The default Application type is Tabbed Multiple documents, click to select Single document. Select Project style of MFC standard, instead of default Visual Studio. Select Visual style and colors using Windows Native/Default.

[Tips: Move your mouse over any of the options in the dialog, a tooltip will be displayed to explaine the option.]

Application Type

- Accept all other default selection by clicking Finish.

- The application wizard will creat the HelloMFC application for you.

- In the Solution Explorer on the right, scroll down and double click to open ReadMe.txt. The ReadMe.txt file contains a summary of what you will find in each of the files that make up your HelloMFC application.
ReadMe.txt

- Finally you can Save All your work, Build and Run HelloMFC to see what happen:
HelloMFC