lab manual · 1 study windows api’s. find out their relationship with mfc classes. appreciate how...

41
1 LAB MANUAL VISUAL PROGRAMMING LAB( CSE 409 F) DEPARTMENT OF COMPUTER SCIECE AND ENGINEERING

Upload: others

Post on 21-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

  • 1

    LAB MANUAL

    VISUAL PROGRAMMING LAB( CSE 409 F)

    DEPARTMENT OF COMPUTER SCIECE AND ENGINEERING

  • 2

    Check list for Lab Manual

    S. No. Particulars Page

    Number

    1 Mission and Vision 3

    2 Guidelines for the student 4

    3 List of Programs as per University 5-6

    4 Beyond Syllabus 7

    5 Sample copy of File 8 – 23

  • 3

    Mission

    To develop BRCM College of Engineering & Technology into a “Center of Excellence”

    By :

    Providing State – of – the art Laboratories, Workshops, Research and instructional facilities

    Encouraging students to delve into technical pursuits beyond their academic curriculum.

    Facilitating Post – graduate teaching and research

    Creating an environment for complete personality development of students.

    Assisting in the best possible placement

    Vision

    To Nurture and Harness talent for empowerment towards self actualization in all technical

    domains – both existing for the future

  • 4

    Guidelines for the Students :

    1. Students should be regular and come prepared for the lab practice.

    2. In case a student misses a class, it is his/her responsibility to complete that missed

    experiment(s).

    3. Students should bring the observation book, lab journal and lab manual. Prescribed textbook

    and class notes can be kept ready for reference if required.

    4. They should implement the given Program individually.

    5. While conducting the experiments students should see that their programs would meet the

    following criteria:

    Programs should be interactive with appropriate prompt messages, error messages if any,

    and descriptive messages for outputs.

    Programs should perform input validation (Data type, range error, etc.) and give appropriate

    error messages and suggest corrective actions.

    Comments should be used to give the statement of the problem and every function should

    indicate the purpose of the function, inputs and outputs

    Statements within the program should be properly indented

    Use meaningful names for variables and functions.

    Make use of Constants and type definitions wherever needed.

    6. Once the experiment(s) get executed, they should show the program and results to the

    instructors and copy the same in their observation book.

    7. Questions for lab tests and exam need not necessarily be limited to the questions in the manual,

    but could involve some variations and / or combinations of the questions.

  • 5

    LIST OF PROGRAMS(University Syllabus)

    VISUAL PROGRAMMING LAB (CSE 409 F) Semester : VI IT

    S.NO PROGRAM

    Study of Visual Basic 6.0 .NET and Visual C++ 6.0 .NET

    1 Study Windows API’s. Find out their relationship with MFC classes. Appreciate

    how they are helpful in finding complexities of windows programming.

    2 Get familiar with essential classes in a typical (Document- view architecture)

    VC++ Program and their relationship with each other.

    3 Create an SDI application in VC++ that adds a popup menu to your application

    which uses File drop down menu attached with the menu bar as the pop-up menu.

    The pop-up menu should be displayed on the right click of the mouse.

    4 Create an SDI application in VC++ using which the user can draw atmost 20

    rectangles in the client area. All the rectangles that are drawn should remain visible

    on the screen even if the window is refreshed. Rectangle should be drawn on the

    second click of the left mouse button out of the two consecutive clicks. If the user

    tries to draw more than 20 rectangles, a message should get displayed in the client

    area that “No more rectangles can be drawn”

    5 Create an application in VC++ that shows how menu items can be grayed, disabled

    and appended at run time. 6 Write a program in VC++ to implement serialization of inbuilt and user defined

    objects.

    7 Write a program in VC++ to create archive class object from C File class that reads

    and stores a simple structure (record)

    8 Write a program in VC++ to create archive class object from C File class that reads

    and stores a simple structure (record). 9 Write a program in VB to implement a simple calculator 10 Create a simple database in MS Access Database /Oracle and a simple database

    application in VB that shows database connectivity through DAO and ADO 11 Write a simple program that displays an appropriate message when the illegal

    operation is performed using error handling technique in VB. 12 Write a program in VB to create a notepad. 13 Create a DLL in VB. 14 Write a program in VC++ to implement a simple calculator. 15 Write a program in VC++ to create a static link library and a dynamic link library.

  • 6

    16 Create a simple database in MS Access Database and a simple database application

    in VC++ that shows database connectivity through ADO model.

    17 Make an Active X control of your own using VB.

  • 7

    Beyond syllabus

    P. No Program

    1 To create a simple window using VC++ programming.

    2 To interact with the mouse using vc++ programming

    3 To interact with the keys using vc++ programming.

    4 To perform the calculator operation using VC++ programming

    5 To Create a ToolBar Using VC++ Programming

    6 To create a DLL using them in a application using VC++ programming

    7 To create a Threads using them in a application using VC++ programming.

    8 To implement the MDI Application using VC++ Programming.

  • 8

    Sample Copy of File

    Program 1: To Create a simple window using vc++ programming

    Steps:

    1. Start →programs Microsoft Visual Studio6.0→Microsoft Visual C++6.0.

    2. Visual C++ Window will be opened.

    3. Select File→New→Win32 Application, then give the project name and then choose empty

    project button and finally give finish→OK.

    4. Again go to File→New→C++ Source File→File Name→OK.

    5. Type the coding.

    6. Build and test the application.

    Simple Window Creation Program

    #include

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

    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR

    szCmdLine,int iCmdShow)

    {

    static TCHAR szAppName[]=TEXT("HelloWin");

    HWND hwnd;

    MSG msg;

    WNDCLASS win1;

    win1.style=CS_HREDRAW|CS_VREDRAW;

    win1.lpfnWndProc=WndProc;

    win1.cbClsExtra=0;

    win1.cbWndExtra=0;

    win1.hInstance=hInstance;

    win1.hIcon=LoadIcon(NULL,IDI_APPLICATION);

    win1.hCursor=LoadCursor(NULL,IDC_WAIT);

    win1.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

    win1.lpszMenuName=NULL;

    win1.lpszClassName=szAppName;

    if(!RegisterClass(&win1))

    {

    MessageBox(0,"welcome",szAppName,MB_OK);

    return FALSE;

  • 9

    }

    hwnd=CreateWindow(szAppName,"vasanth",WS_OVERLAPPEDWINDOW,10,

    20,500,400,NULL,NULL,hInstance,NULL);

    ShowWindow(hwnd,iCmdShow);

    UpdateWindow(hwnd);

    while(GetMessage(&msg,0,0,0))

    {

    TranslateMessage(&msg);

    DispatchMessage(&msg);

    }

    return (0);

    }

    LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM

    wParam,LPARAM lParam)

    {

    HDC hdc;

    PAINTSTRUCT ps;

    RECT rect;

    switch(message)

    {

    case WM_PAINT:

    hdc=BeginPaint(hwnd,&ps);

    GetClientRect(hwnd,&rect);

    DrawText(hdc,TEXT("Hello"),-

    1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);

    EndPaint(hwnd,&ps);

    return(0);

    case WM_DESTROY:

    PostQuitMessage(0);

    return(0);

    }

    return DefWindowProc(hwnd,message,wParam,lParam);

    }

  • 10

    Output:

    Program 2 : To interact with the mouse using vc++ programming.

    Steps :

    1 .Start → programs Microsoft Visual Studio6.0→Microsoft Visual C++6.0.

    2 Visual C++ Window will be opened.

    3 Select File→New→Win32 Application, then give the project name and then choose empty

    project button and finally give finish →OK.

    4 Again go to File→ New→ C++ Source File→ File Name → OK.

    5 Type the coding.

    6 Build and test the application.

    Mouse Events program:

    #include

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

    WNDCLASS a;

  • 11

    int flag=0;

    int WINAPI WinMain(HINSTANCE i,HINSTANCE j,LPSTR k,int l)

    {

    HWND h;

    MSG m;

    a.style=CS_HREDRAW|CS_VREDRAW;

    a.hInstance=i;

    a.cbClsExtra=0;

    a.lpfnWndProc=WndProc;

    a.lpszMenuName=NULL;

    a.cbWndExtra=0;

    a.lpszClassName="my";

    a.hCursor=LoadCursor(NULL,IDC_ARROW);

    a.hIcon=LoadIcon(NULL,IDI_APPLICATION);

    a.hbrBackground=(HBRUSH)GetStockObject(RGB(255,0,0));

    if(!RegisterClass(&a))

    {

    MessageBox(h,TEXT("Error"),"my",MB_ICONERROR);

    return 0;

    }

    h=CreateWindow("my",TEXT("TITLE"),WS_OVERLAPPEDWINDOW,100,100,150,100

    ,

    NULL,NULL,NULL,NULL);

    ShowWindow(h,l);

    while(GetMessage(&m,NULL,0,0))

    {

    TranslateMessage(&m);

    DispatchMessage(&m);

    }

    return m.wParam;

    }

    LRESULT CALLBACK WndProc(HWND w,UINT x,WPARAM y,LPARAM z)

    {

    HDC d;

    switch(x)

    {

    case WM_LBUTTONDOWN:

    flag=1;

    return 0;

    case WM_MOUSEMOVE:

    if(flag==1)

  • 12

    {

    d=GetDC(w);

    SetPixel(d,LOWORD(z),HIWORD(z),RGB(255,0,0));

    ReleaseDC(w,d);

    }

    return 0;

    case WM_LBUTTONUP:

    flag=0;

    return 0;

    case WM_DESTROY:

    PostQuitMessage(10);

    return 0;

    }

    return DefWindowProc(w,x,y,z);

    }

    OUTPUT:

    Program 3 : To interact with the keys using vc++ programming.

    Steps:

    1. Start →programs Microsoft Visual Studio6.0→Microsoft Visual C++6.0.

    2. Visual C++ Window will be opened.

  • 13

    3. Select File→New→Win32 Application, then give the project name and then choose empty

    project button and finally give finish →OK.

    4. Again go to File →New →C++ Source File File Name →OK.

    5. Type the coding.

    6. Build and test the application.

    Keyboard Events Program

    Program:

    #include

    #include

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

    WNDCLASS a;

    char cs[50];

    int X=10,Y=10;

    int flag=0;

    int WINAPI WinMain(HINSTANCE i,HINSTANCE j,LPSTR K,int l)

    {

    HWND h;

    MSG m;

    a.style=CS_HREDRAW|CS_VREDRAW;

    a.hInstance=i;

    a.cbClsExtra=0;

    a.lpszMenuName=NULL;

    a.cbWndExtra=0;

    a.lpszClassName="my";

    a.lpfnWndProc=WndProc;

    a.hCursor=LoadCursor(NULL,IDC_ARROW);

    a.hIcon=LoadIcon(NULL,IDI_APPLICATION);

    a.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

    if(!RegisterClass(&a))

    {

    MessageBox(0,TEXT("ERROR"),"my",MB_ICONERROR);

    return 0;

    }

    h=CreateWindow("my",TEXT("TITLE"),WS_OVERLAPPEDWINDOW,CW_USEDEFA

    ULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,i,NULL);

    ShowWindow(h,l);

    UpdateWindow(h);

    while(GetMessage(&m,NULL,0,0))

    {

    TranslateMessage(&m);

  • 14

    DispatchMessage(&m);

    }

    return m.wParam;

    }

    LRESULT CALLBACK WndProc(HWND h,UINT x,WPARAM y,LPARAM z)

    {

    HDC d;

    switch(x)

    {

    case WM_CHAR:

    sprintf(cs,"%c",LOWORD(y));

    d=GetDC(h);

    if(X>300)

    {

    X=10,Y+=20;

    }

    TextOut(d,X+=8,Y,cs,1);

    ReleaseDC(h,d);

    break;

    case WM_LBUTTONUP:

    flag=1; return 0;

    case WM_DESTROY:

    PostQuitMessage(0);

    return 0;

    }

    return DefWindowProc(h,x,y,z);

    }

  • 15

    OUTPUT:

    Program 4 : To perform the calculator operation using VC++ programming.

    Steps :

    1. Start →programs Microsoft Visual Studio6.0→Microsoft Visual C++6.0 2. File→ New→ MFC AppWizard (exe) →project name →ok. 3. Choose Dialog Based Applications finish. 4. Dialog box will be opened. 5. Design the dialog box like this.

    6. Change each of the button name as 0,1,=,+

    7. After adding button name the dialog box look like this.

    8. Right click on the edit box and choose Class Wizard click on the Member Variables Tab and choose IDC_EDIT1→Add Variable and member variable name as m_t1

    (Or any other name). Give ok. Now the Window appears like this

  • 16

  • 17

    9. Give OK

    10. Click on the 0 button give the member function name and give 0K. 11. Add the Coding for each buttons like this. 12. In the CalcDlg header file under the construction comment line add the declaration part.

    i.e int index,val,data,data1;

    double m,a,b;

    char temp [10];

    13. Build and test the application

    PROGRAM:

    //eDlg.h header file

    int index,val,data,data1;

    double m,a,b;

    char temp[10];

    //eDlg.cpp

    void CEDlg::OnOne()

    {

    UpdateData(true);

    if(m_t1=="0"|| m_t1==" ")

    m_t1="1";

    else

    m_t1+="1";

    UpdateData(false);

    }

  • 18

    void CEDlg::OnZero()

    {

    UpdateData(true);

    if(m_t1=="0"|| m_t1==" ")

    m_t1="0";

    else

    m_t1+="0";

    UpdateData(false);

    }

    void CEDlg::Ontwo()

    {

    UpdateData(true);

    if(m_t1=="0"|| m_t1==" ")

    m_t1="2";

    else

    m_t1+="2";

    UpdateData(false);// TODO: Add your control notification handler code here

    }

    void CEDlg::Onthree()

    {

    UpdateData(true);

    if(m_t1=="0"|| m_t1==" ")

    m_t1="3";

    else

    m_t1+="3";

    UpdateData(false);// TODO: Add your control notification handler code here

    }

    void CEDlg::Onfour()

    {

    UpdateData(true);

    if(m_t1=="0"|| m_t1==" ")

    m_t1="4";

    else

    m_t1+="4";

    UpdateData(false);// TODO: Add your control notification handler code here

    }

    void CEDlg::Onfive()

    {

  • 19

    UpdateData(true);

    if(m_t1=="0"|| m_t1==" ")

    m_t1="5";

    else

    m_t1+="5";

    UpdateData(false);// TODO: Add your control notification handler code here

    }

    void CEDlg::Onsix()

    {

    UpdateData(true);

    if(m_t1=="0"|| m_t1==" ")

    m_t1="6";

    else

    m_t1+="6";

    UpdateData(false);// TODO: Add your control notification handler code here

    }

    void CEDlg::Onseven()

    {

    UpdateData(true);

    if(m_t1=="0"|| m_t1==" ")

    m_t1="7";

    else

    m_t1+="7";

    UpdateData(false);// TODO: Add your control notification handler code here

    }

    void CEDlg::Oneight()

    {

    UpdateData(true);

    if(m_t1=="0"|| m_t1==" ")

    m_t1="8";

    else

    m_t1+="8";

    UpdateData(false);// TODO: Add your control notification handler code here

    }

    void CEDlg::Onnine()

    {

    UpdateData(true);

  • 20

    if(m_t1=="0"|| m_t1==" ")

    m_t1="9";

    else

    m_t1+="9";

    UpdateData(false);// TODO: Add your control notification handler code here

    }

    void CEDlg::Onsine()

    {

    UpdateData(true);

    m=atof(m_t1);

    m=(m*3.14)/180;

    a=sin(m);

    sprintf(temp,"%f",a);

    m_t1=temp;

    UpdateData(false);

    }

    void CEDlg::Onclear()

    {

    UpdateData(true);

    m_t1="0";

    UpdateData(false);

    }

    void CEDlg::Onequal()

    {

    updateData(true);

    switch(index)

    {

    case 0:

    {

    UpdateData(true);

    a=atof(m_t1);

    b=a+m;

    sprintf(temp,"%f",b);

    m_t1=temp;

    UpdateData(false);

    break;

    }

    case 1:

    {

    UpdateData(true);

  • 21

    a=atof(m_t1);

    b=í-a;

    sprintf(temp,"%f",b);

    m_t1=temp;

    UpdateData(false);

    break;

    }

    case 2:

    {

    UpdateData(true);

    a=atof(m_t1);

    b=a*m;

    sprintf(temp,"%f",b);

    m_t1=temp;

    UpdateData(false);

    break;

    }

    case 3:

    {

    UpdateData(true);

    a=atof(m_t1);

    b=m/a;

    sprintf(temp,"%f",b);

    m_t1=temp;

    UpdateData(false);

    break;

    }

    }

    UpdateData(false);

    }

    void CEDlg::Onclear()

    {

    UpdateData(true);

    m_t1="0";

    UpdateData(false); // TODO: Add your control notification handler code here

    }

    void CEDlg::Onplus()

  • 22

    {

    UpdateData(true);

    m=atof(m_t1);

    m_t1=" ";

    index=0;

    UpdateData(false);

    } OUTPUT:

    Program 5 : To Create a ToolBar Using VC++ Programming

    Steps:

    1: Run VC++ AppWizard to create an SDI application and select the document view architecture

    and deselect the printing and print preview by accepting all the default settings and click

    finish to design the project.

  • 23

    2: Use the resource editor to edit the application's main menu.

    3: In Resource View, double-click on IDR_MAINFRAME under Menu and Edit the

    IDR_MAINFRAME menu resource to create a menu.

    Use the following command IDs for your new menu items.

    Menu Caption Command ID

    Diagrams &Rectangle ID_DIAGRAMS_RECT

    Diagrams E&llipse ID_DIAGRAMS_ELLIPSE

    4: Edit the IDR_MAINFRAME toolbar resource to create a bitmap.

    5: Assign the IDs ID_DIAGRAMS_RECT, ID_DIAGRAMS_ELLIPSE to the two buttons.

  • 24

    6: Use ClassWizard to add ToolbarView view class message handlers.

    7: Add message handlers for the following command and update command UI messages, and

    accept the default function names shown in the following table.

    Object ID Message Member Function

    ID_DIAGRAMS_RECT COMMAND OnDiagramsRect

    ID_DIAGRAMS_RECT UPDATE_COMMAND_UI OnUpdateDiagramsRect

    ID_DIAGRAMS_ELLIPSE COMMAND OnDiagramsEllipse

    ID_DIAGRAMS_ELLIPSE UPDATE_COMMAND_UI OnUpdateDiagramsEllipse

    8: Edit the ToolbarView.cpp file.

    void CToolbarView::OnDiagramsRect()

    {

    CClientDC dc(this);

    dc.SelectStockObject(GRAY_BRUSH);

    dc.Rectangle(50,50,100,100);

    }

    void CToolbarView::OnUpdateDiagramsRect(CCmdUI* pCmdUI)

    {

    pCmdUI->Enable(TRUE);

    pCmdUI->SetCheck(1);

    }

    void CToolbarView::OnDiagramsEllipse()

    {

    CClientDC dc(this);

    dc.SelectStockObject(BLACK_BRUSH);

    dc.Ellipse(150,150,200,200);

    }

    void CToolbarView::OnUpdateDiagramsEllipse(CCmdUI* pCmdUI)

  • 25

    {

    pCmdUI->Enable(TRUE);

    }

    9: Build and test the Toolbar application.

    OUTPUT:

    Program 6: To create a DLL using them in a application using VC++ programming.

    Steps:

    1. Start →programs Microsoft Visual Studio6.0→Microsoft Visual C++6.0

    2. File→Win32 Dynamic Link Library →project name (dynamic) &clearly note the Location

    where your project is stored and give OK.

    3. Choose A simple DLL project in the step1 then Finish →OK.

  • 26

    4. Go to File View and in the source file double click on dynamic .cpp (where dynamic is the

    project name).

    5. Add the coding &Build the application .You can see the .lib file &.dll file in your projects

    Debug folder.

    6. Now close the workspace and choose a new MFC Appwizard (exe) &give the project name

    (dynamic1).

    7. Choose the dialog based application &paste one command button on the dialog box double

    click the button and write the coding.

    8. Copy the .lib file and .dll file in the debug folder of the dynamic project and paste in

    dynamic1 projects debug folder.

    9. Go to the coding window of the dynamic1 project select the Project menu and select add to

    project &now you can see the debug folder. Now change the file of type as all files and just

    double click it.

    10. Build &test the application

    PROGRAM

    //The below coding should be typed in Win32 Dynamic Link Library in mydl.cpp at the top.

    extern "C"__declspec(dllexport)double sum(double,double);

    extern "C"__declspec(dllexport)double mul(double,double);

    double sum(double a, double b)

    {

    return(a+b);

    }

    double mul(double a, double b)

    {

    return(a*b);

    }

    The below coding should be typed in MFC Appwizard[exe]

    //dynamicdllDlg.cpp

  • 27

    void CBhuvanaDlg::OnDisplay()

    {

    CString x;

    x.Format("SUM=%2f\n product=%2f",sum(3,4),mul(5,6));

    MessageBox(x);

    At the Top include the below coding

    extern "C"__declspec(dllimport) double sum(double,double);

    extern "C"__declspec(dllimport) double mul(double,double);

    OUTPUT

  • 28

    Program 7 : To create a Threads using them in a application using VC++ programming.

    Steps:1: Run AppWizard to generate the project. Accept all the default settings but two: select

    Single Document and deselect Printing and Print Preview and select document view architecture.

    Finish the application

    2: Use the dialog editor to create the dialog resource IDD_COMPUTE.

    Keep the default control ID for the Cancel button, but use IDC_START for the Start button. For the progress indicator, accept the default ID IDC_PROGRESS1.

    3: Use ClassWizard to create the CComputeDlg class.

    4: After the class is generated, add a WM_TIMER message handler function. Also add

    BN_CLICKED message handlers for IDC_START and IDCANCEL. Accept the default names OnStart and OnCancel.

    5: Add three data members to the CComputeDlg class. Edit the file ComputeDlg.h. Add the following private data members:

    int m_nTimer; int m_nCount; enum { nMaxCount = 10000 };

    6: Add initialization code to the CComputeDlg constructor in the ComputeDlg.cpp file.

  • 29

    m_nCount = 0;

    7: Code the OnStart function in ComputeDlg.cpp.

    void CComputeDlg::OnStart()

    {

    MSG message;

    m_nTimer = SetTimer(1, 100, NULL);

    ASSERT(m_nTimer != 0);

    GetDlgItem(IDC_START)->EnableWindow(FALSE);

    volatile int nTemp;

    for (m_nCount = 0; m_nCount < nMaxCount; m_nCount++)

    {

    for (nTemp = 0; nTemp < 10000; nTemp++) { }

    if (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))

    {

    ::TranslateMessage(&message);

    ::DispatchMessage(&message);

    }

    } CDialog::OnOK();

    }

    8: Code the OnTimer function in ComputeDlg.cpp.

    void CComputeDlg::OnTimer(UINT nIDEvent)

    {

    CProgressCtrl* pBar = (CProgressCtrl*) GetDlgItem(IDC_PROGRESS1);

    pBar->SetPos(m_nCount * 100 / nMaxCount);

    }

    9: Update the OnCancel function in ComputeDlg.cpp.

    void CControlDlg::OnCancel()

    {

    TRACE("entering CComputeDlg::OnCancel\n");

    if (m_nCount == 0) {

    CDialog::OnCancel();

    }

    else {

    m_nCount = nMaxCount; }

    }

    10: Edit the CComputeDlgView class in ComputeDlgView.cpp.

  • 30

    void CComputeDlgView::OnDraw(CDC* pDC)

    {

    pDC->TextOut(0, 0, "Press the left mouse button here.");

    }

    11: Then use ClassWizard to add the OnLButtonDown function to handle

    WM_LBUTTONDOWN messages, and add the following code:

    void CComputeDlgView::OnLButtonDown(UINT nFlags, CPoint point)

    {

    CComputeDlg dlg;

    dlg.DoModal();

    }

    12: In ComputeDlgView.cpp, add the following #include statement:

    #include "ComputeDlg.h"

    13: Build and run the application.

    OUTPUT

    Program 8 : To Create an ODBC and implement it in an application using vc++ programming.

    ODBC CONNECTIVITY:

    1. Create a database with any fields using Ms-Access.

  • 31

    2. Start→settings→controlpanel→Administrative tools→ Select ODBC. In the ODBC data

    source administrator window choose the data source name(Ms Access Database) and then click

    Add and select Microsoft Access Driver(*.mdb)and then click finish.

    3. Give any data source name and click the select button and then select the Database Name from

    the appropriate drives and then click ok.

    CONNECTING ODBC WITH VC

    1. Start→Programs→Microsoft VisualStudio6.0→ Microsoft Visual c++ 6.0.

    2. Choose MFC AppWizard (exe) give the project name and click ok.

    3. Choose Single document applications click Next and then in step2 of MFC AppWizard

    select database view without file support click on the data source button.

    4. Give the data source name give ok then give finish.

    ODBC

    odbcView.cpp

    // COdbcView message handlers

    void COdbcView::OnRecordAdd()

    {

    // TODO: Add your command handler code here

    m_pSet->AddNew();

    UpdateData(true);

    if(m_pSet->CanUpdate()){

    m_pSet->Update();

    }

    if(!m_pSet->IsEOF()){

    m_pSet->MoveLast();

    }

    UpdateData(false);

    }

    void COdbcView::OnRecordDelete()

    {

    // TODO: Add your command handler code here

    CRecordsetStatus status;

  • 32

    try{

    m_pSet->Delete();

    }

    catch(CDBException* e){

    AfxMessageBox(e->m_strError);

    e->Delete();

    m_pSet->MoveFirst();

    UpdateData(false);

    return;

    }

    m_pSet->GetStatus(status);

    if(status.m_lCurrentRecord==0)

    {

    m_pSet->MoveNext();

    }

    UpdateData(false);

    }

    void COdbcView::OnRecordUpdate()

    {

    // TODO: Add your command handler code here

    m_pSet->Edit();

    UpdateData(true);

    if(m_pSet->CanUpdate()){

    m_pSet->Update();

    }

    }

  • 33

    Program 9 : To implement the MDI Application using VC++ Programming.

    Steps & Algorithm:

    1. Start→ Programs →Microsoft VisualStudio6.0→ Microsoft Visual c++ 6.0.

    2. Choose MFC AppWizard (exe) give the project name and click ok.

    3. In the step1 choose multiple documents and in Step 6 change the base class as CFormView

    →finish →ok.

    4. Place two static text, two edit box& one button

    5. Double click the button and add the coding.

    6. In mdiDoc.h add the member variable names for the 2 edit box.

    7. In mdiDoc.cpp add the construction coding.

    8. Go to ClassView → choose→ CmdiView and click add member function.

    9. The member function name added here is UpdateControlsFromDoc().

    10. In View.cpp Update the coding for above function.

    11. Just scroll upwards the view.cpp window and update the coding.

    Build &test the application

  • 34

    MDI

    // mdiDoc.h

    public:

    float m_fSalary;

    CString m_strName;

    // mdiDoc.cpp

    CMdiDoc::CMdiDoc():m_strName("")

    {

    // TODO: add one-time construction code here

    m_fSalary=0.0;

    }

    // mdiView.h

    public:

    void UpdateControlsFromDoc();

    // mdiView.cpp

    void CMdiView::OnInitialUpdate()

    {

    CFormView::OnInitialUpdate();

    ResizeParentToFit();

    UpdateControlsFromDoc();

    }

    void CMdiView::OnEnter()

    {

    // TODO: Add your control notification handler code here

    CMdiDoc* pDoc = GetDocument();

    UpdateData(TRUE);

    pDoc->m_fSalary = m_fSalary;

    pDoc->m_strName = m_strName;

    }

    void CMdiView::UpdateControlsFromDoc()

    {

    CMdiDoc* pDoc = GetDocument();

    m_fSalary = pDoc->m_fSalary;

    m_strName = pDoc->m_strName;

    UpdateData(FALSE);

    }

  • 35

    Program 10 : To implement the Serialization Application using VC++ Programming.

    Steps:

    1. Start→ Programs→ Microsoft VisualStudio6.0→ Microsoft Visual c++ 6.0.

    2.Then choose the new Text tool and paste the student.cpp coding and paste in the empty

    window and save it under the name of “student.cpp” (save it within the double codes) &do the

    same for student.h. close the application.

    3. Choose MFC AppWizard (exe) gives the project name and click ok.

    4. Choose single based application in step1 of MFC AppWizard &in the step 6 change the base

    class as CFormView &design the form view as shown in the figure

  • 36

    5. Change the member variable for the edit boxes as follows.

    6. Add a new function name as UpdateControlsFromDoc() name by using class view (Refer

    MDI Program) and write the coding.

    7. Double click the COMMAND button and write the coding.

    8. Go to Project→ Add to project→ File→ Add the student.cpp and student.h from the

    location where we saved.

    9. Create a new menu under edit menu give the name as Clear All.

    10. GO to class wizard and add the coding for clear all menus.

  • 37

    11. Go to Doc.h file and add the following declaration.

    i.e CStudent m_student

    12. Go to Doc.cpp and add the coding near the constructor.

    13. Override the serialization function.

    14. Check Whether the function UpdateControlsFromDoc() is updated in view.h.

    15. Build and test the applications.

    SERIALIZATION

    // student.h:

    #ifndef _INSIDE_VISUAL_CPP_STUDENT

    #define _INSIDE_VISUAL_CPP_STUDENT

    class CStudent : public CObject

    {

    DECLARE_SERIAL(CStudent)

  • 38

    public:

    CString m_strName;

    int m_nGrade;

    CStudent()

    {

    m_nGrade = 0;

    }

    CStudent(const char* szName, int nGrade) : m_strName(szName)

    {

    m_nGrade = nGrade;

    }

    CStudent(const CStudent& s) : m_strName(s.m_strName)

    {

    // copy constructor

    m_nGrade = s.m_nGrade;

    }

    const CStudent& operator =(const CStudent& s)

    {

    m_strName = s.m_strName;

    m_nGrade = s.m_nGrade;

    return *this;

    }

    BOOL operator ==(const CStudent& s) const

    {

    if ((m_strName == s.m_strName) && (m_nGrade == s.m_nGrade))

    {

    return TRUE;

    }

    else

    {

    return FALSE;

    }

    }

    BOOL operator !=(const CStudent& s) const

    {

    // Let’s make use of the operator we just defined!

    return !(*this == s);

    }

    virtual void Serialize(CArchive& ar);

    };

  • 39

    #endif // _INSIDE_VISUAL_CPP_STUDENT

    //student.cpp

    #include "stdafx.h"

    #include "student.h"

    IMPLEMENT_SERIAL(CStudent, CObject, 0)

    void CStudent::Serialize(CArchive& ar)

    {

    TRACE("Entering CStudent::Serialize\n");

    if (ar.IsStoring()) {

    ar m_strName >> m_nGrade;

    }

    }

    // serializeDoc.h

    public:

    CStudent m_student;

    // serializeDoc.cpp

    //Constructor

    CSerializeDoc::CSerializeDoc() : m_student("default value", 0)

    {

    // TODO: add one-time construction code here

    }

    // CSerializeDoc serialization

    void CSerializeDoc::Serialize(CArchive& ar)

    {

    if (ar.IsStoring())

    {

    // TODO: add storing code here

    }

    else

    {

    // TODO: add loading code here

  • 40

    }

    m_student.Serialize(ar);

    }

    // serializeView.h

    public:

    void UpdateControlsFromDoc();

    // serializeView.cpp

    void CSerializeView::OnEnter()

    {

    CSerializeDoc* pDoc = GetDocument();

    UpdateData(TRUE);

    pDoc->m_student.m_nGrade = m_nGrade;

    pDoc->m_student.m_strName = m_strName;

    }

    Š

    void CSerializeView::UpdateControlsFromDoc()

    {

    CSerializeDoc* pDoc = GetDocument();

    m_nGrade = pDoc->m_student.m_nGrade;

    m_strName = pDoc->m_student.m_strName;

    UpdateData(FALSE); // calls DDX

    }

    void CSerializeView::OnEditClearall()

    {

    // TODO: Add your command handler code here

    GetDocument()->m_student = CStudent(); // "blank" student object

    UpdateControlsFromDoc();

    }

  • 41