Вы находитесь на странице: 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
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 different
options 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,
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 <windows.h>
#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); //Forward
Decleration of WndProc function

//The main function from where the execution starts


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE
hPrevInstance,LPSTR szCmdLine,int iCmdShow)
{
HWND hwnd; //handle of the parent window
HDC hdc; //handle to the device context
MSG msg; //Message structure object
WNDCLASS wndclass; //wndclass onject
HWND button1, button2, hVscroll, hHscroll; // handle
foe vertical scroll, horizontal scroll, button1 and button2
RECT rect; //Rect structure
int 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;
}

/* This will create the main parent window */


hwnd =
CreateWindow(TEXT("Gurpreet"),TEXT("Windows
Programming 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 details


button */
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 end


extreme of the window*/
hHscroll = CreateWindow(TEXT("SCROLLBAR"),
(LPSTR)NULL,WS_CHILD | WS_VISIBLE | SBS_HORZ |
SBS_BOTTOMALIGN,rect.left,rect.top,rect.right-
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 end


extreme 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 the


WndProc function */
while(GetMessage(&msg,0,0,0))
{
DispatchMessage(&msg);
}
/* On successful execution it will return 0 */
return 0;
}

/* 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 what


action is to be taken corresponding to that */
switch(message)
{
/* This case would run when the mouse is moved
over the window */
case WM_MOUSEMOVE:
/* This check is for running this condition
only for one single time */
/* i.e. user will only see the messagebox
when 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 item
or 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 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 is
visible 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);
ShowWindow(edit_phoneno,SW_HIDE);
static_check = FALSE;
}

static_check2 = FALSE;
break;
/* This case would be running when the
Show 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 is


visible 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);

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);
/* This will add new items into the
list 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 :
"),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 when


the 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
{
MessageBox(hwnd,TEXT("Firstly clear the client
area"),TEXT("Warning"),MB_OK | MB_ICONWARNING);
}
break;

/* This case would be exectuted when


the 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 client
area"),TEXT("Warning"),MB_OK | MB_ICONWARNING);
}
break;

/* This case would be exectuted when


the stream menu item is pressed */
case ID_STUDENTINFO_STREAM:
if(static_check2 == FALSE)
{
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("Computer
Science"),WS_VISIBLE | WS_CHILD,
210,50+75,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 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;
}
else
{
MessageBox(hwnd,TEXT("Firstly clear the client
area"),TEXT("Warning"),MB_OK | MB_ICONWARNING);
}
break;

/* This case would be exectuted when


the 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_VISIBL
E | 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 when


the address menu item is pressed */
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 client
area"),TEXT("Warning"),MB_OK | MB_ICONWARNING);
}
break;

/* This case would be exectuted when


the 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
BLE | WS_CHILD,
210,50+175,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 when


the "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("Gurpreet
Singh"),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 |
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("Computer
Science"),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. :
"),WS_VISIBLE | SS_CENTER |
WS_CHILD,21,50+125,150,18,hwnd,NULL,hInstance,NULL)
;
edit_regno =
CreateWindow(TEXT("EDIT"),TEXT("10805721"),WS_VISIBL
E | 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_VISI
BLE | WS_CHILD,
210,50+175,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 when


the "about" menu item is pressed */
case ID_ABOUT:
MessageBox(hwnd,TEXT("This
application is devloped by Gurpreet Singh and is
Submitted to Mr.Gaurav Raj"),TEXT("About DP"),MB_OK |
MB_ICONINFORMATION);
break;

/* This case would be exectuted when


the "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 to
leave 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 */
return
DefWindowProc(hwnd,message,wparam,lparam);
}

Вам также может понравиться