Advertisement

Friday 6 May 2011

LAB PROGRAMS FOR VISUAL PROGRAMMING

1. Using Dialog Controls

#include <afxwin.h>

#include <afxdlgs.h>

#include”resource.h”

class myframe:public CFrameWnd

{

public:

myframe()

{

Create(0,”FileDialog”,WS_OVERLAPPEDWINDOW, rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));

}

void filereport()

{

CString str,temp;

CFileDialog fd(1,0,0,0,”TextFiles(*.txt)|*.txt|Allfiles|*.*||”);

fd.m_ofn.lpstrTitle=”Browse”;

if(fd.DoModal() ==IDOK)

{

temp=fd.GetPathName();

str=”Full path”;

str +=temp;

temp=fd.GetFileName();

str=”\nFile Name”;

str +=temp;

temp=fd.GetFileExt();

str=”\nFile Extension”;

str +=temp;

temp=fd.GetFileTitle();

str=”\nFile Title”;

str +=temp;

MessageBox(str,”File details”);

}

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd) ON_COMMAND(101,filereport)

END_MESSAGE_MAP()

class myapp: public CWinApp

{

public:

int InitInstance()

{

myframe *p;

p = new myframe;

p->ShowWindow(3);

m_pMainWnd=p;

return 1;

}

};

myapp a;

2. Creating Tree View Control

#include <afxwin.h>

#include<afxcmn.h>

class myframe:public CFrameWnd

{

private:

CTreeCtrl tree;

public:

myframe()

{

Create(0,”Tree view Control”);

}

int OnCreate(LPCREATESTRUCT I)

{

HTREEITEM lang,opersys,c,cpp,java;

CFrameWnd::OnCreate(I);

tree.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|TVS_HASLINES

|TVS_LINESATROOT |TVS_HASBUTTONS|TVS_SHOWSELALWAYS,

CRect(30,30,300,350),this,1);

lang=tree.InsertItem(“Computer Languages”,TVI_ROOT,TVI_SORT);

c=tree.InsertItem(“C”,lang,TVI_SORT);

tree.InsertItem(“TC”,c);

tree.InsertItem(“QC”,c);

tree.InsertItem(“MSC”,c);

cpp=tree.InsertItem(“C++”,lang,TVI_SORT);

tree.InsertItem(“VC++”,cpp);

tree.InsertItem(“BORLAND C++”,cpp);

java=tree.InsertItem(“Java”,lang,TVI_SORT);

tree.InsertItem(“VJ++”,java);

tree.InsertItem(“Symantec Cafe”,java);

tree.InsertItem(“Sun JDK”,java);

opersys=tree.InsertItem(“OPerating System”,TVI_ROOT,TVI_SORT);

tree.InsertItem(“Win95″,opersys);

tree.InsertItem(“Win98″,opersys);

tree.InsertItem(“WinXp”,opersys);

tree.InsertItem(“Vista”,opersys);

tree.InsertItem(“Win7.0″,opersys);

return 0;

}

int OnNotify(WPARAM w, LPARAM I, LRESULT *r)

{

HTREEITEM h;

CString str;

CWnd ::OnNotify(w,I,r);

NM_TREEVIEW*p=(NM_TREEVIEW*)I;

if(p->hdr.code==TVN_SELCHANGED)

{

h=tree.GetSelectedItem();

str=tree.GetItemText(h);

MessageBox(str,”You have Selected…”);

}

return 1;

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_WM_CREATE()

END_MESSAGE_MAP()

class myapp: public CWinApp

{  public:

int InitInstance()

{          myframe *p;

p = new myframe;

p->ShowWindow(3);

m_pMainWnd=p;

return 1;

}

};

myapp a;

3. Concept of Serialization

#include <afxwin.h>

#include <afxext.h>

#include “resource.h”

class date:public CObject

{

DECLARE_SERIAL(date)

private:

int mm,dd,yy;

public:

date()

{

dd=0;

mm=0;

yy=0;

}

void setdate(int d, int m, int y)

{

dd=d;

mm=m;

yy=y;

}

void Serialize(CArchive&ar)

{

CObject::Serialize(ar);

if(ar.IsStoring())

ar<<dd<<mm<<yy;

else

ar>>dd>>mm>>yy;

}

void displaydate(CDC *pdc)

{

if(dd==0&&mm==0&&yy==0)

return;

char str[30];

sprintf(str,”%d %d %d”,dd,mm,yy);

pdc->TextOut(10,10,str);

}

};

IMPLEMENT_SERIAL(date,CObject,1)

class myframe : public CFrameWnd

{

private:

CFile fp;

date mydate;

public:

myframe()

{

Create(0,”Serialize”,WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));

fp.Open(“Test.txt”,CFile::modeCreate|CFile::modeReadWrite);

}

void readdata()

{

CClientDC d(this);

fp.SeekToBegin();

CArchive ar(&fp,CArchive::load);

mydate.Serialize(ar);

mydate.displaydate(&d);

}

void writedata()

{

currentdate();

fp.SeekToBegin();

CArchive ar(&fp,CArchive::store);

mydate.Serialize(ar);

}

void currentdate()

{

CTime t;

int dd,yy,mm;

t = CTime::GetCurrentTime();

dd=t.GetDay();

mm=t.GetMonth();

yy=t.GetYear();

mydate.setdate(dd,mm,yy);

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_COMMAND(101,writedata)

ON_COMMAND(201,readdata)

END_MESSAGE_MAP()

class myapp: public CWinApp

{  public:

int InitInstance()

{          myframe *p;

p = new myframe;

p->ShowWindow(1);

m_pMainWnd=p;

return 1;

}

};

myapp a;

4. Program  to load a Toolbar

#include <afxwin.h>

#include <afxext.h>

#include “resource.h”

class myframe:public CFrameWnd

{

private:

CToolBar t;

public:

myframe()

{

Create(0,”Loading a Toolbar”);

}

int OnCreate(LPCREATESTRUCT I)

{

t.Create(this,WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_BORDER_LEFT|CBRS_BORDER_RIGHT|

CBRS_BORDER_TOP|CBRS_BORDER_BOTTOM);

t.LoadToolBar(IDR_TOOLBAR1);

return 0;

}

void drawline()

{

MessageBox(“Line”,”drawline”);

}

void drawrectangle()

{

MessageBox(“Rectangle”,”drawrectangle”);

}

void drawcircle()

{

MessageBox(“Circle”,”drawcircle”);

}

void drawtriangle()

{

MessageBox(“Triangle”,”drawtriangle”);

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_WM_CREATE()

ON_COMMAND(101,drawline)

ON_COMMAND(102,drawrectangle)

ON_COMMAND(103,drawcircle)

ON_COMMAND(104,drawtriangle)

END_MESSAGE_MAP()

class myapp: public CWinApp

{

public:

int InitInstance()

{

myframe *p;

p = new myframe;

p->ShowWindow(3);

m_pMainWnd=p;

return 1;

}

};

myapp a;

5. Working with menu – Choosing a menu item using keyboard accelerator

#include <afxwin.h>

#include “resource.h”

class myframe: public CFrameWnd

{

public:

myframe()

{

LoadAccelTable(

MAKEINTRESOURCE(IDR_ACCELERATOR1));

Create(0,”Accelerator”,WS_OVERLAPPEDWINDOW,rectDefault,

0,MAKEINTRESOURCE(IDR_MENU2));

}

void fun1()

{

MessageBox(“Reached here to draw a line “,”Title”);

}

void fun2()

{

MessageBox(“Reached here to draw a Rectangle “,”Title”);

}

void fun3()

{

MessageBox(“Reached here to draw a Circle”,”Title”);

}

void fun4()

{

MessageBox(“Reached here to draw a Triangle “,”Title”);

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_COMMAND(101,fun1)

ON_COMMAND(102,fun2)

ON_COMMAND(103,fun3)

ON_COMMAND(104,fun4)

END_MESSAGE_MAP()

class myapp: public CWinApp

{

public:

int InitInstance()

{

myframe *p;

p = new myframe;

p->ShowWindow(3);

m_pMainWnd=p;

return 1;

}

};

myapp a;


6. Program to display a simple menu

#include <afxwin.h>

#include “resource.h”

class myframe:public CFrameWnd

{

private:

CMenu menu;

public:

myframe()

{

Create(0,”Creating Menu Using LoadMenu and SetMenu”);

}

int OnCreate(LPCREATESTRUCT I)

{

CFrameWnd ::OnCreate(I);

menu.LoadMenu(IDR_MENU1);

SetMenu(&menu);

return 0;

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_WM_CREATE()

END_MESSAGE_MAP()

class myapp: public CWinApp

{

public:

int InitInstance()

{

myframe *p;

p = new myframe;

p->ShowWindow(1);

m_pMainWnd=p;

return 1;

}

}; myapp a;

7. Program to create a Simple Dialog box

#include <afxwin.h>

#include”resource.h”

class mydialog:public CDialog

{

public:

mydialog(int n):CDialog(n)

{

}

void OnOK()

{

CDialog::OnOK();

MessageBox(“you have pressed the OK button”,”OnOK handler”);

}

void OnCancel()

{

CDialog::OnCancel();

MessageBox(“you have pressed the cancel button”,”Oncancel handler”);

}

};

class myframe : public CFrameWnd

{

public:

myframe()

{

Create(0,”Simple Dialog Box”,WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));

}

void about()

{

mydialog d(IDD_DIALOG1);

d.DoModal();

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd) ON_COMMAND(101,about)

END_MESSAGE_MAP()

class myapp: public CWinApp

{

public:

int InitInstance()

{

myframe *p;

p = new myframe;

p->ShowWindow(3);

m_pMainWnd=p;

return 1;

}

};

myapp a;

8. Display “Hello” wherever you click the left mouse button

#include <afxwin.h>

class myframe:public CFrameWnd

{

public:

myframe()

{

Create(0,”On Single mouse button click”);

}

void OnLButtonDown(UINT flag, CPoint pt)

{

CClientDC d(this);

d.SetTextColor(RGB(0,0,255));

d.TextOut(pt.x,pt.y,”Hello”,5);

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_WM_LBUTTONDOWN()

END_MESSAGE_MAP()

class myapp: public CWinApp

{

public:

int InitInstance()

{

myframe *p;

p = new myframe;

p->ShowWindow(1);

m_pMainWnd=p;

return 1;

}

}; myapp a;

9. Program to get the status of shift keys and toggle keys

#include <afxwin.h>

class myframe : public CFrameWnd

{

public:

myframe()

{

Create(0,”Toggle and Shift key’s status”);

}

void OnPaint()

{

CPaintDC d(this);

int i=1;

CRect r;

GetClientRect(&r);

if(::GetKeyState(VK_SHIFT)<0)

{

d.TextOut(50,10*i,”Shift key is depressed”, strlen(“Shift key is depressed”));

i+=2;

}

if(::GetKeyState(VK_CONTROL)<0)

{

d.TextOut(50,10*i,”Control key is depressed”, strlen(“Control key is depressed”));

i+=2;

}

if(::GetKeyState(VK_MENU)<0)

{

d.TextOut(50,10*i,”Alt key is depressed”, strlen(“Alt key is depressed”));

i+=2;

}

if(::GetKeyState(VK_NUMLOCK)&0×01==1)

{

d.TextOut(50,10*i,”NUm lock is on”, strlen(“NUm lock is on”));

i+=2;

}

if(::GetKeyState(VK_NUMLOCK)<0)

{

d.TextOut(50,10*i,”Num lock is currently depressed”, strlen(“Num lockis currently depressed”));

i+=2;

}

if(::GetKeyState(VK_CAPITAL)&0×01==1)

{

d.TextOut(50,10*i,”Caps lock is on”, strlen(“Caps lock is on”));

i+=2;

}

if(::GetKeyState(VK_CAPITAL)<0)

{

d.TextOut(50,10*i,”Caps lock is currently depressed”, strlen(“Caps lock is currently depressed”));

i+=2;

}

if(::GetKeyState(VK_SCROLL)&0×01==1)

{

d.TextOut(50,10*i,”Scroll lock is on”, strlen(“Caps lock is on”));

i+=2;

}

if(::GetKeyState(VK_SCROLL)<0)

{

d.TextOut(50,10*i,”Scroll lock is currently depressed”, strlen(“Caps lock is currently depressed”));

i+=2;

}

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_WM_PAINT()

END_MESSAGE_MAP()

class myapp: public CWinApp

{

public:

int InitInstance()

{

myframe *p;

p = new myframe;

p->ShowWindow(1);

m_pMainWnd=p;

return 1;

}

};

myapp a;

0 comments:

Post a Comment