VB Program

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 120

VB

Page | 1 Visual Basic & VC++ Programming

1. Program to create a calculator Dim current As Double, previous As Double, choice As String, result As Double Private Sub Command1_Click (Index as Integer) Text1.Text = Text1.Text & Command1 (Index).Caption Current = Val (Text1.Text) End Sub Private Sub Command12_Click () Text1.Text = "" Previous = current Current = 0 Choice = "-" End Sub Private Sub Command14_Click () Text1.Text = "" End Sub Private Sub Command15_Click () Select Case choice Case "+" Result = previous + current Text1.Text = result Case "-"
Page | 2 Visual Basic & VC++ Programming

Result = previous - current Text1.Text = result Case "*" Result = previous * current Text1.Text = result Case "/" Result = previous / current Text1.Text = result End Select Current = result End Sub Private Sub Command16_Click () Text1.Text = "" Previous = current Current = 0 Choice = "+" End Sub Private Sub Command18_Click () Current = -current Text1.Text = current End Sub

Page | 3 Visual Basic & VC++ Programming

Private Sub Command4_Click () Text1.Text = "" Previous = current Current = 0 Choice = "/" End Sub Private Sub Command8_Click () Text1.Text = "" Previous = current Current = 0 Choice = "*" End Sub

Page | 4 Visual Basic & VC++ Programming

Output

Page | 5 Visual Basic & VC++ Programming

2. Factorial, prime, Fibonaacci-Demonstration for List box.

Dim i, c, a, b As Long Private Sub Cmdclear_Click() Text1.Text = Clear List2.Clear Text1.SetFocus End Sub Private Sub Cmdexit_Click() End End Sub Private Sub List1_Click() List2.Clear i = Val(Text1.Text) If List1.Text = "Fibonacci" Then a=0 b=1 c=1 List2.AddItem (a) Do While c < i List2.AddItem (c)
Page | 6 Visual Basic & VC++ Programming

c=a+b a=b b=c Loop End If If List1.Text = "Prime" Then Dim f As Boolean For a = 1 To i f = True For b = 2 To a / 2 If a Mod b = 0 Then f = False End If Next If f = True Then List2.AddItem (a) End If Next End If If List1.Text = "Factorial" Then b=1 For a = 1 To i - 1
Page | 7 Visual Basic & VC++ Programming

b=b+b*a Next List2.AddItem (b) End If End Sub

Page | 8 Visual Basic & VC++ Programming

Output

Page | 9 Visual Basic & VC++ Programming

3. String Operations

Dim str As String Private Sub CMDCOMPARE_Click() str = Text1.Text Text2.Text = StrReverse(str) End Sub Private Sub CMDLCASE_Click() str = Text1.Text Text2.Text = LCase(str) End Sub Private Sub CMDLEFT_Click() str = Text1.Text Text2.Text = Left(str, 8) End Sub Private Sub CMDLEN_Click() str = Text1.Text Text2.Text = Len(str) End Sub Private Sub CMDLTRIM_Click() str = Text1.Text
Page | 10 Visual Basic & VC++ Programming

Text2.Text = LTrim(str) End Sub Private Sub CMDMID_Click() str = Text1.Text Text2.Text = Mid(str, 3, 4) End Sub Private Sub CMDRIGHT_Click() str = Text1.Text Text2.Text = Right(str, 7) End Sub Private Sub CMDRTRIM_Click() str = Text1.Text Text2.Text = RTrim(str) End Sub Private Sub CMDTRIM_Click() str = Text1.Text Text2.Text = Trim(str) End Sub Private Sub CMDUCASE_Click() str = Text1.Text Text2.Text = UCase(str) End Sub
Page | 11 Visual Basic & VC++ Programming

Output

Page | 12 Visual Basic & VC++ Programming

4. Random file operations.

Dim i As Integer Dim j As Integer Dim k As Integer Dim play As playrec Private Type playrec name As String age As Integer cat As String End Type Private Sub Command1_Click() Text1.Text = Clear Text2.Text = Clear Text3.Text = Clear Text1.SetFocus Command1.Enabled = False Command2.Enabled = True End Sub Private Sub Command2_Click() i=i+1
Page | 13 Visual Basic & VC++ Programming

j=i play.name = Text1.Text play.age = Text2.Text play.cat = Text3.Text Put #1, , play Command2.Enabled = False Command1.Enabled = True End Sub Private Sub Command3_Click() If j >= i Then MsgBox ("No more record found") Exit Sub End If j=j+1 Get #1, j, play Text1.Text = play.name Text2.Text = play.age Text3.Text = play.cat End Sub Private Sub Command4_Click() If j <= 1 Then MsgBox ("No previous record")
Page | 14 Visual Basic & VC++ Programming

Exit Sub End If j=j-1 Get #1, j, play Text1.Text = play.name Text2.Text = play.age Text3.Text = play.cat End Sub Private Sub Command5_Click() Close #1 End End Sub Private Sub Form_Load() i=0 Open "play.text" For Random As #1 Len = 40 End Sub

Page | 15 Visual Basic & VC++ Programming

Output

Page | 16 Visual Basic & VC++ Programming

5. Ritch text box-creation of mini word processor.

Dim f, op As Boolean Private Sub Center_Click() Text1.Alignment = 2 End Sub Private Sub Copy_Click() Clipboard.SetText (Text1.SelText) End Sub

Private Sub Cut_Click() Clipboard.Clear Clipboard.SetText = (Text1.selRTF) Text1.SelStart = Clear End Sub Private Sub Exit_Click()

If f = True And op = True Then If MsgBox("do u want to save?" + vbQuestion + vbYesNo) = vbYes Then Text1.savefile (cd1.FileName) End
Page | 17 Visual Basic & VC++ Programming

Else End End If ElseIf f = True Then If megbox("do u want to save?" + vbQuestion + vbYesNo) = vbYes Then cd1.showsave Text1.savefile (cd1.FileName) End End If Else End End If End Sub

Private Sub Font_Click() cd1.CancelError = True On Error GoTo er cd1.flags = cd1vfboth Or cdcfeffects cd1.showfont With Text1 .FontName = cd1.FontName .FontSize = cd1.FontSize
Page | 18 Visual Basic & VC++ Programming

.FontBold = cd1.FontBold .FontItalic = cd1.FontItalic .FontStrikethru = cd1.FontStrikethru .ForeColor = cd1.Color End With er: End Sub

Private Sub Left_Click() Text1.Alignment = 0 End Sub

Private Sub open_Click() op = True If f = True Then If MsgBox("do u want to save?", vbQuestion + vbYesNo) = vbYes Then cd1.showsave Text1.savefile (cd1.FileName) f = False cd1.showopen Text1.loadfile (cd1.FileName) End If
Page | 19 Visual Basic & VC++ Programming

Else cd1.showopen Text1.loadfile (cd1.FileName) Exit Sub er: End If End Sub

Page | 20 Visual Basic & VC++ Programming

Output

Page | 21 Visual Basic & VC++ Programming

6. Demonstration of Timer-Traffic signal.

Private Sub Timer1_Timer() Me.Caption = "Traffic Signal" + "Stop..." Shape1.FillColor = vbRed Shape1.BorderColor = vbRed Shape2.FillColor = vbBlack Shape2.BorderColor = vbYellow Shape3.FillColor = vbBlack Shape3.BorderColor = vbGreen Timer2.Enabled = True Timer1.Enabled = False End Sub Private Sub Timer2_Timer() Me.Caption = "Traffic Signal" + "Ready to Go..." Shape1.FillColor = vbBlack Shape1.BorderColor = vbRed Shape2.FillColor = vbYellow Shape2.BorderColor = vbYellow Shape3.FillColor = vbBlack Shape3.BorderColor = vbGreen
Page | 22 Visual Basic & VC++ Programming

Timer3.Enabled = True Timer2.Enabled = False End Sub Private Sub Timer3_Timer() Me.Caption = "Traffic Signal" + " Go..." Shape1.FillColor = vbBlack Shape1.BorderColor = vbRed Shape2.FillColor = vbBlack Shape2.BorderColor = vbYellow Shape3.FillColor = vbGreen Shape3.BorderColor = vbGreen Timer1.Enabled = True Timer3.Enabled = False End Sub

Page | 23 Visual Basic & VC++ Programming

Output

Page | 24 Visual Basic & VC++ Programming

7. Menu driven program for Graphical operation-for drawing different figures different color scheme, fill style, borders, free hand drawing using mouse. Keyboard drawing.

Private Sub box_Click() Me.Cls Dim i As Integer Scale (0, 0)-(639, 199) For i = 1 To 65 Step 5 Line (5 * i, i)-(639 - 5 * i, 199 - i), , B Next i End Sub Private Sub circle_Click() Me.Cls Circle (2200, 1400), 1000, vbBlue End Sub Private Sub conscir_Click() Me.Cls Dim r As Integer start = 100 ending = Me.Height - 3000 For r = start To ending
Page | 25 Visual Basic & VC++ Programming

Circle (Me.Width / 2, Me.Height / 2), r, RGB(Rnd * 255, Rnd * 255, Rnd * 255) Next r End Sub Private Sub exit_Click() End End Sub Private Sub fillbox_Click() Me.Cls Dim i As Integer Scale (0, 0)-(639, 199) For i = i To 65 Step 5 Dim code code = QBColor(i Mod 10) Line (5 * i, i)-(639 - 5 * i, 199 - i), code, BF Next i End Sub

Page | 26 Visual Basic & VC++ Programming

Output

Page | 27 Visual Basic & VC++ Programming

8. Bank operations using Data Object.

Module 1 public flag as Integer public con as Database public rec as Recordset MDIForm1 Public sub connect () Set con =OpenDatabase(c:\documents and setting\user19\desktop\ramzvb\bank.mdb) End Sub Private sub delete_click () Flag=5 Load frmbankadd Frmbnkadd.visible Frmbnkadd.cmdadd=false Frmbnkadd.cmdupdate.visible=false End Sub Private sub deposit_click() Flag=2 Load frmbnkamt
Page | 28 Visual Basic & VC++ Programming

Frmbnkamt.visible=true Frmbnkamt.caption=DEPOSIT End Sub Private sub exit _click () End End sub Private sub new_click Flag=1 Load frmbnkadd Frmbnkadd.visible=true Frmbnkadd.cmddelete.visible=false Frmbnkadd.cmdupdate.visible=false End Sub Private sub update_click() Flag=4 Load frmbnkadd Frmbnkadd.visible=true Frmbnkadd.cmdadd.visible=false Frmbnkadd.cmddelete.visible=false End Sub Private sub view_click() Flag=7
Page | 29 Visual Basic & VC++ Programming

Load frmbnkview Frmbnkview.visible=true End Sub Private sub viewall_click() Flag=6 Load frmbnkview Frmbnkview.visible=true End Sub Private sub withdraw_click() Flag=3 Load frmbnkamt Frmbnkamt. visible=true Frmbnkamt.Caption=WITHDRAW End Sub FRMBNKADD Dim AN%,t$, a as Variant Private sub cmdadd_click() If radiosav.value=true then t=SAVINGS elseif radiocurr.value=true then t=CURRENT End If
Page | 30 Visual Basic & VC++ Programming

Set rec=con.OpenRecordset(select *from bank where acctype=&t&order by accno) If rec.RecordCount=0 then If t=SAVINGthen An=1 Else If=Currentthen an=101 End If Else Rec.MoveLast An=rec.fields(5)+1 End If If txtname.Text=Or txtocc.Text=Or txtamt.Text=Or Txtadd.Text= then MsgBoxPlease enter values Else Con.execute(insert into bank values(&txtname.text&,&txtocc.text&,&t&,val(txtamt.text) &,&txtadd.text&,&an&);) MsgBox Your account no:is&an Call clearfields End If End sub
Page | 31 Visual Basic & VC++ Programming

Private sub cmdcancel_click() Txtname.text=clear Txtocc.text=clear Txtamt.text=clear Txtadd.text=clear Radiosav.value=False Radiocurr.value=False End sub Private sub cmdexit_click() Unload Me End sub Public Sub clearfields() Txtname.Text=Clear Txtocc.Text=Clear Txtamt.text=clear Txtadd.text=clear Radiosav.value=False Radiocurr.value=False End sub Private sub cmdupdate_click() If radiosav.value=true then t=savings
Page | 32 Visual Basic & VC++ Programming

Else If radiocurr.value=true then T=current End If Con.execute(update bank set accno=&val(txtno.text)&,name=&txtname.text&,acctype=&t&,occupation= ,&txtocc.text&, address=&txtadd.text&,initamt=&val(txtamt.text)&where accno=&a&;) MsgBoxrecord updated Clearfields Unload Me End sub Private Sub form_load() If flag=1 then Txtno.visible=False Lblno. visible=False ElseIf flag=4 or flag=5 then Lbl:a=InputBox(Enter accno:) If a<>then set rec=con.OpenRecordset(select*from bank where accno=&a&;) If rec.RecordCount=0 then MsgBoxNO SUCH RECORD GoTo lbl Else Setvalue
Page | 33 Visual Basic & VC++ Programming

End If Else MsgBoxPlease enter accno Goto lb1 End If End If End Sub public sub setvalue() txtname=rec.fields(0) txttocc=rec.fields(1) txtamt=rec.fields(3) txtadd=rec.fields(4) txtno=rec.fields(5) If rec.fields(2) = "savings" then radiosav.value = true Else radiocurr.value = true End If End Sub private sub cmdexit_click() Unload Me End Sub
Page | 34 Visual Basic & VC++ Programming

private sub cmdok_click() If txt1.text<>""And txt2.text<>""then set rec=con.OpenRecordset("select * from bank where accno=&text1.text&";") If rec.RecordCount = 0 then MsgBox"No such account" Else If flag=2 then con.execute("update bank setr initamt + "&text2.text&";") MsgBox"DEPOSIT SUCCESSFULLY" ElseIf flag = 3 then If rec.fields(3)-val(text2.text)<1000 then MsgBox"cant withdraw-not enough balance" Else con.execute("update bank setr initamt =initamt- "&text2.text&" where accno="&text1.text&";") MsgBox"SUCCESSFULLY WITHDRAWN" End If End If End If text1.text="" text2.text=""
Page | 35 Visual Basic & VC++ Programming

Else MsgBox"Please enter the value" End If End Sub frmbnkview private sub cmdexit_click() Unload Me End Sub private sub from_load() s$="accno"&vb Tab&"name" Tab&"occupation" &vb Tab&"type" &vb Tab&"amount" &vb Tab&"address" MSFlexGrid1.AddItem(s) rec.MovNext s="" Wend ElseIf flag=7 then n=InputBox("enter no") set rec = con.OpenRecordset("select *fram bank where accno="&n&";") If rec.RecordCount 0 then MsgBox"No such record" Else s$=rec.fields(5) &vb Tab&rec.fields(0) &vb Tab&rec.fields(1) &vb Tab&rec.fields(2) &vb Tab&rec.fields(3) &vb Tab&rec.fields(4)
Page | 36 Visual Basic & VC++ Programming

MSFlexGrid1.AddItem(s) End If End If End Sub private sub MDIfrom_Load() connect End Sub

Page | 37 Visual Basic & VC++ Programming

Output

Page | 38 Visual Basic & VC++ Programming

9. Demonstration for ADODB. Dim con As ADODB.Connection Dim res As ADODB.Recordset Private Sub Command1_Click() res.Open "SHOP", con, adOpenDynamic, adLockOptimistic res.AddNew res.Fields(0).Value = Text1.Text res.Fields(1).Value = Text2.Text res.Fields(2).Value = Text3.Text res.Update res.Close End Sub Private Sub Command2_Click() Dim cc As Integer cc = InputBox("Enter the code of cd to be edited") SqL = "Select * from SHOP where code=" & cc & " " res.Open SqL, con, adOpenKeyset, adLockOptimistic Text1.Text = res.Fields(0) Text2.Text = res.Fields(1) Text3.Text = res.Fields(2) res.Close
Page | 39 Visual Basic & VC++ Programming

End Sub Private Sub Command3_Click() Dim cc As Integer cc = InputBox("Enter the code of cd to be deleted") SqL = "select * from SHOP where code=" & cc & "" res.Open SqL, con, adOpenKeyset, adLockOptimistic Text1.Text = res.Fields(0) Text2.Text = res.Fields(1) Text3.Text = res.Fields(2) res.Delete res.Close End Sub Private Sub Command4_Click() res.Open "SHOP", con, adOpenKeyset, adLockOptimistic res.Fields(0) = Val(Text1.Text) res.Fields(1) = Text2.Text res.Fields(2) = Text3.Text res.Update res.Close End Sub Private Sub Command5_Click() Dim cc As Integer
Page | 40 Visual Basic & VC++ Programming

cc = InputBox("enter the cd code") SqL = "select * from SHOP where code=" & cc & "" res.Open "SHOP", con, adOpenKeyset, adLockOptimistic If res.EOF Then MsgBox ("sorry no record") Else Text1.Text = res.Fields(0) Text2.Text = res.Fields(1) Text3.Text = res.Fields(2) End If res.Close End Sub Private Sub Command6_Click() End End Sub Private Sub Form_Initialize() Set con = New ADODB.Connection Set res = New ADODB.Recordset con.Provider = "Microsoft.Jet.OLEDB.4.0" con.Open "C:\accessdb\Film1.mdb" End Sub

Page | 41 Visual Basic & VC++ Programming

Output

Page | 42 Visual Basic & VC++ Programming

VC++

Page | 43 Visual Basic & VC++ Programming

10. Program to draw line, rectangle, ellipse, pie, and circle.

#include "stdafx.h" #include "MainFrame.h" BEGIN_MESSAGE_MAP(CMainFrame , CFrameWnd) ON_WM_LBUTTONDOWN() ON_WM_CREATE() ON_WM_MOUSEMOVE() ON_WM_DESTROY() ON_COMMAND(ID_LINE , Line) ON_COMMAND(ID_RECTANGLE , Rectangle) ON_COMMAND(ID_ELLIPSE , Ellipse) ON_COMMAND(ID_PIE , Pie) ON_COMMAND(ID_CIRCLE , Circle) END_MESSAGE_MAP() CMainFrame::CMainFrame() { CString m; Create(0 , "SHAPES"); m_pDC = new CClientDC(this); m_nShape = NONE;
Page | 44 Visual Basic & VC++ Programming

m_nColor = 0; }

CMainFrame::~CMainFrame() { } void CMainFrame::OnLButtonDown(UINT flag , CPoint pt) { m_OldPoint = m_NewPoint = pt; } void CMainFrame::Circle() { m_nShape = CIRCLE; } void CMainFrame::Ellipse() { m_nShape = ELLIPSE; } void CMainFrame::Line() { m_nShape = LINE; }
Page | 45 Visual Basic & VC++ Programming

void CMainFrame::Pie() { m_nShape = PIE; } void CMainFrame::Rectangle() { m_nShape = RECTANGLE; } void CMainFrame::OnNcLButtonDown(UINT flag , CPoint pt) { switch(flag) { case HTCAPTION: MessageBox("This is the caption bar" , "cursor"); break; case HTCLOSE: MessageBox("This is the close button" , "cursor"); DestroyWindow(); break; case HTGROWBOX: MessageBox("This is the restore button" , "cursor");
Page | 46 Visual Basic & VC++ Programming

break; case HTVSCROLL: MessageBox("This is the vertical scroll bar" , "cursor"); break; case HTHSCROLL: MessageBox("This is the horizontal scroll bar" , "cursor"); break; case HTMENU: MessageBox("This is the menu" , "cursor"); break; case HTREDUCE: MessageBox("This is the minimize button" , "cursor"); break; case HTSYSMENU: MessageBox("This is the system menu" , "cursor"); break; case HTZOOM: MessageBox("This is the maximize button" , "cursor"); break; } }

Page | 47 Visual Basic & VC++ Programming

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { CMenu myMenu; tbl.Create(this , WS_CHILD|WS_VISIBLE|CBRS_TOP); myMenu.LoadMenu(IDR_MENU1); SetMenu(&myMenu); return CFrameWnd::OnCreate(lpCreateStruct); } void CMainFrame::OnMouseMove(UINT flag , CPoint point) { if(flag == MK_LBUTTON) { //m_pDC = new CClientDC(this); m_pDC->SetROP2(R2_NOTXORPEN); RECT rt = { 40, 20, 226, 144 }; switch(m_nShape) { case LINE: m_pDC->MoveTo(m_OldPoint); m_pDC->LineTo(m_NewPoint); m_pDC->MoveTo(m_OldPoint); m_pDC->LineTo(point);
Page | 48 Visual Basic & VC++ Programming

m_NewPoint = point; break; case RECTANGLE: m_pDC->Rectangle(m_OldPoint.x , m_OldPoint.y , m_NewPoint.x , m_NewPoint.y); m_pDC->Rectangle(m_OldPoint.x , m_OldPoint.y , point.x , point.y); m_NewPoint = point; break; case ELLIPSE: m_pDC->Ellipse(m_OldPoint.x , m_OldPoint.y , m_NewPoint.x , m_NewPoint.y); m_pDC->Ellipse(m_OldPoint.x , m_OldPoint.y , point.x , point.y); m_NewPoint = point; break; case PIE: m_pDC->Pie(&rt , m_OldPoint , m_NewPoint); m_pDC->Pie(&rt , m_OldPoint , point); m_NewPoint = point; break; case CIRCLE: m_pDC->Ellipse(m_OldPoint.x - 55 , m_OldPoint.y + 55 , m_NewPoint.x + 55, m_NewPoint.y - 55);
Page | 49 Visual Basic & VC++ Programming

m_pDC->Ellipse(m_OldPoint.x - 55 , m_OldPoint.y + 55, point.x + 55, point.y - 55); m_NewPoint = point; break;

default: break; } } CFrameWnd::OnMouseMove(flag, point); } #include "stdafx.h" #include "MyApp.h" #include "MainFrame.h" CMyApp theApp; CMyApp::CMyApp() { } CMyApp::~CMyApp() { } int CMyApp::InitInstance()
Page | 50 Visual Basic & VC++ Programming

{ CMainFrame* pMyFrame = new CMainFrame; pMyFrame->ShowWindow(3); m_pMainWnd = pMyFrame; return 1; } #if !defined(AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577 AF__INCLUDED_) #define AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577AF__INC LUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CMainFrame : public CFrameWnd { CClientDC* m_pDC; CPoint m_NewPoint , m_OldPoint; int m_nShape , m_nColor ; enum {NONE =0 , LINE , RECTANGLE , ELLIPSE , PIE , CIRCLE}; CToolBar tbl; void Circle();
Page | 51 Visual Basic & VC++ Programming

void Ellipse(); void Line(); void Pie(); void Rectangle(); public: CMainFrame(); virtual ~CMainFrame(); void OnLButtonDown(UINT flag , CPoint pt); void OnNcLButtonDown(UINT flag , CPoint pt); int OnCreate(LPCREATESTRUCT lpCreateStruct); void OnMouseMove(UINT flag , CPoint pt); DECLARE_MESSAGE_MAP() }; #endif // !defined(AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577 AF__INCLUDED_) #if !defined(AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__IN CLUDED_) #define AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__INCLUDE D_ #if _MSC_VER > 1000 #pragma once
Page | 52 Visual Basic & VC++ Programming

#endif // _MSC_VER > 1000 class CMyApp : public CWinApp { public: CMyApp(); virtual ~CMyApp(); int InitInstance(); }; #endif // !defined(AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__IN CLUDED_)

Page | 53 Visual Basic & VC++ Programming

Output

Page | 54 Visual Basic & VC++ Programming

11. Loading different menus at run time (large and small menu)

#include<afxwin.h> #include resource.h #define SMALL 1 #define LARGE 2 Class myframe:public CFrameWnd { Private: CMenu smallmenu,largemenu; Int menutype; Int w,c; Public: Myframe() { Create(0,Menu,WS_OVERLAPPEDWINDOW_rectDefault,0,MAKEINTRESO URCE(IDR_MENU1)); W=2; C=RGB(10,120,250); }
Page | 55 Visual Basic & VC++ Programming

Int OnCreate(LPCREATESTRUCT 1) { CFrameWnd::OnCreate(1); Smallmenu.LoadMenu(IDR_MENU1); Largemenu. LoadMenu(IDR_MENU1); SetMenu(&smallmenu); Menutype=SMALL; Return 0; } Void shorter() { SetMenu(&smallmenu); DrawMenuBar(); Menutype=SMALL: } Void longer() { SetMenu(&largemenu); DrawMenuBar(); Menutype=LARGE; } Void line()
Page | 56 Visual Basic & VC++ Programming

{ CPen pn; CClientDC *p=new CClientDC(this); Pn.CreatePen(PS_SOLID,w,c); p->SelectObject(&pn); p->MoveTo(32,100); p->LineTo(34,16); } Void rectangle() { CPen pn; CClientDc *p=new CClientDC(this); Pn.CreatePen(PS_SOLID,w,c); p->SelectObject(&pn); p->Rectangle(125,46,54,78); } Void circle() { CPen pn; CClientDc *p=new CClientDC(this); Pn.CreatePen(PS_SOLID,w,c); p->SelectObject(&pn);
Page | 57 Visual Basic & VC++ Programming

p->Ellipse(25,46,54,78); } Void red() { C=RGB(255,0,0); } Void green() { C=RGB(0,255,0); } Void blue() { C=RGB(0,0,255); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(myframe,CFrameWnd) ON_WM_CREATE() ON_COMMAND(ID_DRAW_LINE,line) ON_COMMAND(ID_DRAW_RECTANGLE,rectangle) ON_COMMAND(ID_DRAW_CIRCLE,circle) ON_COMMAND(ID_COLOR_RED,red)
Page | 58 Visual Basic & VC++ Programming

ON_COMMAND(ID_COLOR_GREEN,green) ON_COMMAND(ID_COLOR_BLUE,blue) ON_COMMAND(ID_SHORTER,shorter) ON_COMMAND(ID_LONGER,longer) 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;

Page | 59 Visual Basic & VC++ Programming

Output

Page | 60 Visual Basic & VC++ Programming

12. Programs showing the data entered in the different controls to a message box.

#include <AFXWIN.H> class CMainFrame : public CFrameWnd { CEdit e1 , e2; CStatic st1 , st2; CButton b[1]; public: CMainFrame(); int OnCreate(LPCREATESTRUCT lpCreateStruct); void Show(); DECLARE_MESSAGE_MAP() }; CMainFrame::CMainFrame() { CString m; m = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW , 0 , (HBRUSH)::GetStockObject(LTGRAY_BRUSH) , 0); Create(m , "Edit"); }
Page | 61 Visual Basic & VC++ Programming

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { st1.Create("Course" , WS_CHILD|WS_VISIBLE|SS_RIGHT|SS_CENTERIMAGE, CRect(100 , 20 , 180 , 45) , this , 1 ) e1.CreateEx(WS_EX_CLIENTEDGE,"EDIT" , "" , WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL , CRect(200 , 20 , 400 , 50) , this , 2 ); e2.CreateEx(WS_EX_CLIENTEDGE,"EDIT" , "" , WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL , CRect(420 , 20 , 620 , 50) , this , 9 ); b[0].Create("Show" , BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE, CRect(250 , 220 , 300 , 250) , this , 6 ); return 0; } void CMainFrame::Show() { CString s , t , p , t1 , t2; st1.GetWindowText(s); e1.GetWindowText(t1); e2.GetWindowText(t2); p = s + "\t" + t1 + "\t" + t2; MessageBox(p , "Control" , 1); }

Page | 62 Visual Basic & VC++ Programming

BEGIN_MESSAGE_MAP(CMainFrame , CFrameWnd) ON_WM_CREATE() ON_COMMAND(6 , Show) END_MESSAGE_MAP() class CExerciseApp : public CWinApp { public: virtual BOOL InitInstance(); }; BOOL CExerciseApp::InitInstance() { m_pMainWnd = new CMainFrame; m_pMainWnd->ShowWindow(SW_NORMAL); return TRUE; } CExerciseApp theApp;

Page | 63 Visual Basic & VC++ Programming

Output

Page | 64 Visual Basic & VC++ Programming

13. Programs illustrate the use of font and color dialog box.

#include "stdafx.h" #include "PGM 16.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) //}}AFX_MSG_MAP END_MESSAGE_MAP() CMainFrame::CMainFrame() { } CMainFrame::~CMainFrame() {


Page | 65 Visual Basic & VC++ Programming

} BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; return TRUE; } #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG #include "stdafx.h" #include "PGM 16.h" #include "MainFrm.h" #include "PGM 16Doc.h" #include "PGM 16View.h"
Page | 66 Visual Basic & VC++ Programming

#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif BEGIN_MESSAGE_MAP(CPGM16App, CWinApp) //{{AFX_MSG_MAP(CPGM16App) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) //}}AFX_MSG_MAP ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) END_MESSAGE_MAP() CPGM16App::CPGM16App() { } CPGM16App theApp; BOOL CPGM16App::InitInstance() { SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) CSingleDocTemplate* pDocTemplate;
Page | 67 Visual Basic & VC++ Programming

pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CPGM16Doc), RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(CPGM16View)); AddDocTemplate(pDocTemplate); CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); if (!ProcessShellCommand(cmdInfo)) return FALSE; m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } class CAboutDlg : public CDialog { public: CAboutDlg(); protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support protected:
Page | 68 Visual Basic & VC++ Programming

DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { }

void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() void CPGM16App::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); } #include "stdafx.h" #include "PGM 16.h" #include "PGM 16Doc.h" #ifdef _DEBUG #define new DEBUG_NEW
Page | 69 Visual Basic & VC++ Programming

#undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif IMPLEMENT_DYNCREATE(CPGM16Doc, CDocument) BEGIN_MESSAGE_MAP(CPGM16Doc, CDocument) END_MESSAGE_MAP() CPGM16Doc::CPGM16Doc() { } CPGM16Doc::~CPGM16Doc() { } BOOL CPGM16Doc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE;

return TRUE; } void CPGM16Doc::Serialize(CArchive& ar) { if (ar.IsStoring())


Page | 70 Visual Basic & VC++ Programming

{ } else { } } #ifdef _DEBUG void CPGM16Doc::AssertValid() const { CDocument::AssertValid(); }

void CPGM16Doc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); } #endif //_DEBUG #include "stdafx.h" #include "PGM 16.h" #include "PGM 16Doc.h" #include "PGM 16View.h" #ifdef _DEBUG
Page | 71 Visual Basic & VC++ Programming

#define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif IMPLEMENT_DYNCREATE(CPGM16View, CView) BEGIN_MESSAGE_MAP(CPGM16View, CView) //{{AFX_MSG_MAP(CPGM16View) ON_COMMAND(ID_DIALOG_FONT, OnDialogFont) ON_COMMAND(ID_DIALOG_COLOR, OnDialogColor) END_MESSAGE_MAP() CPGM16View::CPGM16View() { } CPGM16View::~CPGM16View() { } BOOL CPGM16View::PreCreateWindow(CREATESTRUCT& cs) { return CView::PreCreateWindow(cs); } void CPGM16View::OnDraw(CDC* pDC) {
Page | 72 Visual Basic & VC++ Programming

CPGM16Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); } #ifdef _DEBUG void CPGM16View::AssertValid() const { CView::AssertValid(); } void CPGM16View::Dump(CDumpContext& dc) const { CView::Dump(dc); } CPGM16Doc* CPGM16View::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPGM16Doc))); return (CPGM16Doc*)m_pDocument; } #endif //_DEBUG void CPGM16View::OnDialogFont() { CClientDC dc(this); CFontDialog dFont;
Page | 73 Visual Basic & VC++ Programming

CString fc , st; CFont Font; COLORREF rgb; int s , w; BOOL sk , u , b , i; Invalidate();

if(IDOK == dFont.DoModal()) { fc = dFont.GetFaceName(); s = dFont.GetSize() / 10; rgb = dFont.GetColor(); w = dFont.GetWeight(); sk = dFont.IsStrikeOut(); u = dFont.IsUnderline(); b = dFont.IsBold(); i = dFont.IsItalic(); Font.CreateFont(s , 15 , 0 , 0 , w , i , u , sk , 0 , 0 , 0 , 0 , 0 , fc); dc.SelectObject(&Font); dc.TextOut(50 , 50 , "FONT STYLE DEMO" , strlen("FONT STYLE DEMO")); }
Page | 74 Visual Basic & VC++ Programming

} void CPGM16View::OnDialogColor() { CColorDialog cd; CRect rt; CClientDC dc(this); COLORREF clr; if(IDOK == cd.DoModal()) { clr = cd.GetColor(); CBrush myBrush(clr); GetClientRect(&rt); dc.FillRect(&rt , &myBrush); }

} #include "stdafx.h" #if !defined(AFX_MAINFRM_H__4A4E9309_6D70_4216_ACF3_50122F106283__ INCLUDED_) #define AFX_MAINFRM_H__4A4E9309_6D70_4216_ACF3_50122F106283__INCLUD ED_


Page | 75 Visual Basic & VC++ Programming

#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CMainFrame : public CFrameWnd { protected: // create from serialization only CMainFrame(); DECLARE_DYNCREATE(CMainFrame) public: public: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); public: virtual ~CMainFrame(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: DECLARE_MESSAGE_MAP() }; #endif // !defined(AFX_MAINFRM_H__4A4E9309_6D70_4216_ACF3_50122F106283__ INCLUDED_)
Page | 76 Visual Basic & VC++ Programming

#if !defined(AFX_PGM16_H__1BAB9EB8_AAF1_4C5B_A7BF_0B50B3EFDE30_ _INCLUDED_) #define AFX_PGM16_H__1BAB9EB8_AAF1_4C5B_A7BF_0B50B3EFDE30__INCLU DED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols

class CPGM16App : public CWinApp { public: CPGM16App(); public: virtual BOOL InitInstance(); afx_msg void OnAppAbout(); DECLARE_MESSAGE_MAP() };

Page | 77 Visual Basic & VC++ Programming

#endif // !defined(AFX_PGM16_H__1BAB9EB8_AAF1_4C5B_A7BF_0B50B3EFDE30_ _INCLUDED_) #if !defined(AFX_PGM16DOC_H__2B4A6918_521C_4CE7_BFF3_5ED7F950ECE 8__INCLUDED_) #define AFX_PGM16DOC_H__2B4A6918_521C_4CE7_BFF3_5ED7F950ECE8__INCL UDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CPGM16Doc : public CDocument { protected: // create from serialization only CPGM16Doc(); DECLARE_DYNCREATE(CPGM16Doc) public: public: public: virtual BOOL OnNewDocument(); virtual void Serialize(CArchive& ar); public: virtual ~CPGM16Doc();
Page | 78 Visual Basic & VC++ Programming

#ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: protected: DECLARE_MESSAGE_MAP() }; #endif // !defined(AFX_PGM16DOC_H__2B4A6918_521C_4CE7_BFF3_5ED7F950ECE 8__INCLUDED_) #if !defined(AFX_PGM16VIEW_H__EE084F93_B2FB_47E3_ABC6_03726DD42B 2A__INCLUDED_) #define AFX_PGM16VIEW_H__EE084F93_B2FB_47E3_ABC6_03726DD42B2A__INC LUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CPGM16View : public CView { protected: // create from serialization only CPGM16View();
Page | 79 Visual Basic & VC++ Programming

DECLARE_DYNCREATE(CPGM16View) public: CPGM16Doc* GetDocument(); public: public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: public: virtual ~CPGM16View(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: protected: afx_msg void OnDialogFont(); afx_msg void OnDialogColor(); DECLARE_MESSAGE_MAP() }; #ifndef _DEBUG // debug version in PGM 16View.cpp inline CPGM16Doc* CPGM16View::GetDocument()
Page | 80 Visual Basic & VC++ Programming

{ return (CPGM16Doc*)m_pDocument; } #endif #endif // !defined(AFX_PGM16VIEW_H__EE084F93_B2FB_47E3_ABC6_03726DD42B 2A__INCLUDED_) #if !defined(AFX_STDAFX_H__15708854_9941_441A_92EE_824F9D2044F3__IN CLUDED_) #define AFX_STDAFX_H__15708854_9941_441A_92EE_824F9D2044F3__INCLUDE D_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #define VC_EXTRALEAN #include <afxwin.h> #include <afxext.h> #include <afxdtctl.h> #ifndef _AFX_NO_AFXCMN_SUPPORT #include <afxcmn.h> #endif // _AFX_NO_AFXCMN_SUPPORT #endif // !defined(AFX_STDAFX_H__15708854_9941_441A_92EE_824F9D2044F3__IN CLUDED_)

Page | 81 Visual Basic & VC++ Programming

Output

Page | 82 Visual Basic & VC++ Programming

14. Programs to display a message in the selected color from the color dialog box clicking the left mouse button. Also display another message on the right mouse click.

#include "stdafx.h" #include "MainFrame.h" BEGIN_MESSAGE_MAP(CMainFrame , CFrameWnd) ON_COMMAND(ID_COLOR , Color) ON_WM_LBUTTONDOWN() ON_WM_RBUTTONDOWN() END_MESSAGE_MAP() CMainFrame::CMainFrame() { CString m; m = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW , 0 , (HBRUSH)::GetStockObject(LTGRAY_BRUSH) , 0); Create(0 , "COLOR" , WS_OVERLAPPEDWINDOW , rectDefault , 0 , MAKEINTRESOURCE(IDR_MENU1)); clr = RGB(255 , 255 , 255); } CMainFrame::~CMainFrame()
Page | 83 Visual Basic & VC++ Programming

{ } void CMainFrame:: Color() { CColorDialog dlgColor; CRect rt; CClientDC dc(this); if(IDOK == dlgColor.DoModal()) { clr = dlgColor.GetColor(); } } void CMainFrame::OnLButtonDown(UINT flag , CPoint pt) { CClientDC dc(this); dc.SetTextColor(clr); dc.TextOut(pt.x , pt.y , "HELLO" , 5); CFrameWnd::OnLButtonDown(flag , pt); } void CMainFrame::OnRButtonDown(UINT flag , CPoint pt) { CClientDC dc(this);
Page | 84 Visual Basic & VC++ Programming

dc.SetTextColor(clr); dc.TextOut(pt.x , pt.y , "FRIENDS" , 7); CFrameWnd::OnRButtonDown(flag , pt); } #include "stdafx.h" #include "MyApp.h" #include "MainFrame.h" CMyApp theApp; CMyApp::CMyApp() { } CMyApp::~CMyApp() { } int CMyApp::InitInstance() { CMainFrame* pMyFrame = new CMainFrame; pMyFrame->ShowWindow(3); m_pMainWnd = pMyFrame; return 1; }

Page | 85 Visual Basic & VC++ Programming

#if !defined(AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577 AF__INCLUDED_) #define AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577AF__INC LUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CMainFrame : public CFrameWnd { COLORREF clr; public: CMainFrame(); virtual ~CMainFrame(); void Color(); void OnLButtonDown(UINT flag , CPoint pt); void OnRButtonDown(UINT flag , CPoint pt); DECLARE_MESSAGE_MAP() }; #endif // !defined(AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577 AF__INCLUDED_)

Page | 86 Visual Basic & VC++ Programming

#if !defined(AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__IN CLUDED_) #define AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__INCLUDE D_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CMyApp : public CWinApp { public: CMyApp(); virtual ~CMyApp(); int InitInstance(); }; #endif // !defined(AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__IN CLUDED_)

Page | 87 Visual Basic & VC++ Programming

Output

Page | 88 Visual Basic & VC++ Programming

15. Program to find the cursor location

#include "stdafx.h" #include "MainFrame.h" BEGIN_MESSAGE_MAP(CMainFrame , CFrameWnd) ON_WM_LBUTTONDOWN() ON_WM_RBUTTONDOWN() ON_WM_NCLBUTTONDOWN() ON_WM_CREATE() ON_WM_MOUSEMOVE() ON_WM_SIZE() ON_WM_DESTROY() END_MESSAGE_MAP() CMainFrame::CMainFrame() { CString m; Create(0 , "Cursor Location " , WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL , rectDefault , 0 ); } CMainFrame::~CMainFrame() {
Page | 89 Visual Basic & VC++ Programming

} void CMainFrame::OnLButtonDown(UINT flag , CPoint pt) { MessageBox("This is the client area" , "cursor"); CFrameWnd::OnLButtonDown(flag , pt); } void CMainFrame::OnNcLButtonDown(UINT flag , CPoint pt) { switch(flag) { case HTCAPTION: MessageBox("This is the caption bar" , "cursor"); break; case HTCLOSE: MessageBox("This is the close button" , "cursor"); DestroyWindow(); break; case HTGROWBOX: MessageBox("This is the restore button" , "cursor"); break; case HTVSCROLL: MessageBox("This is the vertical scroll bar" , "cursor");
Page | 90 Visual Basic & VC++ Programming

break; case HTHSCROLL: MessageBox("This is the horizontal scroll bar" , "cursor"); break; case HTMENU: MessageBox("This is the menu" , "cursor"); break; case HTREDUCE: MessageBox("This is the minimize button" , "cursor"); break; case HTSYSMENU: MessageBox("This is the system menu" , "cursor"); break; case HTZOOM: MessageBox("This is the maximize button" , "cursor"); break; } } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { CFrameWnd::OnCreate(lpCreateStruct); s.Create(this);
Page | 91 Visual Basic & VC++ Programming

indicators[0] = 0; indicators[1] = ID_INDICATOR_CAPS; indicators[2] = ID_INDICATOR_NUM; indicators[3] = ID_INDICATOR_SCRL; s.SetIndicators(indicators , 4); return 0;

return 0; } void CMainFrame::OnMouseMove(UINT flag , CPoint pt) { char str[100]; GetClientRect(&rt); SetCapture(); if(pt.x < rt.right && pt.x > rt.left && pt.y < rt.bottom && pt.y > rt.top) { sprintf(str , "x = %d , y = %d" , pt.x , pt.y); s.SetPaneText(0 , str); } else s.SetPaneText(0 , ""); ReleaseCapture();
Page | 92 Visual Basic & VC++ Programming

} #include "stdafx.h" #include "MyApp.h" #include "MainFrame.h" CMyApp theApp; CMyApp::CMyApp() { } CMyApp::~CMyApp() { } int CMyApp::InitInstance() { CMainFrame* pMyFrame = new CMainFrame; pMyFrame->ShowWindow(3); m_pMainWnd = pMyFrame; return 1; } #if !defined(AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577 AF__INCLUDED_)

Page | 93 Visual Basic & VC++ Programming

#define AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577AF__INC LUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CMainFrame : public CFrameWnd { CStatusBar s; unsigned int indicators[4]; CRect rt; public: CMainFrame(); virtual ~CMainFrame(); void OnLButtonDown(UINT flag , CPoint pt); void OnNcLButtonDown(UINT flag , CPoint pt); int OnCreate(LPCREATESTRUCT lpCreateStruct); void OnMouseMove(UINT flag , CPoint pt); DECLARE_MESSAGE_MAP() }; #endif // !defined(AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577 AF__INCLUDED_)
Page | 94 Visual Basic & VC++ Programming

#if !defined(AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__IN CLUDED_) #define AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__INCLUDE D_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CMyApp : public CWinApp { public: CMyApp(); virtual ~CMyApp(); int InitInstance();

}; #endif // !defined(AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__IN CLUDED_)

Page | 95 Visual Basic & VC++ Programming

Output

Page | 96 Visual Basic & VC++ Programming

16. Student database.

#include "stdafx.h" #include "PGM 21.h" #include "PGM 21Dlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif BEGIN_MESSAGE_MAP(CPGM21App, CWinApp) ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() CPGM21App::CPGM21App() { } CPGM21App theApp; BOOL CPGM21App::InitInstance() { AfxEnableControlContainer(); #ifdef _AFXDLL
Page | 97 Visual Basic & VC++ Programming

Enable3dControls(); #else Enable3dControlsStatic(); #endif CPGM21Dlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { } else if (nResponse == IDCANCEL) { } return FALSE; } #include "stdafx.h" #include "PGM 21.h" #include "PGM 21Dlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__;
Page | 98 Visual Basic & VC++ Programming

#endif CPGM21Dlg::CPGM21Dlg(CWnd* pParent /*=NULL*/) : CDialog(CPGM21Dlg::IDD, pParent) { m_RegNo = 0; m_Name = _T(""); m_Branch = _T(""); m_Mark1 = 0; m_Mark2 = 0; m_Mark3 = 0; m_Total = 0; m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } CPGM21Dlg::~CPGM21Dlg() { m_db.Close(); } void CPGM21Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT1, m_RegNo); DDX_Text(pDX, IDC_EDIT2, m_Name);
Page | 99 Visual Basic & VC++ Programming

DDX_Text(pDX, IDC_EDIT3, m_Branch); DDX_Text(pDX, IDC_EDIT4, m_Mark1); DDX_Text(pDX, IDC_EDIT5, m_Mark2); DDX_Text(pDX, IDC_EDIT6, m_Mark3); DDX_Text(pDX, IDC_EDIT7, m_Total); } BEGIN_MESSAGE_MAP(CPGM21Dlg, CDialog) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnFirstRecord) ON_BN_CLICKED(IDC_BUTTON2, OnPrevRecord) ON_BN_CLICKED(IDC_BUTTON3, OnNextRecord) ON_BN_CLICKED(IDC_BUTTON4, OnLastRecord) ON_BN_CLICKED(IDC_BUTTON5, OnAddRecord) ON_BN_CLICKED(IDC_BUTTON6, OnUpdateRecord) ON_EN_KILLFOCUS(IDC_EDIT4, OnKillfocusEdit4) ON_EN_KILLFOCUS(IDC_EDIT5, OnKillfocusEdit5) ON_EN_KILLFOCUS(IDC_EDIT6, OnKillfocusEdit6) ON_BN_CLICKED(IDC_BUTTON8, OnDeleteRecord) END_MESSAGE_MAP() BOOL CPGM21Dlg::OnInitDialog() {
Page | 100 Visual Basic & VC++ Programming

CDialog::OnInitDialog(); SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE); try { m_db.Open(_T("Student.mdb"),FALSE,FALSE,NULL); m_rs.m_pDatabase = &m_db; m_rs.Open(AFX_DAO_USE_DEFAULT_TYPE,_T("Select * from Stud"),NULL); UpdateRecord(); } catch( CDaoException * pEX ) { AfxMessageBox( pEX->m_pErrorInfo->m_strDescription ); pEX->Delete(); return FALSE; } return TRUE; // return TRUE unless you set the focus to a control } void CPGM21Dlg::OnPaint() { if (IsIconic())
Page | 101 Visual Basic & VC++ Programming

{ CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } HCURSOR CPGM21Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CPGM21Dlg::UpdateRecord()
Page | 102 Visual Basic & VC++ Programming

{ COleVariant var; m_rs.GetFieldValue(0,var); m_RegNo = var.dblVal; m_rs.GetFieldValue(1,var); m_Name = (LPCTSTR)var.pbstrVal; m_rs.GetFieldValue(2,var); m_Branch = (LPCTSTR)var.pbstrVal; m_rs.GetFieldValue(3,var); m_Mark1 = var.dblVal; m_rs.GetFieldValue(4,var); m_Mark2 = var.dblVal; m_rs.GetFieldValue(5,var); m_Mark3 = var.dblVal; m_rs.GetFieldValue(6,var); m_Total = var.dblVal; UpdateData(FALSE); } void CPGM21Dlg::OnFirstRecord() { m_rs.MoveFirst(); UpdateRecord();
Page | 103 Visual Basic & VC++ Programming

} void CPGM21Dlg::OnPrevRecord() { if(m_rs.IsBOF()) { m_rs.MoveFirst(); return; } m_rs.MovePrev(); if(m_rs.IsBOF()) { m_rs.MoveFirst(); return; } UpdateRecord(); } void CPGM21Dlg::OnNextRecord() { if(m_rs.IsEOF()) { m_rs.MoveLast(); return;
Page | 104 Visual Basic & VC++ Programming

} m_rs.MoveNext(); if(m_rs.IsEOF()) { m_rs.MoveLast(); return; } UpdateRecord(); void CPGM21Dlg::OnLastRecord() { m_rs.MoveLast(); UpdateRecord(); } void CPGM21Dlg::OnAddRecord() { UpdateData(TRUE); CString str; m_rs.AddNew(); str.Format("%d" , m_RegNo); m_rs.SetFieldValue("RegNo", (LPCTSTR)str); m_rs.SetFieldValue("Stud_Name",(LPCTSTR)m_Name); m_rs.SetFieldValue("Branch",(LPCTSTR)m_Branch);
Page | 105 Visual Basic & VC++ Programming

str.Format("%d" , m_Mark1); m_rs.SetFieldValue("Mark1", (LPCTSTR)str); str.Format("%d" , m_Mark2); m_rs.SetFieldValue("Mark2", (LPCTSTR)str); str.Format("%d" , m_Mark3); m_rs.SetFieldValue("Mark3", (LPCTSTR)str); str.Format("%d" , m_Mark1 + m_Mark2 + m_Mark3); m_rs.SetFieldValue("Total", (LPCTSTR)str); m_rs.Update(); m_rs.MoveLast(); } void CPGM21Dlg::OnUpdateRecord() { UpdateData(TRUE); CString Sql; Sql.Format("RegNo = %d" , m_RegNo); if(m_rs.Find(AFX_DAO_FIRST,Sql)) { m_rs.Edit(); CString str; m_rs.SetFieldValue("Stud_Name",(LPCTSTR)m_Name); m_rs.SetFieldValue("Branch",(LPCTSTR)m_Branch);
Page | 106 Visual Basic & VC++ Programming

str.Format("%d" , m_Mark1); m_rs.SetFieldValue("Mark1", (LPCTSTR)str); str.Format("%d" , m_Mark2); m_rs.SetFieldValue("Mark2", (LPCTSTR)str); str.Format("%d" , m_Mark3); m_rs.SetFieldValue("Mark3", (LPCTSTR)str); str.Format("%d" , m_Mark1 + m_Mark2 + m_Mark3); m_rs.SetFieldValue("Total", (LPCTSTR)str); m_rs.Update(); } } void CPGM21Dlg::OnKillfocusEdit4() { UpdateData(TRUE); m_Total = m_Mark1 + m_Mark2 + m_Mark3; UpdateData(FALSE); } void CPGM21Dlg::OnKillfocusEdit5() { UpdateData(TRUE); m_Total = m_Mark1 + m_Mark2 + m_Mark3; UpdateData(FALSE);
Page | 107 Visual Basic & VC++ Programming

} void CPGM21Dlg::OnKillfocusEdit6() { UpdateData(TRUE); m_Total = m_Mark1 + m_Mark2 + m_Mark3; UpdateData(FALSE); } void CPGM21Dlg::OnDeleteRecord() { UpdateData(TRUE); CString Sql; Sql.Format("RegNo = %d" , m_RegNo);

if(m_rs.Find(AFX_DAO_FIRST,Sql)) { m_rs.Delete(); if(m_rs.IsDeleted()) { OnNextRecord(); } } }


Page | 108 Visual Basic & VC++ Programming

#include "stdafx.h" #if !defined(AFX_PGM21_H__26DA6BA2_BC86_4630_9417_0062A211ACD0__I NCLUDED_) #define AFX_PGM21_H__26DA6BA2_BC86_4630_9417_0062A211ACD0__INCLUDE D_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif

#include "resource.h" class CPGM21App : public CWinApp { public: CPGM21App(); public: virtual BOOL InitInstance(); DECLARE_MESSAGE_MAP() };
Page | 109 Visual Basic & VC++ Programming

#endif // !defined(AFX_PGM21_H__26DA6BA2_BC86_4630_9417_0062A211ACD0__I NCLUDED_) #if !defined(AFX_PGM21DLG_H__F091D220_EB33_42B3_A31C_EBB06B0B5AC F__INCLUDED_) #define AFX_PGM21DLG_H__F091D220_EB33_42B3_A31C_EBB06B0B5ACF__INC LUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <afxdao.h> class CPGM21Dlg : public CDialog { CDaoDatabase m_db; CDaoRecordset m_rs; void UpdateRecord(); public: CPGM21Dlg(CWnd* pParent = NULL); ~CPGM21Dlg(); enum { IDD = IDD_PGM21_DIALOG }; int CString m_RegNo; m_Name;
Page | 110 Visual Basic & VC++ Programming

// standard constructor

CString int int int int protected:

m_Branch; m_Mark1; m_Mark2; m_Mark3; m_Total;

virtual void DoDataExchange(CDataExchange* pDX); protected: HICON m_hIcon; virtual BOOL OnInitDialog(); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnFirstRecord(); afx_msg void OnPrevRecord(); afx_msg void OnNextRecord(); afx_msg void OnLastRecord(); afx_msg void OnAddRecord(); afx_msg void OnUpdateRecord(); afx_msg void OnKillfocusEdit4(); afx_msg void OnKillfocusEdit5(); afx_msg void OnKillfocusEdit6(); afx_msg void OnDeleteRecord();
Page | 111 Visual Basic & VC++ Programming

DECLARE_MESSAGE_MAP() }; #endif // !defined(AFX_PGM21DLG_H__F091D220_EB33_42B3_A31C_EBB06B0B5AC F__INCLUDED_) #if !defined(AFX_STDAFX_H__F7850AF8_8047_416F_B2A5_93150D67DD71__I NCLUDED_) #define AFX_STDAFX_H__F7850AF8_8047_416F_B2A5_93150D67DD71__INCLUD ED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #define VC_EXTRALEAN #include <afxwin.h> #include <afxext.h> #include <afxdisp.h> #include <afxdtctl.h> #ifndef _AFX_NO_AFXCMN_SUPPORT #include <afxcmn.h> #endif // _AFX_NO_AFXCMN_SUPPORT #endif // !defined(AFX_STDAFX_H__F7850AF8_8047_416F_B2A5_93150D67DD71__I NCLUDED_)
Page | 112 Visual Basic & VC++ Programming

Output

Page | 113 Visual Basic & VC++ Programming

17. Rubber band line and rectangle.

#include "stdafx.h" #include "MainFrame.h" BEGIN_MESSAGE_MAP(CMainFrame , CFrameWnd) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() ON_COMMAND(ID_LINE , Line) ON_COMMAND(ID_RECTANGLE , Rectangle) ON_COMMAND(ID_CLEAR , Clear) END_MESSAGE_MAP() CMainFrame::CMainFrame() { Create(0 , "Rubberband" , WS_OVERLAPPEDWINDOW , rectDefault , 0 , MAKEINTRESOURCE(IDR_MENU1)); } CMainFrame::~CMainFrame() { } void CMainFrame::OnLButtonDown(UINT flag , CPoint pt)
Page | 114 Visual Basic & VC++ Programming

{ startpoint = endpoint = pt; } void CMainFrame::OnMouseMove(UINT flag , CPoint pt) { CClientDC d(this); if(1 == f) { if(MK_LBUTTON == flag) { d.SetROP2(R2_NOTXORPEN); d.MoveTo(startpoint); d.LineTo(endpoint); d.MoveTo(startpoint); d.LineTo(pt); endpoint = pt; } } if(2 == f) { if(MK_LBUTTON == flag) {
Page | 115 Visual Basic & VC++ Programming

d.SetROP2(R2_NOTXORPEN); d.MoveTo(startpoint.x , endpoint.y); d.Rectangle(startpoint.x , endpoint.y , pt.x , pt.y); d.MoveTo(pt.x , pt.y); endpoint = pt; } } } void CMainFrame::OnLButtonUp(UINT flag , CPoint pt) { CClientDC d(this); d.SetROP2(R2_NOTXORPEN); d.MoveTo(startpoint); if(1 ==f) d.LineTo(endpoint); } void CMainFrame::Line() { f = 1; } void CMainFrame::Rectangle() {
Page | 116 Visual Basic & VC++ Programming

f = 2; } void CMainFrame::Clear() { RedrawWindow(); } #include "stdafx.h" #include "MyApp.h" #include "MainFrame.h" CMyApp theApp; CMyApp::CMyApp() { } CMyApp::~CMyApp() { } int CMyApp::InitInstance() { CMainFrame* pMyFrame = new CMainFrame; pMyFrame->ShowWindow(3); m_pMainWnd = pMyFrame; return 1;
Page | 117 Visual Basic & VC++ Programming

} #if !defined(AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577 AF__INCLUDED_) #define AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577AF__INC LUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CMainFrame : public CFrameWnd { CPoint startpoint , endpoint; int f; public: CMainFrame(); virtual ~CMainFrame(); void Line(); void Rectangle(); void Clear(); void OnLButtonDown(UINT flag , CPoint pt); void OnLButtonUp(UINT flag , CPoint pt); void OnMouseMove(UINT flag , CPoint pt);
Page | 118 Visual Basic & VC++ Programming

DECLARE_MESSAGE_MAP() }; #endif // !defined(AFX_MAINFRAME_H__DE6F88E5_B491_4978_ABA4_4130003577 AF__INCLUDED_) #if !defined(AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__IN CLUDED_) #define AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__INCLUDE D_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CMyApp : public CWinApp { public: CMyApp(); virtual ~CMyApp(); int InitInstance(); }; #endif // !defined(AFX_MYAPP_H__2BF148C1_573E_4F5D_8128_A649069F6E06__IN CLUDED_)

Page | 119 Visual Basic & VC++ Programming

Output

Page | 120 Visual Basic & VC++ Programming

You might also like