Visual Basic Programming Examples
Visual Basic Programming Examples
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) [Link] = [Link] & Command1 (Index).Caption Current = Val ([Link]) End Sub Private Sub Command12_Click () [Link] = "" Previous = current Current = 0 Choice = "-" End Sub Private Sub Command14_Click () [Link] = "" End Sub Private Sub Command15_Click () Select Case choice Case "+" Result = previous + current [Link] = result Case "-"
Page | 2 Visual Basic & VC++ Programming
Result = previous - current [Link] = result Case "*" Result = previous * current [Link] = result Case "/" Result = previous / current [Link] = result End Select Current = result End Sub Private Sub Command16_Click () [Link] = "" Previous = current Current = 0 Choice = "+" End Sub Private Sub Command18_Click () Current = -current [Link] = current End Sub
Private Sub Command4_Click () [Link] = "" Previous = current Current = 0 Choice = "/" End Sub Private Sub Command8_Click () [Link] = "" Previous = current Current = 0 Choice = "*" End Sub
Output
Dim i, c, a, b As Long Private Sub Cmdclear_Click() [Link] = Clear [Link] [Link] End Sub Private Sub Cmdexit_Click() End End Sub Private Sub List1_Click() [Link] i = Val([Link]) If [Link] = "Fibonacci" Then a=0 b=1 c=1 [Link] (a) Do While c < i [Link] (c)
Page | 6 Visual Basic & VC++ Programming
c=a+b a=b b=c Loop End If If [Link] = "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 [Link] (a) End If Next End If If [Link] = "Factorial" Then b=1 For a = 1 To i - 1
Page | 7 Visual Basic & VC++ Programming
Output
3. String Operations
Dim str As String Private Sub CMDCOMPARE_Click() str = [Link] [Link] = StrReverse(str) End Sub Private Sub CMDLCASE_Click() str = [Link] [Link] = LCase(str) End Sub Private Sub CMDLEFT_Click() str = [Link] [Link] = Left(str, 8) End Sub Private Sub CMDLEN_Click() str = [Link] [Link] = Len(str) End Sub Private Sub CMDLTRIM_Click() str = [Link]
Page | 10 Visual Basic & VC++ Programming
[Link] = LTrim(str) End Sub Private Sub CMDMID_Click() str = [Link] [Link] = Mid(str, 3, 4) End Sub Private Sub CMDRIGHT_Click() str = [Link] [Link] = Right(str, 7) End Sub Private Sub CMDRTRIM_Click() str = [Link] [Link] = RTrim(str) End Sub Private Sub CMDTRIM_Click() str = [Link] [Link] = Trim(str) End Sub Private Sub CMDUCASE_Click() str = [Link] [Link] = UCase(str) End Sub
Page | 11 Visual Basic & VC++ Programming
Output
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() [Link] = Clear [Link] = Clear [Link] = Clear [Link] [Link] = False [Link] = True End Sub Private Sub Command2_Click() i=i+1
Page | 13 Visual Basic & VC++ Programming
j=i [Link] = [Link] [Link] = [Link] [Link] = [Link] Put #1, , play [Link] = False [Link] = 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 [Link] = [Link] [Link] = [Link] [Link] = [Link] 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 [Link] = [Link] [Link] = [Link] [Link] = [Link] End Sub Private Sub Command5_Click() Close #1 End End Sub Private Sub Form_Load() i=0 Open "[Link]" For Random As #1 Len = 40 End Sub
Output
Dim f, op As Boolean Private Sub Center_Click() [Link] = 2 End Sub Private Sub Copy_Click() [Link] ([Link]) End Sub
Private Sub Cut_Click() [Link] [Link] = ([Link]) [Link] = 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 [Link] ([Link]) 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 [Link] [Link] ([Link]) End End If Else End End If End Sub
Private Sub Font_Click() [Link] = True On Error GoTo er [Link] = cd1vfboth Or cdcfeffects [Link] With Text1 .FontName = [Link] .FontSize = [Link]
Page | 18 Visual Basic & VC++ Programming
.FontBold = [Link] .FontItalic = [Link] .FontStrikethru = [Link] .ForeColor = [Link] End With er: End Sub
Private Sub open_Click() op = True If f = True Then If MsgBox("do u want to save?", vbQuestion + vbYesNo) = vbYes Then [Link] [Link] ([Link]) f = False [Link] [Link] ([Link]) End If
Page | 19 Visual Basic & VC++ Programming
Else [Link] [Link] ([Link]) Exit Sub er: End If End Sub
Output
Private Sub Timer1_Timer() [Link] = "Traffic Signal" + "Stop..." [Link] = vbRed [Link] = vbRed [Link] = vbBlack [Link] = vbYellow [Link] = vbBlack [Link] = vbGreen [Link] = True [Link] = False End Sub Private Sub Timer2_Timer() [Link] = "Traffic Signal" + "Ready to Go..." [Link] = vbBlack [Link] = vbRed [Link] = vbYellow [Link] = vbYellow [Link] = vbBlack [Link] = vbGreen
Page | 22 Visual Basic & VC++ Programming
[Link] = True [Link] = False End Sub Private Sub Timer3_Timer() [Link] = "Traffic Signal" + " Go..." [Link] = vbBlack [Link] = vbRed [Link] = vbBlack [Link] = vbYellow [Link] = vbGreen [Link] = vbGreen [Link] = True [Link] = False End Sub
Output
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() [Link] 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() [Link] Circle (2200, 1400), 1000, vbBlue End Sub Private Sub conscir_Click() [Link] Dim r As Integer start = 100 ending = [Link] - 3000 For r = start To ending
Page | 25 Visual Basic & VC++ Programming
Circle ([Link] / 2, [Link] / 2), r, RGB(Rnd * 255, Rnd * 255, Rnd * 255) Next r End Sub Private Sub exit_Click() End End Sub Private Sub fillbox_Click() [Link] 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
Output
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\[Link]) End Sub Private sub delete_click () Flag=5 Load frmbankadd [Link] [Link]=false [Link]=false End Sub Private sub deposit_click() Flag=2 Load frmbnkamt
Page | 28 Visual Basic & VC++ Programming
[Link]=true [Link]=DEPOSIT End Sub Private sub exit _click () End End sub Private sub new_click Flag=1 Load frmbnkadd [Link]=true [Link]=false [Link]=false End Sub Private sub update_click() Flag=4 Load frmbnkadd [Link]=true [Link]=false [Link]=false End Sub Private sub view_click() Flag=7
Page | 29 Visual Basic & VC++ Programming
Load frmbnkview [Link]=true End Sub Private sub viewall_click() Flag=6 Load frmbnkview [Link]=true End Sub Private sub withdraw_click() Flag=3 Load frmbnkamt Frmbnkamt. visible=true [Link]=WITHDRAW End Sub FRMBNKADD Dim AN%,t$, a as Variant Private sub cmdadd_click() If [Link]=true then t=SAVINGS elseif [Link]=true then t=CURRENT End If
Page | 30 Visual Basic & VC++ Programming
Set rec=[Link](select *from bank where acctype=&t&order by accno) If [Link]=0 then If t=SAVINGthen An=1 Else If=Currentthen an=101 End If Else [Link] An=[Link](5)+1 End If If [Link]=Or [Link]=Or [Link]=Or [Link]= then MsgBoxPlease enter values Else [Link](insert into bank values(&[Link]&,&[Link]&,&t&,val([Link]) &,&[Link]&,&an&);) MsgBox Your account no:is&an Call clearfields End If End sub
Page | 31 Visual Basic & VC++ Programming
Private sub cmdcancel_click() [Link]=clear [Link]=clear [Link]=clear [Link]=clear [Link]=False [Link]=False End sub Private sub cmdexit_click() Unload Me End sub Public Sub clearfields() [Link]=Clear [Link]=Clear [Link]=clear [Link]=clear [Link]=False [Link]=False End sub Private sub cmdupdate_click() If [Link]=true then t=savings
Page | 32 Visual Basic & VC++ Programming
Else If [Link]=true then T=current End If [Link](update bank set accno=&val([Link])&,name=&[Link]&,acctype=&t&,occupation= ,&[Link]&, address=&[Link]&,initamt=&val([Link])&where accno=&a&;) MsgBoxrecord updated Clearfields Unload Me End sub Private Sub form_load() If flag=1 then [Link]=False Lblno. visible=False ElseIf flag=4 or flag=5 then Lbl:a=InputBox(Enter accno:) If a<>then set rec=[Link](select*from bank where accno=&a&;) If [Link]=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=[Link](0) txttocc=[Link](1) txtamt=[Link](3) txtadd=[Link](4) txtno=[Link](5) If [Link](2) = "savings" then [Link] = true Else [Link] = true End If End Sub private sub cmdexit_click() Unload Me End Sub
Page | 34 Visual Basic & VC++ Programming
private sub cmdok_click() If [Link]<>""And [Link]<>""then set rec=[Link]("select * from bank where accno=&[Link]&";") If [Link] = 0 then MsgBox"No such account" Else If flag=2 then [Link]("update bank setr initamt + "&[Link]&";") MsgBox"DEPOSIT SUCCESSFULLY" ElseIf flag = 3 then If [Link](3)-val([Link])<1000 then MsgBox"cant withdraw-not enough balance" Else [Link]("update bank setr initamt =initamt- "&[Link]&" where accno="&[Link]&";") MsgBox"SUCCESSFULLY WITHDRAWN" End If End If End If [Link]="" [Link]=""
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" [Link](s) [Link] s="" Wend ElseIf flag=7 then n=InputBox("enter no") set rec = [Link]("select *fram bank where accno="&n&";") If [Link] 0 then MsgBox"No such record" Else s$=[Link](5) &vb Tab&[Link](0) &vb Tab&[Link](1) &vb Tab&[Link](2) &vb Tab&[Link](3) &vb Tab&[Link](4)
Page | 36 Visual Basic & VC++ Programming
[Link](s) End If End If End Sub private sub MDIfrom_Load() connect End Sub
Output
9. Demonstration for ADODB. Dim con As [Link] Dim res As [Link] Private Sub Command1_Click() [Link] "SHOP", con, adOpenDynamic, adLockOptimistic [Link] [Link](0).Value = [Link] [Link](1).Value = [Link] [Link](2).Value = [Link] [Link] [Link] 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 & " " [Link] SqL, con, adOpenKeyset, adLockOptimistic [Link] = [Link](0) [Link] = [Link](1) [Link] = [Link](2) [Link]
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 & "" [Link] SqL, con, adOpenKeyset, adLockOptimistic [Link] = [Link](0) [Link] = [Link](1) [Link] = [Link](2) [Link] [Link] End Sub Private Sub Command4_Click() [Link] "SHOP", con, adOpenKeyset, adLockOptimistic [Link](0) = Val([Link]) [Link](1) = [Link] [Link](2) = [Link] [Link] [Link] 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 & "" [Link] "SHOP", con, adOpenKeyset, adLockOptimistic If [Link] Then MsgBox ("sorry no record") Else [Link] = [Link](0) [Link] = [Link](1) [Link] = [Link](2) End If [Link] End Sub Private Sub Command6_Click() End End Sub Private Sub Form_Initialize() Set con = New [Link] Set res = New [Link] [Link] = "[Link].4.0" [Link] "C:\accessdb\[Link]" End Sub
Output
VC++
#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; } }
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { CMenu myMenu; [Link](this , WS_CHILD|WS_VISIBLE|CBRS_TOP); [Link](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_)
Output
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); [Link](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); [Link](PS_SOLID,w,c); p->SelectObject(&pn); p->MoveTo(32,100); p->LineTo(34,16); } Void rectangle() { CPen pn; CClientDc *p=new CClientDC(this); [Link](PS_SOLID,w,c); p->SelectObject(&pn); p->Rectangle(125,46,54,78); } Void circle() { CPen pn; CClientDc *p=new CClientDC(this); [Link](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;
Output
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) { [Link]("Course" , WS_CHILD|WS_VISIBLE|SS_RIGHT|SS_CENTERIMAGE, CRect(100 , 20 , 180 , 45) , this , 1 ) [Link](WS_EX_CLIENTEDGE,"EDIT" , "" , WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL , CRect(200 , 20 , 400 , 50) , this , 2 ); [Link](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; [Link](s); [Link](t1); [Link](t2); p = s + "\t" + t1 + "\t" + t2; MessageBox(p , "Control" , 1); }
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;
Output
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)
} 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
void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() void CPGM16App::OnAppAbout() { CAboutDlg aboutDlg; [Link](); } #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;
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
if(IDOK == [Link]()) { fc = [Link](); s = [Link]() / 10; rgb = [Link](); w = [Link](); sk = [Link](); u = [Link](); b = [Link](); i = [Link](); [Link](s , 15 , 0 , 0 , w , i , u , sk , 0 , 0 , 0 , 0 , 0 , fc); [Link](&Font); [Link](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 == [Link]()) { clr = [Link](); CBrush myBrush(clr); GetClientRect(&rt); [Link](&rt , &myBrush); }
#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() };
#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 [Link] 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_)
Output
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 == [Link]()) { clr = [Link](); } } void CMainFrame::OnLButtonDown(UINT flag , CPoint pt) { CClientDC dc(this); [Link](clr); [Link](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
[Link](clr); [Link](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; }
#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_)
#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_)
Output
#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); [Link](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; [Link](indicators , 4); return 0;
return 0; } void CMainFrame::OnMouseMove(UINT flag , CPoint pt) { char str[100]; GetClientRect(&rt); SetCapture(); if(pt.x < [Link] && pt.x > [Link] && pt.y < [Link] && pt.y > [Link]) { sprintf(str , "x = %d , y = %d" , pt.x , pt.y); [Link](0 , str); } else [Link](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_)
#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();
Output
#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 = [Link](); 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("[Link]"),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) [Link](), 0); int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = ([Link]() - cxIcon + 1) / 2; int y = ([Link]() - cyIcon + 1) / 2; [Link](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 = [Link]; m_rs.GetFieldValue(1,var); m_Name = (LPCTSTR)[Link]; m_rs.GetFieldValue(2,var); m_Branch = (LPCTSTR)[Link]; m_rs.GetFieldValue(3,var); m_Mark1 = [Link]; m_rs.GetFieldValue(4,var); m_Mark2 = [Link]; m_rs.GetFieldValue(5,var); m_Mark3 = [Link]; m_rs.GetFieldValue(6,var); m_Total = [Link]; 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(); [Link]("%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
[Link]("%d" , m_Mark1); m_rs.SetFieldValue("Mark1", (LPCTSTR)str); [Link]("%d" , m_Mark2); m_rs.SetFieldValue("Mark2", (LPCTSTR)str); [Link]("%d" , m_Mark3); m_rs.SetFieldValue("Mark3", (LPCTSTR)str); [Link]("%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; [Link]("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
[Link]("%d" , m_Mark1); m_rs.SetFieldValue("Mark1", (LPCTSTR)str); [Link]("%d" , m_Mark2); m_rs.SetFieldValue("Mark2", (LPCTSTR)str); [Link]("%d" , m_Mark3); m_rs.SetFieldValue("Mark3", (LPCTSTR)str); [Link]("%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; [Link]("RegNo = %d" , m_RegNo);
#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
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
#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); [Link](startpoint); [Link](endpoint); [Link](startpoint); [Link](pt); endpoint = pt; } } if(2 == f) { if(MK_LBUTTON == flag) {
Page | 115 Visual Basic & VC++ Programming
d.SetROP2(R2_NOTXORPEN); [Link](startpoint.x , endpoint.y); [Link](startpoint.x , endpoint.y , pt.x , pt.y); [Link](pt.x , pt.y); endpoint = pt; } } } void CMainFrame::OnLButtonUp(UINT flag , CPoint pt) { CClientDC d(this); d.SetROP2(R2_NOTXORPEN); [Link](startpoint); if(1 ==f) [Link](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_)
Output