Getting Started with Visual Studio 2005 Learning Notes

Source: Internet
Author: User
Tags configuration settings

Reprinted from: http://blog.163.com/zm_shichaoren/blog/static/6880234120087211452776/

Getting Started with Visual Studio 2005

Finally removed the visual c++6.0 and replaced it with a new visual c++2005. Although it is Simplified Chinese version, but the powerful Visual Studio 2005 IDE Large development environment and n many configuration settings, learning is not very simple. C + +/CLI through the ISO C + + standard of pure extension and new syntax, unlike the traditional MS-DOS console program, so for beginners, you should first figure out how to compile a common console application and Windows applications in the Visual Studio 2005 development environment. Here is my entry notes:

1) Win32 Console Application

File-〉 new-〉 Project àvisual C++--〉WIN32--〉WIN32 Console Application-〉 Console application-〉 Precompiled header-〉 complete-〉 new win32.cpp.

<1>//basic Ioprogram

Winconsole.cpp: Defines the entry point of the console application.

#include "stdafx.h"

#include <iostream>

Using Std::cin;

Using Std::cout;

int _tmain (int argc, _tchar* argv[])

{

cout<< "Hello world.\n";

GetChar ();

return 0;

}

Ctrl+f7 compile, Ctrl+f5 run.

<2>//Swap code and unmanaged code mixed programming

Managed.cpp: Defines the entry point of the console application.

#include "stdafx.h"

#using <mscorlib.dll>//In order to use the Console::WriteLine method

#include <stdio.h>//In order to use printf

using namespace System;

Declaring unmanaged code

#pragma unmanaged

void print (char *msg)

{

printf ("%s\n", msg);

}

Switch back to the replacement code

#pragma managed

int _tmain (int argc, _tchar* argv[])

{

Call the managed method output to the console

Console::WriteLine (L "Hello World from Managed Method");

Use standard output

Print ("Hello World from Unmanaged Method");

GetChar ();

return 0;

}

Note: Project Properties-〉 Configuration Properties-〉 General-〉 Common Language Runtime support-〉 Common Language Runtime support (/clr)

We observe the "Configuration Properties-〉 General--〉 multibyte Character set/unicode character set", which defaults to "Unicode character set". The vs2005 previous version of VS uses the multibyte character set by default, whereas VS2005 uses the Unicode character set by default, which causes some code not to compile. Here we need to figure out the problem from main to _tmain and from char* to _tchar*.

Beginning with Visual Studio 7, Microsoft introduced the so-called generic text routine mapping. TCHAR. h defines macros or inline functions that are mapped to multibyte character set (MBCS), single-byte character set (SBCS), and Unicode models. These mappings handle character data that is represented as single-byte ANSI ASCII or double-byte Unicode encoding.

If you are following the Unicode algorithm, be sure to change all the main () functions to _tmain () and change the character pointer from char* to _tchar*. About the Windows coding problem will be frequently encountered, you can check the relevant information online.

2) CLR Console application

File-〉 new-〉 Project àvisual C++--〉CLR-〉CLR Console Application-〉 Open the source file in Solution Explorer on the left-〉 right-click-〉 Add New Item-〉 build. cpp.

Note: Project Properties-〉 Configuration Properties-〉 General-〉 Common Language Runtime support-〉 Common Language Runtime support (/clr)

Clrconsole.cpp: Main project file.

#include "stdafx.h"

using namespace System;

int main (array<system::string ^> ^args)

{

Console::WriteLine (L "Hello World from CLR Console application.");

return 0;

}

3) MFC writing Windows console applications

File-〉 new-〉 Project àvisual C++--〉WIN32--〉WIN32 Console Application-〉 console application-〉 Empty project

#include <afx.h>//Required

#include <afxwin.h>//Required

#include <iostream>

Using Std::cout;

Using Std::endl;

CWINAPP Theapp; Create an application instance

int _tmain (int argc, tchar* argv[], tchar* envp[])//Entry function

{

int nretcode = 0;

if (! AfxWinInit (:: GetModuleHandle (NULL),

Null

:: GetCommandLine (),

0)//Because the WinMain function provided by MFC is not called, this afxwininit is initialized

)

{

nRetCode = 1;

}

Else

{

CString Strhello ("Hello World from MFC winconsole platform"); MFC class CString defines a string object

AfxMessageBox (Strhello);

Char *strhellot = "Hello World from Win32 console platform";

cout << strhellot << Endl;

}

GetChar ();

return nretcode;

}

Fatal error C1189: #error: Building MFC Application With/md[d] (CRT DLL version) requires MFC shared DLL version. Please #define _afxdll or does not use/md[d]

Workaround: Project Properties-〉 Configuration Properties-〉 General-〉mfc using MFC with-〉 in shared DLLs

Or use MFC in static.

4) using vc++2005 for traditional SDK programming;

File-〉 new-〉 project àvisual c++--〉win32--〉win32 application-〉 Empty project-〉 complete-〉 new win32.cpp.

#include <windows.h>

Message handler, the Windows system stipulates that each message handler function is defined in the same form

LRESULT CALLBACK WndProc (

HWND hwnd,//Window handle

UINT nmessage,//message Sent

WPARAM WPARAM,//Parameter 1

LPARAM LPARAM//Parameter 2

)

{

Switch (nmessage)

{

Responding to WM_LBUTTONDOWN messages

Case WM_LBUTTONDOWN:

MessageBox (hwnd,l "received WM_LBUTTONDOWN message!", L "notice", MB_OK);

Break

Responding to Wm_rbuttondown messages

Case Wm_rbuttondown:

MessageBox (hwnd,l "received Wm_rbuttondown message!", L "notice", MB_OK);

Break

Responding to wm_create messages

Case WM_CREATE:

MessageBox (hwnd,l "received wm_create message!", L "notice", MB_OK);

Break

Responding to Wm_destroy messages

Case Wm_destroy:

MessageBox (hwnd,l "received Wm_destroy message!", L "notice", MB_OK);

PostQuitMessage (0);

Break

The function DefWindowProc () must be called, which is a multi-regulation of the Windows system

Default

return DefWindowProc (Hwnd,nmessage,wparam,lparam);

}

return FALSE;

}

Application entry function (first executed by this program)

int WINAPI WinMain (

HINSTANCE hinstance,//program instance handle

HInstance hprevinstance,//to keep Win16 compatible with the handle

LPSTR lpcmdline,//Command Line arguments

int nCmdShow//Initialize window display mode

)

{

Wndclass Ownd; Window class, available for WNDCLASS

MSG msgtmp; Windows messages

HWND hwnd; Window handle

Ownd.style=cs_hredraw|cs_vredraw; Window type

Ownd.lpfnwndproc= (WNDPROC) WNDPROC; The window handler function is WndProc ()

Ownd.cbclsextra=null; Window class no extension

Ownd.cbwndextra=null; Window instance has no extension

Ownd.hinstance=hinstance; Current instance Handle

Ownd.hicon=loadicon (null,idi_application); window minimized icon, default icon

Ownd.hcursor=loadcursor (Null,idc_arrow); Use arrows as mouse icons

Ownd.hbrbackground= (Hbrush) getstockobject (White_brush);//white as window color

Ownd.lpszmenuname=null; Window No Menu

Ownd.lpszclassname=l "First Windows program"; Window belongs to class name

Register window class

if (! RegisterClass (&ownd))

{

MessageBeep (0); Warn if registration fails

return FALSE;

}

Create window

Hwnd=createwindow (

L "First Windows Program",//Registered window class name

L "First Vwindows program",//Window title name

Ws_overlappedwindow,//Window style

Cw_usedefault,//display the x-coordinate of the upper-left corner of the window, taking the default value

Cw_usedefault,//Displays the y-coordinate of the upper-left corner of the window, taking the default value

Cw_usedefault,//display the x-coordinate of the lower-right corner of the window, taking the default value

Cw_usedefault,//display the y-coordinate of the lower-right corner of the window, taking the default value

NULL,//This window has no parent window

NULL,//menu handle (set to no menu handle here)

HINSTANCE,//program strength handle

Null//points to a pointer-type parameter passed to the window, set to null here

);

if (!hwnd)

return FALSE; Failed to create window, return false

ShowWindow (hwnd,ncmdshow); Display window

UpdateWindow (HWND); Update window

while (GetMessage (&msgtmp,null,0,0))

{

TranslateMessage (&MSGTMP); Translating messages

DispatchMessage (&MSGTMP); Passing a message to a handler function

}

return msgtmp.wparam; Additional parameters for returning messages

}

Compile error: Msvcr80d.dll file not found when using VC2005 Express Edition

Workaround: There is a "use FAT32 solution" under the General List tool, "Properties-----", and then you can select "Yes" and rebuild the solution (RECOMPILE)! (Note: Be sure to configure this option before compiling the project, otherwise it will not work)
5) Developing Windows applications with MFC class libraries

File-〉 new-〉 Project Àvisual C++--〉WIN32--〉WIN32 Project-〉WIN32 application-〉 Empty Project-〉 Add source file

#include <afxwin.h>//mfc header file

Class Chelloapp:public CWINAPP//Create an instance of the application class

{

Public

Virtual BOOL InitInstance ();

};

Chelloapp Theapp;

Class Cmainframe:public CFRAMEWND//declaring main window classes

{

Public

CMainFrame ()

{

Create (NULL, "I love U", Ws_overlappedwindow,crect (0,0,400,300));

}

Protected

afx_msg void OnLButtonDown (UINT nflags,cpoint point);

afx_msg void Onrbuttondown (UINT nflags,cpoint point);

Declare_message_map ()

};

Message map entry

Begin_message_map (Cmainframe,cframewnd)

On_wm_lbuttondown ()//Click the left mouse button to map the macro

On_wm_rbuttondown ()

End_message_map ()

Defining message Map functions

void Cmainframe::onlbuttondown (UINT nflags,cpoint point)

{

MessageBox ("Everlasting Sky", "Hello");

Cframewnd::onlbuttondown (Nflags,point);

}

void Cmainframe::onrbuttondown (UINT nflags,cpoint point)

{

MessageBox ("Once", "Hello");

Cframewnd::onrbuttondown (Nflags,point);

}

The initialization function to invoke whenever the application executes

BOOL chelloapp::initinstance ()

{

M_pmainwnd=new CMainFrame ();

M_pmainwnd->showwindow (m_nCmdShow);

M_pmainwnd->updatewindow ();

return TRUE;

}

Note 1: Project Properties-〉 Configuration Properties-〉 General-〉MFC use of MFC in shared DLLs-〉

Or use MFC in static.

Compilation error: "Error C2664: ' cframewnd::create ': cannot convert parameter 2 from ' const char [9] ' to ' LPCTSTR '

Types pointed to is unrelated; Conversion requires reinterpret_cast, C-style cast or Function-style cast

Workaround: Project Properties-〉 Configuration Properties-〉 General-〉 character Set-〉 multibyte character set

Getting Started with Visual Studio 2005 Learning Notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.