CS 325 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CS 325

Description:

CS 325. Unix Programming Environment. and. Windows Programming ... #include 'example1.h' CWelcomeWindow::CWelcomeWindow() { Create(NULL, 'Hello CS 325' ... – PowerPoint PPT presentation

Number of Views:479
Avg rating:3.0/5.0
Slides: 19
Provided by: dyes
Category:
Tags: example1

less

Transcript and Presenter's Notes

Title: CS 325


1
CS 325
  • Unix Programming Environment
  • and
  • Windows Programming
  • with Microsoft Foundation Classes (MFC)

2
Lecture 18
  • Today
  • The Visual C environment (V6 .NET)
  • CStatic Hello World in Windows
  • Menus
  • Reading
  • Visual C manuals are online
  • http//msdn.microsoft.com/library
  • The MFC book Read the whole thing. Only about
    100 pages. Great tips and examples to avoid
    pitfalls. And some stuff you may not see in
    class.
  • Assignments
  • Get familiar with Visual C
  • You will be using it for your remaining projects

3
Visual C environment VS6
  • Commercial development environment
  • Editor, compiler, graphical interface, debugger,
  • Assume some familiarity
  • VC (V6) builds around workspaces projects
  • Project a group of program files, a file xxx.dsp
    contains names locations of all program files
  • Workspace a folder and control file that serves
    as a container for projects, control file is
    xxx.dsw
  • .NET calls it a solution (xxx.sln)
  • 6 cd installation (3 hrs)
  • 1.2 - 1.9 gigs
  • V6 solutions generally run unchanged in .NET and
    vice versa (1 or 2 correctable problems found
    last year)
  • VS6 and .NET use different DLLs

4
Building a VC project (VS6)
  • Open Visual C
  • Select New and Project from menu
  • Need a Win32 Application
  • NOT A CONSOLE
  • Select a location to store this projects files
  • Give the name of a new directory to be created
  • Note the parent directory of this new directory
  • Select New and File
  • C source file
  • Type the code for hello world in this file
  • Files in that directory
  • xxx.dsp, xxx.dsw, xxx.ncb, xxx.opt
  • xxx.cpp
  • Project-gtsettings
  • Using MFC in shared DLL

5
Steps to create a project in .NET
  • MENU File-gtnew-gtproject
  • Project Type Visual C
  • Template Win 32 Application
  • Specify Name and Location for Project
  • Under "MORE" can create another directory layer
  • Finish will launch application wizard
  • Application Wizard
  • Select application settings tab
  • Application type Windows Application
  • Additional options
  • "empty project" should be checked
  • Finish Will create an empty project

6
Not Done Yet
  • If "Solution Explorer" is not open
  • MENU View-gtSolution Explorer
  • Make sure Project name is highlighted in solution
    explorer.
  • (2nd line project folder)
  • MENU Project-gtProperties
  • Note if project name is not highlighted in
    solution explorer, project-gtproperties either
    will not exist or will be the wrong properties
    dialog

7
real windows programming
  • Utilize MFC (Microsoft Foundation Classes)
  • Designed to build windows applications quickly
    and easily
  • MFC has a hierarchy of classes it utilizes when
    building applications
  • Naming conventions for classes and
    methods/members follow Hungarian notation
  • VC has wizards to build skeletons for an
    MFC application.
  • Will try not to use in 325 (at least at first),
    rather let you see what is really taking place.

8
Before we dive into MFC
  • Three initial comments
  • MFC has a set of classes defined (see next slide)
    that does most things for you auto magically
  • Microsoft (as well as other commercial vendors)
    uses a unique naming convention notation, once
    you understand this reading other peoples code
    becomes easier
  • No main in windows applications (build off of
    existing Microsoft classes instead)

9
Comment 1 MFC Hierarchy
  • CObjectCGdiObject CPen CBrush CFont CBitmapC
    MenuCCmdTarget CWnd CDialog
    CStatic CButton CListBox
    CComboBox CFrameWnd CWinThread
    CWinApp
  • MFC base classgraphical device class class used
    for drawing patterns/colors class used for fill
    patterns/colors class for fonts class for
    bitmap imagesmenu classbase class for targeting
    event messages windows base class class for
    dialog box windows class for static
    items class for buttons (controls) class for
    list boxes class for combination boxes class
    for frame windows base class for task
    threads windows application class

10
Comment 2 Hungarian Notation
  • Leading part of variable identifies type of the
    variable
  • Eliminates guessing
  • Standard formats
  • Examples
  • Guess the following
  • m_dAverage
  • sName
  • m_arData
  • CStack
  • m_pNext
  • Array ar
  • Char c
  • Double d
  • Integer n or i
  • Boolean b
  • Pointer p
  • String s
  • Class C
  • Class member m_
  • Static class member s_

11
Comment 3 No main
  • Users normally interact with windows program by
    events
  • Move the mouse, click the mouse, click a button,
    press a key, etc.
  • These events generate messages that are sent to
    the windows program
  • MFC provides a wrapper that hides the details
    of capturing/interpreting these events from the
    programmer
  • Program provides responses to events

12
Build an MFC application
  • In VC
  • File/New, Win32 Application, Empty project,
    Finish
  • File/New, C Source File, ltgive file a namegt
  • Insert the code from the web (WelcomeApp)
  • Compile it
  • Build, build ltproject namegt.exe
  • Two errors while linking
  • nafxcwd.lib(thrdcore.obj) error LNK2001
    unresolved external symbol __endthreadex
  • nafxcwd.lib(thrdcore.obj) error LNK2001
    unresolved external symbol __beginthreadex
  • Debug/demo1.exe fatal error LNK1120 2
    unresolved externals
  • Fix this error
  • Project/Properties (Net) Settings(VS6), MFC, Use
    MFC in a shared DLL

13
Class Exercises
  • The code in our project should really be in two
    files
  • Class declaration in header file (xxx.h)
  • class CWelcomeWindow public CFrameWnd
  • public
  • CWelcomeWindow()
  • CWelcomeWindow()
  • private
  • CStatic m_pGreeting
  • Rest of code in C source file (xxx.cpp)
  • Fix this so we have a header source file

14
What does this code mean/do?
  • Header File (WelcomeApp.h)
  • Declares a new class that builds off of CFrameWnd
  • CFrameWnd gives basic window functionality
    (resize the window, move the window, close the
    window, etc.)
  • Class has constructor, destructor, one data
    member
  • class CWelcomeWindow public CFrameWnd
  • public
  • CWelcomeWindow()
  • CWelcomeWindow()
  • private
  • CStatic m_pGreeting // static block of text
    (text box) in window

15
What does this code mean/do?
  • include ltafxwin.hgt
  • include "example1.h"
  • CWelcomeWindowCWelcomeWindow()
  • Create(NULL, "Hello CS 325", WS_OVERLAPPEDWINDOW
    , CRect(100,100,400,400) )
  • m_pGreeting new CStatic
  • m_pGreeting-gtCreate("Welcome to Visual C in CS
    325", WS_CHILD WS_VISIBLE WS_BORDER
    SS_CENTER, CRect(20,20,100,100), this)
  • CWelcomeWindowCWelcomeWindow() delete
    m_pGreeting
  • MFC Header file needed
  • Our class declaration
  • Our class constructor
  • Create a new window, second parameter is title,
    third is style of window, fourth is
    location/size
  • Declare a new CStatic (block of text)
  • Initialize text block with Create method (text to
    display, style of display, location/size, parent)
  • Our class destructor

16
What does this code mean/do?
  • Class that drives program
  • InitInstance starts app
  • Creates the window
  • Makes window visible
  • Forces a refresh of screen
  • Reports success
  • As welcomeApp instantiates
  • Calls constructor, CWinApp constructor, which
    registers w/WinMain as entry point
  • WinMain calls InitInstance
  • InitInstance creates window
  • Window creates static block
  • class CWelcomeApp public CWinApp
  • public
  • BOOL InitInstance()
  • m_pMainWnd new
  • CWelcomeWindow()
  • m_pMainWnd-gtShowWindow (m_nCmdShow)
  • m_pMainWnd-gtUpdateWindow()
  • return TRUE
  • welcomeApp

17
Class Exercises
  • Using help in VC
  • Search for CFrameWnd, look up Create method
  • Only look in Visual C documentation
  • Search for CFrameWnd
  • Want to find document called CFrameWndCreate
  • Read the parameters that Create takes
  • Check out the style attributes for dwStyle
  • Search for CStatic
  • Look up how the Create function works in CStatic

18
Extra Class Exercises
  • Use help files to find dwstyle for
  • CFrameWndCreate
  • CStaticCreate
  • Load the source file demoHCPP into a VC Win32
    Application project.
  • Separate the code into two files within the
    project, demo.cpp and demo.h
  • Run the program and observe its output
  • Experiment with the options for Create w/
    CStatic, what do SS_CENTER and WS_BORDER do?
  • Modify the program so that Alabama is in the
    upper right corner, and Crimson Tide in the
    lower left (in a box appropriately sized for the
    text)
  • Comment CRect defines upper-left and lower-right
    endpoints of the rectangle in question
Write a Comment
User Comments (0)
About PowerShow.com