05 - Using Microsoft Visual C++

Embed Size (px)

Citation preview

  • 7/31/2019 05 - Using Microsoft Visual C++

    1/8

    CS4233 Network Programming

    Using Microsoft Visual C++

    Chen-Lung Chan

    Department of Computer Science

    National Tsing Hua University

    New a Project (1/3)

    File New

    Fill in the

    project name

    New a Project (2/3)

    Select Win32 Console Application

    New a Project (3/3)

  • 7/31/2019 05 - Using Microsoft Visual C++

    2/8

    Project Settings (1/2) Project Settings (2/2)

    Settings For

    Win32 Debug Win32 Release

    C++ Preprocessor

    Additional include directories

    Link General

    Object/library modules

    Input Additional library path

    Post-build step

    Build

    Build Test.exe

    Rebuild All Clean

    Set Active Configuration

    Test Win32 Release

    Test Win32 Debug

    Execute

    Files

    Release\Test.exe

    Debug\Test.exe

    Build Execute Test.exe

    Execute:

    Ctrl + F5

    Go:

    F5

    For debugging

  • 7/31/2019 05 - Using Microsoft Visual C++

    3/8

    Debug

    Breakpoint

    In Go

    Watch

    Step

    Step Into: F11

    Step Over: F10

    Stop Debugging

    Shift + F5

    New a C/C++ File (1/2)

    File New

    New a C/C++ File (2/2)

    Insert a file to the project

    MSDN (1/2)

    http://msdn.microsoft.com/visualc/previous/vc6/default.aspx http://msdn.microsoft.com/library/default.asp?url=/library/en-

    us/vccore98/HTML/vccorehm.asp

  • 7/31/2019 05 - Using Microsoft Visual C++

    4/8

    MSDN (2/2) System Configuration

    Add directories of new system libraries and

    include files to Visual C++ Tools Options Directories

    Workspace

    A workspace can contains several projects.

    Ex: add a MFC AppWizard (exe) project to the

    Test workspace

    Dialog based MFC Application

  • 7/31/2019 05 - Using Microsoft Visual C++

    5/8

    Switch among Projects

    Menu

    Project

    Set Active Project Build Set Active Configuration

    Customize the GUI

    Set the Properties of a Button (RightClick)

    Add a Message Handler (DoubleClick on the Button)

  • 7/31/2019 05 - Using Microsoft Visual C++

    6/8

    Add a Member Variable for theButton (1/2)

    Right Click (on the Button) ClassWizard

    Add a Member Variable for theButton (2/2)

    Handling Windows Messages Write a Win32 Static Library

    New a Win32 Static Library project

    Add the essential header files and source files

    to the project Build the library

    How to use the library? Add the path of the header file to Additional

    include directories Add the path of the library file to Additional library

    path Append the library file to Object/library modules

    Include the header file

  • 7/31/2019 05 - Using Microsoft Visual C++

    7/8

    Device Context (Dialog based)

    void CTestDlg::OnPaint(){

    if (IsIconic()){

    CPaintDC dc(this); // device context for painting

    SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

    // Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;

    // Draw the icondc.DrawIcon(x, y, m_hIcon);

    }else{

    CDialog::OnPaint();

    }}

    modify this!

    Use CDC

    Functions

    SetBkColor() SetTextColor() DrawText() FillSolidRect() DrawIcon() BitBlt()

    COLORREF RGB(0, 0, 0) - black RGB(255, 255, 255) - white

    Bitmap Functions (1/2)

    Insert Resource Import

    Bitmap Functions (2/2)

    CBitmap m_bmpTest;CDC m_dcTest;

    BOOL CTestDlg::OnInitDialog(){

    CClientDC dc(this);m_bmpTest.LoadBitmap(IDB_BITMAP1);m_dcTest.CreateCompatibleDC(&dc);m_dcTest.SelectObject(&m_bmpTest);

    } void CTestDlg::OnPaint()

    {CPaintDC dc(this);dc.FillSolidRect(0, 0, 640, 480, RGB(0, 0, 0));

    dc.BitBlt(0, 0, 200, 100, &m_dcTest, 0, 0, SRCCOPY);}

  • 7/31/2019 05 - Using Microsoft Visual C++

    8/8

    Set Windows Timer to Refresh

    SetTimer(4233, 30, 0);

    KillTimer(4233);

    Add a message handler for WM_TIMERvoid CTestDlg::OnTimer(UINT nIDEvent){

    if (nIDEvent == 4233)Invalidate();

    CDialog::OnTimer(nIDEvent);}

    Avoid Flicker: Do not EraseBackground

    Add a message handler for

    WM_ERASEBKGND Just return FALSE

    BOOL CTestDlg::OnEraseBkgnd(CDC* pDC)

    {

    return FALSE;}

    Avoid Flicker: Double Buffering Double Buffering

    CBitmap m_bmpBG;CDC m_dcMem;

    BOOL CTestDlg::OnInitDialog(){

    m_bmpBG.CreateCompatibleBitmap(&dc, 640, 480);m_dcMem.CreateCompatibleDC(&dc);m_dcMem.SelectObject(&m_bmpBG);

    }

    void CTestDlg::OnPaint(){

    CPaintDC dc(this);m_dcMem.FillSolidRect(0, 0, 640, 480, RGB(0, 0, 0));m_dcMem.BitBlt(0, 0, 200, 100, &m_dcTest, 0, 0, SRCCOPY);

    dc.BitBlt(0, 0, 640, 480, &m_dcMem, 0, 0, SRCCOPY);}