Gurpreet Dp Documentation Windows

Embed Size (px)

Citation preview

  • 8/7/2019 Gurpreet Dp Documentation Windows

    1/22

    Design

    Problem -1

    SUBJECT

    CODE: CSE 361

    SUBMITTED BY:

    SUBMITTED TO:

    Name: Gurpreet Singh Lec. Mr.

    Gaurav Raj

    Roll No: 17

    Department of CSE

  • 8/7/2019 Gurpreet Dp Documentation Windows

    2/22

    Sec No: RA1805 LOVELY

    PROFESSIONAL

    Group No: G2

    UNIVERSITY

    Reg. No: 10805721

    PROBLEM:

    Create a win32 api based application which is displaying

    different information regarding the student such that

    student is able to display all the information regarding

    him/her.

    Expectations-

    1.Student must display a menu having the differentoptions corresponding to the data that they want to

    display.

    2.Student must display a message box that it is the

    version 1(or whatever, he choose), on the screen and

    he must display some data on this message box on

    the screen regarding the application developed.

    3.Student should display an output with variable font

    colors and background colors.

    4.Student should use all predefined window controls

    like Buttons etc.(that are in his course) in his code,

  • 8/7/2019 Gurpreet Dp Documentation Windows

    3/22

    while working on these controls he can display the

    information in client area or wherever he choose.

    5.Student should comment the code regarding every

    new block of code.

    6. Student can design his own icon or any other

    resource (if he wants, it is not compulsory).

    Student should submit the code, resource file and the

    executable file to the faculty

    Source Code

    #include#include"resource.h"#define ID_MYBUTTON1 1 //id of clear button#define ID_MYBUTTON2 2 //id of show details button#define IDC_HSCROLL 3 //id of horizontal scroll#define IDC_VERT 4 //id of vertical scroll#define ID_LISTBOX 5 //id of listbox

    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); //ForwardDecleration of WndProc function

    //The main function from where the execution starts

  • 8/7/2019 Gurpreet Dp Documentation Windows

    4/22

    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCEhPrevInstance,LPSTR szCmdLine,int iCmdShow){

    HWND hwnd; //handle of the parent window

    HDC hdc; //handle to the device contextMSG msg; //Message structure objectWNDCLASS wndclass; //wndclass onjectHWND button1, button2, hVscroll, hHscroll; // handle

    foe vertical scroll, horizontal scroll, button1 and button2RECT rect; //Rect structureint ver,hor; //stores the values returned by the scroll

    bars

    /* This section defines the properties of our class */wndclass.style = CS_HREDRAW | CS_VREDRAW;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.lpfnWndProc = WndProc;wndclass.lpszMenuName =

    MAKEINTRESOURCE(IDR_MENU1);

    wndclass.lpszClassName = TEXT("Gurpreet");wndclass.hCursor =LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));

    wndclass.hbrBackground =(HBRUSH)GetStockObject(GRAY_BRUSH);

    wndclass.hIcon =LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));

    wndclass.hInstance = hInstance;

    /* Now our class would be registered */if(!RegisterClass(&wndclass)){

    return 0;

  • 8/7/2019 Gurpreet Dp Documentation Windows

    5/22

    }

    /* This will create the main parent window */hwnd =

    CreateWindow(TEXT("Gurpreet"),TEXT("WindowsProgramming DP"),WS_OVERLAPPEDWINDOW |WS_VISIBLE /*| WS_VSCROLL | WS_HSCROLL*/ ,430,150,500,500,NULL,NULL,hInstance,NULL);

    /* The will create and show the clear button */button1 = CreateWindow(TEXT("BUTTON"),

    TEXT("CLEAR"), WS_CHILD | BS_PUSHBUTTON, 300, 350,

    100, 40, hwnd, (HMENU)ID_MYBUTTON1, hInstance,NULL);

    ShowWindow(button1,SW_SHOWNORMAL);

    /* This will create and show the second view detailsbutton */

    button2 = CreateWindow(TEXT("BUTTON"),TEXT("SHOW RECORD"), WS_CHILD | BS_PUSHBUTTON,

    100, 350, 150, 40, hwnd, (HMENU)ID_MYBUTTON2,hInstance, NULL);ShowWindow(button2,SW_SHOWNORMAL);

    /* This will get the coordinates of the window*/GetClientRect( hwnd, &rect);hor = GetSystemMetrics(SM_CXHTHUMB);ver = GetSystemMetrics(SM_CYVTHUMB);

    /*This will set the horizontal scrooll bar to the endextreme of the window*/

    hHscroll = CreateWindow(TEXT("SCROLLBAR"),(LPSTR)NULL,WS_CHILD | WS_VISIBLE | SBS_HORZ |SBS_BOTTOMALIGN,rect.left,rect.top,rect.right-

  • 8/7/2019 Gurpreet Dp Documentation Windows

    6/22

    ver,rect.bottom,hwnd,(HMENU)IDC_HSCROLL,(HANDLE)hInstance,NULL);

    SetScrollRange( hHscroll, SB_CTL, 0, 10, FALSE);

    /*This will set the vertical scrooll bar to the endextreme of the window*/

    hVscroll = CreateWindow(TEXT("Scrollbar"),(LPSTR)NULL,WS_CHILD | WS_VISIBLE | SBS_VERT |SBS_RIGHTALIGN,rect.left,rect.top,rect.right,rect.bottom-ver,hwnd,(HMENU)IDC_VERT,hInstance,NULL);

    SetScrollRange( hVscroll, SB_CTL, 0, 10, FALSE);

    /* This will make the scroll bars visible */ShowWindow(hHscroll,SW_SHOWNORMAL);ShowWindow(hVscroll,SW_SHOWNORMAL);

    /* To set a text on the window */hdc = GetDC(hwnd);SetBkColor(hdc,RGB(0,0,255));SetBkMode(hdc,OPAQUE);

    SetTextColor(hdc,RGB(255,255,0));SelectObject(hdc,GetStockObject(500));TextOut(hdc,150,5,TEXT("Windows Programming

    DP"),lstrlen(TEXT("Windows Programming DP")));ReleaseDC(hwnd,hdc);

    /* This will get and dispatch all the messages to theWndProc function */

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

    DispatchMessage(&msg);}/* On successful execution it will return 0 */return 0;

  • 8/7/2019 Gurpreet Dp Documentation Windows

    7/22

    }

    /* This is the window procedure function */LRESULT CALLBACK WndProc(HWND hwnd,UINT

    message,WPARAM wparam,LPARAM lparam){

    static BOOL i = TRUE;static BOOL static_check = FALSE, static_check2 =

    FALSE;static HWND static_name , edit_name;static HWND static_age,edit_age;static HWND static_course,edit_course;

    static HWND static_stream,edit_stream;static HWND static_cgpa,edit_cgpa;static HWND static_regno,edit_regno;static HWND static_address,edit_address;static HWND static_phoneno,edit_phoneno;static HWND static_clear;static HWND hlistbox;WNDCLASS wndclass;

    HWND hwndchild;

    HINSTANCE hInstance;

    hInstance =(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);

    /* This will check which message has come and whataction is to be taken corresponding to that */

    switch(message){

    /* This case would run when the mouse is movedover the window */

    case WM_MOUSEMOVE:

  • 8/7/2019 Gurpreet Dp Documentation Windows

    8/22

    /* This check is for running this conditiononly for one single time */

    /* i.e. user will only see the messageboxwhen he will first time execute the program */

    if(i == TRUE){

    /* This will show a greeting message */

    MessageBox(hwnd,TEXT("Hello"),TEXT("Welcome"),MB_OK| MB_ICONEXCLAMATION);

    i = FALSE;}

    break;/* This case would run if a menu item is pressed

    or a button is pressed */case WM_COMMAND:

    /* Since WPARAM contains which menu itemor button has benn clicked so deciding on the basis of that*/

    switch(wparam)

    {/* this case would be executed when the

    clear button is pressed */case ID_MYBUTTON1:

    /* If the list box is visible it woulddestroy its contents and hide it */

    if(static_check2 == TRUE){

    ShowWindow(hlistbox,SW_HIDE);

    SendMessage(hlistbox,LB_RESETCONTENT,0,0L);}

  • 8/7/2019 Gurpreet Dp Documentation Windows

    9/22

    /* if any static text or edit box isvisible then it would hide it */

    if(static_check == TRUE){

    ShowWindow(static_name,SW_HIDE);

    ShowWindow(edit_name,SW_HIDE);

    ShowWindow(static_age,SW_HIDE);

    ShowWindow(edit_age,SW_HIDE);

    ShowWindow(static_course,SW_HIDE);

    ShowWindow(edit_course,SW_HIDE);

    ShowWindow(static_stream,SW_HIDE);

    ShowWindow(edit_stream,SW_HIDE);

    ShowWindow(static_cgpa,SW_HIDE);

    ShowWindow(edit_cgpa,SW_HIDE);

    ShowWindow(static_regno,SW_HIDE);

    ShowWindow(edit_regno,SW_HIDE);

    ShowWindow(static_address,SW_HIDE);

    ShowWindow(edit_address,SW_HIDE);

    ShowWindow(static_phoneno,SW_HIDE);

  • 8/7/2019 Gurpreet Dp Documentation Windows

    10/22

    ShowWindow(edit_phoneno,SW_HIDE);static_check = FALSE;

    }

    static_check2 = FALSE;break;

    /* This case would be running when theShow details button is pressed */

    case ID_MYBUTTON2:/* If the list box is visible it would

    destroy its contents and hide it */

    if(static_check2 == TRUE){

    ShowWindow(hlistbox,SW_HIDE);

    SendMessage(hlistbox,LB_RESETCONTENT,0,0L);}

    /* if any static text or edit box isvisible then it would hide it */if(static_check == TRUE){

    ShowWindow(static_name,SW_HIDE);

    ShowWindow(edit_name,SW_HIDE);

    ShowWindow(static_age,SW_HIDE);

    ShowWindow(edit_age,SW_HIDE);

    ShowWindow(static_course,SW_HIDE);

  • 8/7/2019 Gurpreet Dp Documentation Windows

    11/22

    ShowWindow(edit_course,SW_HIDE);

    ShowWindow(static_stream,SW_HIDE);

    ShowWindow(edit_stream,SW_HIDE);

    ShowWindow(static_cgpa,SW_HIDE);

    ShowWindow(edit_cgpa,SW_HIDE);

    ShowWindow(static_regno,SW_HIDE);

    ShowWindow(edit_regno,SW_HIDE);

    ShowWindow(static_address,SW_HIDE);

    ShowWindow(edit_address,SW_HIDE);

    ShowWindow(static_phoneno,SW_HIDE);

    ShowWindow(edit_phoneno,SW_HIDE);static_check = FALSE;

    }

    /* This would make the list box */hlistbox =

    CreateWindow(TEXT("LISTBOX"), TEXT("Gurpreet"),WS_CHILD | LBS_SORT | LBS_STANDARD, 100, 50, 210,100, hwnd, (HMENU)ID_LISTBOX, hInstance, NULL);

    static_check2 = TRUE;/* This would show the list box */

    ShowWindow(hlistbox,SW_SHOWNORMAL);

  • 8/7/2019 Gurpreet Dp Documentation Windows

    12/22

    /* This will add new items into thelist box */

    SendMessage(hlistbox,LB_ADDSTRING,0,(LPARAM)TEXT("Name: Gurpreet Singh"));

    SendMessage(hlistbox,LB_ADDSTRING,0,(LPARAM)TEXT("Age: 21"));

    SendMessage(hlistbox,LB_ADDSTRING,0,(LPARAM)TEXT("Course: B.Tech(Hons.)"));

    SendMessage(hlistbox,LB_ADDSTRING,0,(LPARAM)TEXT("Stream: CSE"));

    SendMessage(hlistbox,LB_ADDSTRING,0,(LPARAM)TEXT("CGPA: 9.0"));

    SendMessage(hlistbox,LB_ADDSTRING,0,(LPARAM)TEXT("Reg. No. 10805721"));

    SendMessage(hlistbox,LB_ADDSTRING,0,(LPARAM)TEXT("Address: Satnampura, Phagwara"));

    SendMessage(hlistbox,LB_ADDSTRING,0,(LPARAM)TEXT("Phone No. 9779430631"));

    break;

    /* This case would be exectuted when

    the name menu item is pressed */case ID_STUDENTINFO_NAME:

    if(static_check2 == FALSE){

    static_name =CreateWindow(TEXT("STATIC"),TEXT("Student Name :

  • 8/7/2019 Gurpreet Dp Documentation Windows

    13/22

    "),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50,150,18,hwnd,NULL,hInstance,NULL);

    edit_name =

    CreateWindow(TEXT("EDIT"),TEXT("Gurpreet Singh"),WS_VISIBLE | WS_CHILD,210,50,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_check = TRUE;}else{

    MessageBox(hwnd,TEXT("Firstly clear the client

    area"),TEXT("Warning"),MB_OK | MB_ICONWARNING);}

    break;

    /* This case would be exectuted whenthe age menu item is pressed */

    case ID_STUDENTINFO_AGE:

    if(static_check2 == FALSE){

    static_age =CreateWindow(TEXT("STATIC"),TEXT("Student Age :"),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+25,150,18,hwnd,NULL,hInstance,NULL);

    edit_age =

    CreateWindow(TEXT("EDIT"),TEXT("21"),WS_VISIBLE |WS_CHILD,210,50+25,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_check = TRUE;}else

  • 8/7/2019 Gurpreet Dp Documentation Windows

    14/22

    {

    MessageBox(hwnd,TEXT("Firstly clear the clientarea"),TEXT("Warning"),MB_OK | MB_ICONWARNING);

    }break;

    /* This case would be exectuted whenthe course menu item is pressed */

    case ID_STUDENTINFO_COURSE:if(static_check2 == FALSE){

    static_course =CreateWindow(TEXT("STATIC"),TEXT("Student Course :"),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+50,150,18,hwnd,NULL,hInstance,NULL);

    edit_course =CreateWindow(TEXT("EDIT"),TEXT("B.Tech(Hons.)"),WS_VISIBLE | WS_CHILD,210,50+50,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_check = TRUE;}else{

    MessageBox(hwnd,TEXT("Firstly clear the clientarea"),TEXT("Warning"),MB_OK | MB_ICONWARNING);

    }

    break;

    /* This case would be exectuted whenthe stream menu item is pressed */

    case ID_STUDENTINFO_STREAM:if(static_check2 == FALSE)

  • 8/7/2019 Gurpreet Dp Documentation Windows

    15/22

    {static_stream =

    CreateWindow(TEXT("STATIC"),TEXT("Student Stream :"),WS_VISIBLE | SS_CENTER |

    WS_CHILD,21,50+75,150,18,hwnd,NULL,hInstance,NULL);

    edit_stream =CreateWindow(TEXT("EDIT"),TEXT("ComputerScience"),WS_VISIBLE | WS_CHILD,210,50+75,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_check = TRUE;}

    else{

    MessageBox(hwnd,TEXT("Firstly clear the clientarea"),TEXT("Warning"),MB_OK | MB_ICONWARNING);

    }break;

    /* This case would be exectuted when

    the cgpa menu item is pressed */case ID_STUDENTINFO_CGPA:

    if(static_check2 == FALSE){

    static_cgpa =CreateWindow(TEXT("STATIC"),TEXT("Student CGPA :"),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+100,150,18,hwnd,NULL,hInstance,NULL)

    ;edit_cgpa =

    CreateWindow(TEXT("EDIT"),TEXT("9.0"),WS_VISIBLE |WS_CHILD,210,50+100,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_check = TRUE;

  • 8/7/2019 Gurpreet Dp Documentation Windows

    16/22

    }else{

    MessageBox(hwnd,TEXT("Firstly clear the clientarea"),TEXT("Warning"),MB_OK | MB_ICONWARNING);}break;

    /* This case would be exectuted whenthe name "reg no." item is pressed */

    case ID_STUDENTINFO_REGNO:

    if(static_check2 == FALSE){static_regno =

    CreateWindow(TEXT("STATIC"),TEXT("Student Reg. No. :"),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+125,150,18,hwnd,NULL,hInstance,NULL);

    edit_regno =CreateWindow(TEXT("EDIT"),TEXT("10805721"),WS_VISIBLE | WS_CHILD,210,50+125,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_check = TRUE;}else{

    MessageBox(hwnd,TEXT("Firstly clear the client

    area"),TEXT("Warning"),MB_OK | MB_ICONWARNING);}break;

    /* This case would be exectuted whenthe address menu item is pressed */

  • 8/7/2019 Gurpreet Dp Documentation Windows

    17/22

    case ID_STUDENTINFO_ADDRESS:if(static_check2 == FALSE){

    static_address =

    CreateWindow(TEXT("STATIC"),TEXT("Student Address :"),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+150,150,18,hwnd,NULL,hInstance,NULL);

    edit_address =CreateWindow(TEXT("EDIT"),TEXT("Satnampura,Phagwara"),WS_VISIBLE | WS_CHILD,210,50+150,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_check = TRUE;}else{

    MessageBox(hwnd,TEXT("Firstly clear the clientarea"),TEXT("Warning"),MB_OK | MB_ICONWARNING);

    }break;

    /* This case would be exectuted whenthe phone no. menu item is pressed */

    case ID_STUDENTINFO_PHONENO:if(static_check2 == FALSE){

    static_phoneno =CreateWindow(TEXT("STATIC"),TEXT("Student Phone No. :

    "),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+175,150,18,hwnd,NULL,hInstance,NULL);

    edit_phoneno =CreateWindow(TEXT("EDIT"),TEXT("9779430631"),WS_VISI

  • 8/7/2019 Gurpreet Dp Documentation Windows

    18/22

    BLE | WS_CHILD,210,50+175,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_check = TRUE;}

    else{

    MessageBox(hwnd,TEXT("Firstly clear the clientarea"),TEXT("Warning"),MB_OK | MB_ICONWARNING);

    }break;

    /* This case would be exectuted whenthe "all info" menu item is pressed */case ID_STUDENTINFO_ALLINFO:

    if(static_check2 == FALSE){

    static_name =CreateWindow(TEXT("STATIC"),TEXT("Student Name :"),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50,150,18,hwnd,NULL,hInstance,NULL);

    edit_name =CreateWindow(TEXT("EDIT"),TEXT("GurpreetSingh"),WS_VISIBLE | WS_CHILD,210,50,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_age =CreateWindow(TEXT("STATIC"),TEXT("Student Age :

    "),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+25,150,18,hwnd,NULL,hInstance,NULL);

    edit_age =CreateWindow(TEXT("EDIT"),TEXT("21"),WS_VISIBLE |

  • 8/7/2019 Gurpreet Dp Documentation Windows

    19/22

    WS_CHILD,210,50+25,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_course =

    CreateWindow(TEXT("STATIC"),TEXT("Student Course :"),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+50,150,18,hwnd,NULL,hInstance,NULL);

    edit_course =CreateWindow(TEXT("EDIT"),TEXT("B.Tech(Hons.)"),WS_VISIBLE | WS_CHILD,210,50+50,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_stream =CreateWindow(TEXT("STATIC"),TEXT("Student Stream :"),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+75,150,18,hwnd,NULL,hInstance,NULL);

    edit_stream =CreateWindow(TEXT("EDIT"),TEXT("ComputerScience"),WS_VISIBLE | WS_CHILD,

    210,50+75,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_cgpa =CreateWindow(TEXT("STATIC"),TEXT("Student CGPA :"),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+100,150,18,hwnd,NULL,hInstance,NULL);

    edit_cgpa =

    CreateWindow(TEXT("EDIT"),TEXT("9.0"),WS_VISIBLE |WS_CHILD,210,50+100,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_regno =CreateWindow(TEXT("STATIC"),TEXT("Student Reg. No. :

  • 8/7/2019 Gurpreet Dp Documentation Windows

    20/22

    "),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+125,150,18,hwnd,NULL,hInstance,NULL);

    edit_regno =

    CreateWindow(TEXT("EDIT"),TEXT("10805721"),WS_VISIBLE | WS_CHILD,210,50+125,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_address =CreateWindow(TEXT("STATIC"),TEXT("Student Address :"),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+150,150,18,hwnd,NULL,hInstance,NULL)

    ;edit_address =

    CreateWindow(TEXT("EDIT"),TEXT("Old Stanampura,Phagwara"),WS_VISIBLE | WS_CHILD,210,50+150,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_phoneno =CreateWindow(TEXT("STATIC"),TEXT("Student Phone No. :

    "),WS_VISIBLE | SS_CENTER |WS_CHILD,21,50+175,150,18,hwnd,NULL,hInstance,NULL);

    edit_phoneno =CreateWindow(TEXT("EDIT"),TEXT("9779430631"),WS_VISIBLE | WS_CHILD,210,50+175,150,18,hwnd,NULL,NULL,hInstance,NULL);

    static_check = TRUE;}else{

    MessageBox(hwnd,TEXT("Firstly clear the clientarea"),TEXT("Warning"),MB_OK | MB_ICONWARNING);

  • 8/7/2019 Gurpreet Dp Documentation Windows

    21/22

    }break;

    /* This case would be exectuted when

    the "about" menu item is pressed */case ID_ABOUT:

    MessageBox(hwnd,TEXT("Thisapplication is devloped by Gurpreet Singh and isSubmitted to Mr.Gaurav Raj"),TEXT("About DP"),MB_OK |MB_ICONINFORMATION);

    break;

    /* This case would be exectuted whenthe "quit" menu item is pressed */

    case ID_QUIT:DestroyWindow(hwnd);break;

    }break;

    /* This case would execute when we click the

    cross button or whwn we double click the application icon*/case WM_DESTROY:

    MessageBox(hwnd,TEXT("You are about toleave the application"),TEXT("Bye......"),MB_OK |MB_ICONEXCLAMATION);

    PostQuitMessage(0);break;

    }/* windows has a default window procedure function

    *//* All the functionality which we havn't added is there

    in ths function */

  • 8/7/2019 Gurpreet Dp Documentation Windows

    22/22

    returnDefWindowProc(hwnd,message,wparam,lparam);}