Monday, April 19, 2010

Mutithreading

Procedure:
1. Run AppWizard(exe)—select SDI
2. Insert  Resource Dialog  IDD_DIALOG1
3. Modify the ThreadDialog and ThreadView files
4. Build and test the application.



ThreadDialog.h
#define WM_THREADFINISHED WM_USER+5
public:
int m_nTimer;
enum {nMaxCount=10000};
protected:
afx_msg LRESULT OnThreadFinished(WPARAM wParam,LPARAM lParam);
ThreadDialog.cpp
int g_nCount=0;
UINT ComputeThreadProc(LPVOID pParam)
{
volatile int nTemp;
for (g_nCount=0;g_nCount< CThreadDialog::nMaxCount;::InterlockedIncrement((long*)&g_nCount))
{
for (nTemp=0;nTemp<100000;nTemp++)
{
}
::PostMessage((HWND)pParam,WM_TIMER,0,0);
}
::PostMessage((HWND)pParam,WM_THREADFINISHED,0,0);
g_nCount=0;
return 0;
}
BEGIN_MESSAGE_MAP(CThreadDialog, CDialog)
ON_MESSAGE(WM_THREADFINISHED,OnThreadFinished)
END_MESSAGE_MAP()

void CThreadDialog::OnStart()
{ m_nTimer=SetTimer(1,10000,NULL);
ASSERT(m_nTimer != 0);
GetDlgItem(ID_START)->EnableWindow(FALSE);
AfxBeginThread(ComputeThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
}
void CThreadDialog::OnCancel()
{
if (g_nCount ==0)
CDialog::OnCancel();
else
g_nCount=nMaxCount;
}
LRESULT CThreadDialog::OnThreadFinished(WPARAM wParam,LPARAM lParam)
{
CDialog::OnOK();
return 0;
}
void CThreadDialog::OnTimer(UINT nIDEvent)
{
CProgressCtrl* pBar=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
pBar->SetPos(g_nCount*100/nMaxCount);
}
ThreadView.cpp
#include “ThreadDialog.h”
void CThreadView::OnLButtonDown(UINT nflags, CPOINT point)
{
CThreadDialog dlg;
dlg.DoModal();
}

ODBC

Procedure:

1. Run MFCAppWizard(exe)
Step 1: Choose single document
Step 2: Select Database View without file support option  also click Data
source button and choose ODBC data source name(student registration)
and select Dynaset as the record type, then select the table(student)
2. Add controls to the form template.
3. Using class wizardMember variable tabAdd variable buttonchoose the ID corresponding to the edit box for each variable.
4. Add the following items to the IDR_MAINFRAME menu
- Add record, Clear fields, Delete record, Update record
5. Use class wizard to map the menu command id’s with the corresponding
command handlers.
ID_RECORD_ADD OnRecordAdd
ID_RECORD_CLEARFIELDS OnRecordClearfields
ID_RECORD_DELETE OnRecordDelete
ID_RECORD_UPADTE OnRecordUpdate
6. Edit the menu command handlers under View.cpp file
7. Build and test the application
SOURCE CODE:
void CEx31bView::OnRecordAdd()
{
m_pSet->AddNew();
UpdateData(TRUE);
if (m_pSet->CanUpdate())
{
m_pSet->Update();
}
if (!m_pSet->IsEOF())
{
m_pSet->MoveLast();
}
//m_pSet->Requery();
UpdateData(FALSE);
}
void CEx31bView::OnRecordClearFields()
{
m_pSet->SetFieldNull(NULL);
UpdateData(FALSE);
}
void CEx31bView::OnRecordDelete()
{
CRecordsetStatus status;
try
{
m_pSet->Delete();
}
catch(CDBException* e)
{
AfxMessageBox(e->m_strError);
e->Delete();
m_pSet->MoveFirst();
UpdateData(FALSE);
return;
}
m_pSet->GetStatus(status);
if (status.m_lCurrentRecord ==0)
{
m_pSet->MoveFirst();
}
else
{
m_pSet->MoveNext();
}
UpdateData(FALSE);
}
void CEx31bView::OnUpdateRecordDelete(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_pSet->IsEOF());
}
void CEx31bView::OnRecordUpdate()
{
m_pSet->Edit();
UpdateData(TRUE);
if (m_pSet->CanUpdate())
{
m_pSet->Update();
}
}
void CEx31bView::OnUpdateRecordUpdate(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_pSet->IsEOF());
}

activex

Procedure:
1. Run AppWizard(exe) . Select SDI and check ActiveXControls
2. ProjectsAdd to Project-Components and controls-Registered Activex controls select calendar control 8.0.
3. Insert  Resource Dialog-IDD_DIALOG1(add calendar control 8.0)
4. Create a class CActiveXDialog for the dialog resource.
5. Modify the ActiveXDialog.cpp file and ProjectView.cpp.
6. Build and tes
7. tActiveXDialog.cpp

//BN_CLICKED
void CActiveXDialogl::OnSelectdate()
{
CDataExchange dx(this,TRUE);

DDX_Text(&dx, IDC_DAY,m_day);
DDX_Text(&dx, IDC_MONTH,m_month);
DDX_Text(&dx, IDC_YEAR,m_year);
m_calendar.SetDay(m_day);
m_calendar.SetMonth(m_month);
m_calendar.SetYear(m_year);
}
//BN_CLICKED
void CActiveXDialog::OnNextweek()
{
m_calendar.NextWeek();
}
//BN_CLICKED
void CActiveXDialog::OnNextday()
{
m_calendar.NextDay();
}
// calendar - Click event
void CActiveXDialog::OnClickCalendar1()
{
COleDateTime dt(m_calendar.GetValue());
SetWindowText(dt.Format(" date chosen %#x"));
}
//calendar - NewMonth
void CActiveXDialog::OnNewMonthCalendar1()
{
AfxMessageBox("EVENT: New Month ");
}
//Dialog - WM_LBUTTONDOWN
void CActiveXDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
CActiveXDialog dlg;
COleDateTime today=COleDateTime::GetCurrentTime();
dlg.m_varvalue=COleDateTime(today.GetYear(),today.GetMonth(),today.GetDay(),0,0,0);
COleDateTime date(dlg.m_varvalue);
MessageBox(date.Format("%B,%d,%y"));
}
PROJVIEW.cpp
#include "ActiveXDialog.h"
//WM_LBUTTONDOWN
void CProjView::OnLButtonDown(UINT nFlags, CPoint point)
{
CActiveXDialog dlg;
dlg.DoModal();
}

DLL

DLL
DLL:
1.Run MFC AppWizard (dll)– check SDI and accept “Regular DD Using Shared MFC DLL” from the AppWizard page.
2.Add the code for the exported function DLLSquareRoot().
3.Build the project to create DLL.
Test:
1.Run MFC AppWizard(exe) – check SDI
3.InsertResourceDialog-IDD_DIALOG1.
4.Use class wizard to add data members and to create a test dialog class.
5.Define OnSqrt() to call the DLL’s exported function and declare.
6.Call DoModal() from view to open the dialog.
7.Project  Settings  Link TabObject/Library – specify the path to dllsquare.lib file.
8.copy the dllsquare.dll file from “dllsquare” project to the “test” project folder.
Build and test.
Source code:
a) Dllsquare Project:
dllsquare.cpp
#include “math.h”
// add the following code
extern "C" _declspec(dllexport) double DLLSquareRoot(double d)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

if (d>=0.0)
return sqrt(d);
AfxMessageBox("Invalid Square Root");
return 0.0;
}
// Build the project and copy the file dllsquare.dll from the “dllsquare” project dir to the “test project” //directory.
b) DLLTest Project:
TestDialog.h
extern "C" __declspec(dllimport) double DLLSquareRoot(double d);


TestDialog.cpp
// Class Wizard
void CTestDialog::OnSqrt()
{
UpdateData(TRUE);
m_result=DLLSquareRoot(m_input);
UpdateData(FALSE);
}
dllTestView.cpp
#include "TestDialog.h"
//Class Wizard
void CDllTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
CTestDialog dlg;
dlg.DoModal();
}



a) Dllsquare Project:
dllsquare.cpp
// add the following code
extern "C" _declspec(dllexport) double DLLSquareRoot(double d)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (d>=0.0)
return d*d;
AfxMessageBox("Invalid Square Root");
return 0.0;
}
// Build the project and copy the file dllsquare.dll from the “dllsquare” project dir to the “test project” //directory.
b) DLLTest Project:
TestDialog.h
extern "C" __declspec(dllimport) double DLLSquareRoot(double d);
TestDialog.cpp
// Class Wizard
void CTestDialog::OnSqrt()
{
UpdateData(TRUE);
m_result=DLLSquareRoot(m_input);
UpdateData(FALSE);
}
dllTestView.cpp
#include "TestDialog.h"
//Class Wizard
void CDllTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
CTestDialog dlg;
dlg.DoModal();
}

MDI

MDI
1.Run AppWizard(.exe),check MDI.Choose CFormView base class instead of CView.
2.Insert->Resource->Dialog->IDD_Dialog1.
3.Project->Add to project->New->Student.h and Student.cpp.
4.Define a student class that inherits the base class Cobject to make the class serializable and override the function serialize().
5.Modify the Document and View class.
6.Build and test the application.
CODING:
Student.h
#ifndef _INSIDE_VISUAL_CPP_STUDENT
#define _INSIDE_VISUAL_CPP_STUDENT
class student:public CObject
{
DECLARE_SERIAL(student)
public:
CString m_strName;
int m_grade;
void Serialize(CArchive& ar);
student()
{
}
student(const char* szName,int nGrade):m_strName(szName)
{
m_grade=nGrade;
}
};
#endif
Student.cpp
#include "stdafx.h"
#include "student.h"
IMPLEMENT_SERIAL(student,CObject,0)
#ifdef DEBUG
void student::Serialize(CArchive& ar)
{
if(ar.IsStoring())
ar<else
ar>>m_strName>>(int&)m_grade;
}
#endif
MDIDoc.h
#include "student.h"
public:
student m_student;
MDIDoc.cpp
CMdiDoc::CMdiDoc():m_student("VPLAB",0)
{
// TODO: add one-time construction code here
}
void CMdiDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
m_student.Serialize(ar);
}
BOOL CMdiDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
return TRUE;
}
BOOL CMdiDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
return CDocument::OnSaveDocument(lpszPathName);
}
MDIView.h
public:
void UpdateControlsFromdoc();
MDIView.cpp
void CMdiView::OnInitialUpdate()
{
UpdateControlsFromdoc();
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
void CMdiView::OnEnter()
{
CMdiDoc* p=GetDocument();
UpdateData(TRUE);
p->m_student.m_grade=m_grade;
p->m_student.m_strName=m_name;
}
void CMdiView::UpdateControlsFromdoc()
{
CMdiDoc* p=GetDocument();
m_grade=p->m_student.m_grade;
m_name=p->m_student.m_strName;
UpdateData(FALSE);}

SDI

SDI
1.Run AppWizard(.exe),check SDI.Choose CFormView base class instead of CView.
2.Insert->Resource->Dialog->IDD_Dialog1.
3.Project->Add to project->New->Student.h and Student.cpp.
4.Define a student class that inherits the base class Cobject to make the class serializable and override the function serialize().
5.Modify the Document and View class.
6.Build and test the application.
CODING:
Student.h
#ifndef _INSIDE_VISUAL_CPP_STUDENT
#define _INSIDE_VISUAL_CPP_STUDENT
class student:public CObject
{
DECLARE_SERIAL(student)
public:
CString m_strName;
int m_grade;
void Serialize(CArchive& ar);
student()
{
}
student(const char* szName,int nGrade):m_strName(szName)
{
m_grade=nGrade;
}
};
#endif
Student.cpp
#include "stdafx.h"
#include "student.h"
IMPLEMENT_SERIAL(student,CObject,0)
#ifdef DEBUG
void student::Serialize(CArchive& ar)
{
if(ar.IsStoring())
ar<else
ar>>m_strName>>(int&)m_grade;
}
#endif
SDIDoc.h
#include "student.h"
public:
student m_student;
SDIDoc.cpp
CSDIDoc::CSDIDoc():m_student("VPLAB",0)
{
// TODO: add one-time construction code here
}
void CSDIDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
m_student.Serialize(ar);
}
BOOL CSDIDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
return TRUE;
}
BOOL CSDIDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
return CDocument::OnSaveDocument(lpszPathName);
}
SDIView.h
public:
void UpdateControlsFromdoc();
SDIView.cpp
void CSDIView::OnInitialUpdate()
{
UpdateControlsFromdoc();
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
void CSDIView::OnEnter()
{
CSDIDoc* p=GetDocument();
UpdateData(TRUE);
p->m_student.m_grade=m_grade;
p->m_student.m_strName=m_name;
}
void CSDIView::UpdateControlsFromdoc()
{
CSDIDoc* p=GetDocument();
m_grade=p->m_student.m_grade;
m_name=p->m_student.m_strName;
UpdateData(FALSE);
}

menutoolbar

Menutoolbar
1) Run application wizard and choose single document
2) Create a Menu bar IDR_MAINFRAME and Toolbar IDR_MAINFRAME in Resource tab
3) Define a Rich Edit Control to transfer and store data
4) In the Doc class
Define the functions
i)OnNewDocument
ii)OnEditClearall
4)In the View class
Define the functions
i)OnDrawSquare
ii)OnDrawCircle
iii)OnDrawPattern
iv)OnUpdateDrawSquare
v)OnUpdateDrawCircle
vi)OnUpdateDrawPattern
vii)OnTransferGetData
viii)OnTransferStoreData
ix)OnCreate
x)OnSize
b)Build and Execute the functions
CODE
Menudoc.h
Public:
CString m_strText;
Menudoc.cpp
BOOL CMenuDoc::OnNewDocument()
{
If(!CDocument::OnNewDocument())
return FALSE;
m_strText=”Hello(from CMenuDoc::OnNewDocument)”
return TRUE;
}
Void CMenuDoc::OnEditClearall(CCmdUI *pCmdUI)
{
pCmdUI->Enable(!m_strText.IsEmpty());
}

Menuview.h
Public:
CRichEditCtrl m_rich;
Private:
CRect m_rect;
BOOL m_bCircle;
BOOL m_bPattern;
Menuview.cpp
CMenuView::CMenuView():m_rect(0,0,100,100)
{
m_bCircle=TRUE;
m_bPattern=FALSE;
}

Void CMenuView::OnDraw(CDC *pDC)
{
CBrush brush(HS_BDIAGONAL,0L);
If(m_bPattern)
{
pDC->SelectObject(&brush);
}
else
{
pDC->SelectStockObject(WHITE_BRUSH);
}
If(m_bCircle)
{
pDC->Ellipse(m_rect);
ValidateRect(m_rect);
}
else
{
pDC->Rectangle(m_rect);
ValidateRect(m_rect);
}
pDC->SelectStockObject(WHITE_BRUSH);
}
Void CMenuView::OnTransferGetData()
{
CMenuDoc *pDoc=GetDocument();
m_rich.SetWindowText(pDC->m_strText);
m_rich.SetModify(FALSE);
}
Void CMenuView::OnTransferStoreData()
{
CMenuDoc *pDoc=GetDocument();
m_rich.GetWindowText(pDC->m_strText);
m_rich.SetModify(FALSE);
}
Int CMenuView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CRect rect(0,0,0,0);
If(CView::OnCreate(lpCreateStruct==-1)
return -1;
m_rich.Create(ES_MULTILINE|ES_WANTRETURN|WS_CHILD|WS_VISIBLE|WS_VSCROLL,rect,this,1);
return0;
}

Void CMenuView::OnSize(UINT nType,int cx,int cy)
{
CView::OnSize(nType,cx,cy);
CRect rect;
GetClientRect(rect);
M_rich.SetWindowPos(&wndTop,0,0,rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW);
}

void CMenuView::OnDrawCircle()
{
m_bCircle=TRUE;
m_rect+=CPoint(25,25);
InvalidateRect(m_rect);
}

Void CMenuView::OnDrawPattern()
{
m_bPattern^=1;
}

void CMenuView::OnDrawSquare()
{
m_bCircle=FALSE;
m_rect+=CPoint(25,25);
InvalidateRect(m_rect);
}

void CMenuView::OnUpdateDrawCircle(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_bCircle);
}

void CMenuView::OnUpdateDrawPattern(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bPattern);
}
void CMenuView::OnUpdateDrawSquare(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bCircle);

}

calc

Calc
1)New->Project->MFC->app.wizard(exe)->Dialogbased->OK
2)Insert->Resource->Dialog
3)Create a new class ex5cdialog
4)Set the member variables and the group function for the first radio button.
5)Define function on compute that is used to calculate values
6)Build & execute project.
CODE:
void ex5cdialog::OnButton1()
{
UpdateData(TRUE);
switch(m_op)
{
case 0:
m_out=m_in1+m_in2;
break;
case 1:
m_out=m_in1-m_in2;
break;
case 2:
m_out=m_in1*m_in2;
break;
case 3:
m_out=m_in1/m_in2;
break;
}
UpdateData(FALSE);
}

modeless

MODELESS
1)Run application wizard and choose single document
2)Create a dialog resource,IDP_DIALOG,Dialog class projdialog
a)Create a pointer to view window in construction
b)Define function create( ) then call base class create( ) function.
c)Define ON-OK,ON-CANCEL functions and post the message GOODBYE.
3)View class:
a)Create a pointer to dialog window with constructor.
b)Onleft mouse button class call create( ) of dialog class.
c)Define on goodbye function that destroy window.
d)Build and execute program.
CODE:
Projdialog.h
#define WM_GOODBYE WM_USER+5
private:
CView* m_pView;
public:
ProjDialog(CView* pView);
BOOL Create();
Projdialog.cpp:
Projdialog::Projdialog(CView* pView)
{
m_pview=pView;
}
BOOL Projdialog::Create()
{
Return CDialog::Create(Projdialog::IDD);
}
void Projdialog::OnOK()
{
if(m_pView!=NULL)
{
UpdateData(TRUE);
m_pView->PostMessage(WM_GOODBYE,IDOK);
}
else
CDialog::OnOK();
}
void Projdialog::OnCancel( )
{
if(m_pView!=NULL)
{
m_pView->PostMessage(WM_GOODBYE,IDCANCEL);
}
else
CDialog::OnCancel();
}

Projview.h
//Add this at the begning
Class Projdialog;
Private:
Projdialog *m_pDlg;
Protected:
//add this line after AFX_MSG and before DECLARE MESSAGE MAP
Afx_msg LRESULT OnGoodbye(WPARAM wparam,LPARAM lparam);
Projview.cpp
//Add after Begin_Message_Map but inside AFX_MSG_MAP
ON_MESSAGE(WM_GOODBYE,OnGoodbye);
#include “Projview.h”
#include “Projdialog.h”
Projview::Projview()
{
m_pDlg=new Projdialog(this);
}
Projview::~Projview()
{
delete m_pDlg;
}
void Projview::OnLButtonDown(UINT nFlags,CPoint point)
{
if(m_pDlg->Get SafeHwnd( )==0)
{
m_pDlg->Create( );
}
CView::OnLButtonDown(nFlags,point);
}
LRESULT Projview::OnGoodbye(WPARAM wparam,LPARAM lparam)
{
m_pDlg->DestroyWindow( );
Return 0;
}

modal

Modal
1)Run application wizard(exe)->SDI->Project name and location.
2)Create dialog resource IDD_DIALOG1 and dialog class projdialog
3)Define OnLButtonDown->click view.cpp
4)Define InitDialog
5)Execute.
CODE:
Modaldialogview.cpp
#include “resource.h”
#include "projdialog.h"
void CModView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Moddialog dlg;
dlg.DoModal();
CView::OnLButtonDown(nFlags, point);
}

projdialog.cpp
#include “resource.h”
#include "projdialog.h"
BOOL projdialog::OnInitDialog()
{
CListBox *plb=(CListBox*)GetDlgItem(IDC_LIST1);
plb->InsertString(-1,"ece");
plb->InsertString(-1,"it");
plb->InsertString(-1,"eee");
plb->InsertString(-1,"cse");

return TRUE;
}

menu

menu
6.Window Procedure:
a)WM_PAINT:
i)Get the device context handle and coordinates of client area.
ii)Draw the text at the center of client area.
b)WM_COMMAND:
i)When the child window control is pressed,corresponding message is
sent to window procedure.
c)WM_LBUTTONDOWN:
Set the flag value to ‘1’
d)WM_MOUSEMOVE:
1.if the flag is set to ‘1’,get the device context handle
2.set the pixel value
e)WM_LBUTTONUP:
set the flag value to ‘0’
f)WM_DESTROY:
i)Post quit message that generates WM_DESTROY
g)Default:
Other messages are handled by default window procedure
HEADER FILES:
#define ID_FILE_NEW 9000
#define ID_FILE_OPEN 9001
#define ID_FILE_SAVE 9002
#define ID_FILE_EXIT 9003
#define ID_EDIT_CUT 8000
#define ID_EDIT_COPY 8001
#define ID_EDIT_PASTE 8002
#define ID_EDIT_CLEAR 8003
#define ID_DRAW_RECT 7000
#define ID_DRAW_ELLIPSE 7001
#define ID_DRAW_POLYGON 7002
SOURCE FILE:
#include"nn.h"
#include"windows.h"
WNDCLASS a;
int id;
static RECT rect;
static POINT apt[6];
static int flag=0,f=0,x1,y1,x2,y2,i=0;
LRESULT CALLBACK wndproc(HWND hwnd,UINT x,WPARAM w,LPARAM l)
{
HDC hdc;
switch(x)
{
case WM_CREATE:
{
HMENU hmenu,hsubmenu;
hmenu=CreateMenu();
hsubmenu=CreatePopupMenu();
AppendMenu(hsubmenu,MF_STRING,ID_FILE_NEW,"&new");
AppendMenu(hsubmenu,MF_STRING,ID_FILE_OPEN,"&open");
AppendMenu(hsubmenu,MF_STRING,ID_FILE_SAVE,"&save");
AppendMenu(hsubmenu,MF_STRING,ID_FILE_EXIT,"&exit");
AppendMenu(hmenu,MF_STRING|MF_POPUP,(UINT) hsubmenu,"&file");
hsubmenu=CreatePopupMenu();
AppendMenu(hsubmenu,MF_STRING,ID_EDIT_CUT,"&cut");
AppendMenu(hsubmenu,MF_STRING,ID_EDIT_COPY,"©");
AppendMenu(hsubmenu,MF_STRING,ID_EDIT_PASTE,"&paste");
AppendMenu(hsubmenu,MF_STRING,ID_EDIT_CLEAR,"&clear");
AppendMenu(hmenu,MF_STRING|MF_POPUP,(UINT) hsubmenu,"&edit");
hsubmenu=CreatePopupMenu();
AppendMenu(hsubmenu,MF_STRING,ID_DRAW_RECT,"&rectangle");
AppendMenu(hsubmenu,MF_STRING,ID_DRAW_ELLIPSE,"&ellipse");
AppendMenu(hsubmenu,MF_STRING,ID_DRAW_POLYGON,"&polygon");
AppendMenu(hmenu,MF_STRING|MF_POPUP,(UINT) hsubmenu,"&draw");
SetMenu(hwnd,hmenu);}break;
case WM_LBUTTONDOWN:
x1=LOWORD(l);
y1=HIWORD(l);
if(flag==3)
{
apt[i].x=x1;
apt[i].y=y1;
hdc=GetDC(hwnd);
SetPixel(hdc,x1,y1,RGB(255,0,255));
ReleaseDC(hwnd,hdc);
if(i==5)
{
hdc=GetDC(hwnd);
Polygon(hdc,apt,6);
i=0;
ReleaseDC(hwnd,hdc);
return 0;
}
else
i++;
}
else
f=1;
return 0;
case WM_MOUSEMOVE:
x2=LOWORD(l);
y2=HIWORD(l);
if(f==1)
{
hdc=GetDC(hwnd);
InvalidateRect(hwnd,&rect,TRUE);
GetUpdateRect(hwnd,&rect,TRUE);
ValidateRect(hwnd,&rect);
switch(flag)
{
case 1:
Rectangle(hdc,x1,y1,x2,y2);
break;
case 2:
Ellipse(hdc,x1,y1,x2,y2);
break;
}
SetRect(&rect,x1,y1,x2,y2);
ReleaseDC(hwnd,hdc);
}
return 0;
case WM_LBUTTONUP:
f=0;
SetRect(&rect,0,0,0,0);
return 0;
case WM_COMMAND:
switch(LOWORD(w))
{
case ID_FILE_NEW:
case ID_FILE_OPEN:
case ID_FILE_SAVE:
MessageBeep(0);
return 0;
case ID_FILE_EXIT:
id=MessageBox(hwnd,"do u want to exit","exiting",MB_OKCANCEL);
if(id==0)
PostQuitMessage(0);
break;
case ID_EDIT_CUT:
case ID_EDIT_COPY:
case ID_EDIT_PASTE:
case ID_EDIT_CLEAR:
case ID_DRAW_RECT:
flag=1;
return 0;
case ID_DRAW_ELLIPSE:
flag=2;
break;
case ID_DRAW_POLYGON:
flag=3;
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CLOSE:
DestroyWindow(hwnd);break;
default:
return DefWindowProc(hwnd,x,w,l);
}return 0;}

Button

Button
6.Window Procedure:
a)WM_PAINT:
i)Get the device context handle and coordinates of client area.
ii)Draw the text at the center of client area.
b)WM_COMMAND:
i)When the child window control is pressed,corresponding message is
sent to window procedure.
c)WM_LBUTTONDOWN:
Set the flag value to ‘1’
d)WM_MOUSEMOVE:
1.if the flag is set to ‘1’,get the device context handle
2.set the pixel value
e)WM_LBUTTONUP:
set the flag value to ‘0’
f)WM_DESTROY:
i)Post quit message that generates WM_DESTROY
g)Default:
Other messages are handled by default window procedure.
long _stdcall wproc(HWND hwnd,UINT x,WPARAM w,LPARAM lp)
{
HDC hdc;
HWND h1,h2,h3;
static RECT rect;
switch(x)
{
case WM_CREATE:

h1=CreateWindow("button","rect",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,20,80,60,60,hwnd,(HMENU)1,hinst,0);
h2=CreateWindow("button","ellipse",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,20,10,60,60,hwnd,(HMENU)2,hinst,0);
h3=CreateWindow("button","poly",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,20,200,60,60,hwnd,(HMENU)3,hinst,0);
return 0;
case WM_COMMAND:
if(LOWORD(w)==1 && HIWORD(w)==0)
f=1;
else if(LOWORD(w)==2 && HIWORD(w)==0)
f=2;
else if(LOWORD(w)==3 && HIWORD(w)==0)
f=3;
break;
case WM_LBUTTONDOWN:

x1=LOWORD(lp);
y1=HIWORD(lp);
hdc=GetDC(hwnd);
InvalidateRect(hwnd,&rect,TRUE);
GetUpdateRect(hwnd,&rect,TRUE);
ValidateRect(hwnd,&rect);
if(f==3)
{

c[i].x=x1;
c[i].y=y1;
i++;
SetPixel(hdc,x1,y1,RGB(255,0,0));
}
if(i==6)
{
Polygon(hdc,c,6);
i=0;
}
flag=1;
ReleaseDC(hwnd,hdc);
break;
case WM_MOUSEMOVE:
if(flag==1)
{
x2=LOWORD(lp);
y2=HIWORD(lp);
hdc=GetDC(hwnd);
InvalidateRect(hwnd,&rect,TRUE);
GetUpdateRect(hwnd,&rect,TRUE);
ValidateRect(hwnd,&rect);
rect.left=x1;
rect.bottom=y2;
rect.top=y1;
rect.right=x2;
if(f==1)
Rectangle(hdc,x1,y1,x2,y2);
else if(f==2)

Ellipse(hdc,x1,y1,x2,y2);
ReleaseDC(hwnd,hdc);
}
break;
case WM_LBUTTONUP:
flag=0;
SetRect(&rect,0,0,0,0);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hwnd,x,w,lp);

}
}

Stroke

Stroke
long _stdcall wproc(HWND hwnd,UINT x,WPARAM w,LPARAM l);
{
HDC hdc;
char buffer[100];
TCHAR szkeyname[30];
switch(x)
{
case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
hdc=GetDC(hwnd);
InvalidateRect(hwnd,NULL,TRUE);
GetUpdateRect(hwnd,NULL,TRUE);
ValidateRect(hwnd,NULL);
GetKeyNameText(l,szkeyname,sizeof(szkeyname)/sizeof(TCHAR));
TextOut(hdc,0,0,"keypressed",12);
TextOut(hdc,120,0,szkeyname,strlen(szkeyname));
sprintf(buffer,"keypressed=%d",w);
TextOut(hdc,0,50,buffer,strlen(buffer));
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,x,w,l);
}
return 0;

Free hand I and II

FREE HAND DRAWING-1&II
6.Window Procedure(I&II)
a) WM_LBUTTONDOWN:
i)Set a flag value
b) WM_MOUSEMOVE:
i)Set the pixel if flag is true
ii)Print the pixel
c) WM_LBUTTONUP:
i)Reset the flag value
d)WM_DESTROY
Post quit Message that generates WM_QUIT
e) Default:
Other messages are handled by the default window procedure.
I
Int flag=0;
long _stdcall WProc (HWND hwnd,UINT x,WPARAM w,LPARAM l)
{
switch(x)
{
case WM_LBUTTONDOWN:
flag=1;break;
case WM_MOUSEMOVE:
if(flag==1)
{
hdc=GetDC(hwnd);
SetPixel(hdc,LOWRD(l),HIWORD(l),RGB(255,255.255));
ReleaseDC(hwnd,hdc);
}
Break;
case WM_LBUTTONUP:
flag=0;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,x,w,l);
}
return 0;}
II
Int flag=0;
Int x1,y1,x2,y2;
long _stdcall WProc (HWND hwnd,UINT x,WPARAM w,LPARAM l)
{
switch(x)
{
case WM_LBUTTONDOWN:
if(flag==0)
{
x1=LOWORD(l);
y1=HIWORD(l);
flag=1;
}
break;
case WM_MOUSEMOVE:
if(flag==1)
{
x2=LOWORD(l);
y2=HIWORD(l);
hdc = GetDC(hwnd);
MOVETOEX(hdc,x1,y1,0);
LineTo(hdc,x2,y2);
ReleaseDC(hwnd,hdc);
x1=x2;
y1=y2;
}
break;
case WM_LBUTTONUP:
flag=0;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,x,w,l);
}return 0;}

KEYBOARD EVENTS-PROCESSING CHARACTER MESSAGES
6.window procedure
a)WM_CHAR: -It is used to handle character keys.
b)InvalidateRect is used for making variable invalid.
c)GetUpdateRect is used for returning co-ordinate value of client area and validate client area.
d)GetKeyNameText is used to get keys name.
e)WM_DESTROY:
Post Quit Message that generates WM_QUIT.
f)default:
Other messages are handled by the default window procedure.
long _stdcall wproc(HWND hwnd,UINT x,WPARAM w,LPARAM l)
{
HDC hdc;char buffer[100];
TCHAR szkeyname[30];
switch(x)
{
case WM_CHAR:
hdc=GetDC(hwnd);
InvalidateRect(hwnd,NULL,TRUE);
GetUpdateRect(hwnd,NULL,TRUE);
ValidateRect(hwnd,NULL);
GetKeyNameText(1,szkeyname,sizeof(szkeyname)/sizeof(TCHAR));
wsprintf(buffer,"keypressed=%c",w);
TextOut(hdc,0,20,buffer,strlen(buffer));
TextOut(hdc,120,0,szkeyname,strlen(szkeyname));
wsprintf(buffer,"keypressed=%d",w);
TextOut(hdc,0,50,buffer,strlen(buffer));
break;
case WM_DESTROY:
PostQuitMessage(0);break;
default:return DefWindowProc(hwnd,x,w,l);
}return 0;}

left mouse button

DISPLAYING TEXT ON LEFT MOUSE BUTTON CLICK
Step6:Window Procedure
a) WM_LBUTTONDOWN:
i)Get the Device context handle and the coordinates of the client area whenever left mouse
button is clicked.
ii)Draw the text in the client area.
b)WM_DESTROY
Post quit Message that generates WM_QUIT
c) Default:
Other messages are handled by the default window procedure.
long _stdcall WProc (HWND hwnd,UINT x,WPARAM w,LPARAM z)
{
int xi,yi;
HDC d;
switch(x)
{
case WM_LBUTTONDOWN:
x1=LOWORD(z);
y1=HIWORD(z);
d=GetDC(hwnd);
TextOut(d,x1,y1,”HELLO”,5);
ReleaseDC(hwnd,d);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,x,w,l);
}
return 0;
}

drawing text

DRAWING Text

Step1: Select File- >New- >Projects- > Win32 Application-> Ok- > Empty project-> Ok

Step2: Select File- >New- >Files- >C++ Source File -> Ok

Step3:Define WNDCLASS structure and Register

Step4:Creating the window based on the WNDCLASS and displaying.

Step5:Message Loop – Retrieve and dispatch the messages to the window procedure

Step6:Window Procedure

a) WM_LBUTTONDOWN:

i)set the flag variable to 1.

b)WM_MOUSEMOVE:

i)Get the Device context handle and the coordinates of the client.

ii)Set the pixel as any one of the colours.

c)WM_LBUTTONUP:

reset the flag variable to zero.

d)WM_DESTROY

Post quit Message that generates WM_QUIT

e) Default:

Other messages are handled by the default window procedure.

CODE:

#include

int flag=0;

long _stdcall wproc(HWND,UINT,UINT,long);

WNDCLASS a;

int WINAPI WinMain(HINSTANCE i,

HINSTANCE j,

char *k,

int l)

{

HWND h;

MSG m;

a.style=CS_HREDRAW|CS_VREDRAW;

a.hInstance=i;

a.lpszClassName="c1";

a.lpfnWndProc=wproc;

a.hbrBackground=HBRUSH(GetStockObject(WHITE_BRUSH));

RegisterClass(&a);

h=CreateWindow("c1","title",WS_OVERLAPPEDWINDOW,10,10,150,300,0,0,i,0);

ShowWindow(h,l);

while(GetMessage(&m,0,0,0))

{DispatchMessage(&m);}

return 0;

}

long _stdcall wproc(HWND hwnd,UINT x,UINT y,long z)

{

HDC hdc;

switch(x)

{

case WM_LBUTTONDOWN:

flag=1;

break;

case WM_MOUSEMOVE:

if(flag==1)

{

hdc=GetDC(hwnd);

SetPixel(hdc,LOWORD(z),HIWORD(z),RGB(255,0,0));

ReleaseDC(hwnd,hdc);

}

break;

case WM_LBUTTONUP:

flag=0;

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hwnd,x,y,z);

}

return 0;}