1) Write a program in vc++ to create a simple window using MFC
#include<afxwin.h>
class myframe:public CFrameWnd
{
public:
myframe()
{
Create (0,"Simple Window");
}
};
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe*p;
p=new myframe;
p->ShowWindow(3);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:
2) Write a program in vc++ to create a Window in different style using MFC
#include<afxwin.h>
class myframe:public CFrameWnd
{
public:
myframe()
{
Create (0,"Simple
Window",WS_OVERLAPPEDWINDOW,CRect(20,200,200,200));
}
};
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe*p;
p=new myframe;
p->ShowWindow(1);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:-
3) Write a program in vc++ to create a window of desired size.
#include<afxwin.h>
class myframe:public CFrameWnd
{
public:
myframe()
{
Create(0,"Desired Size",WS_OVERLAPPEDWINDOW,CRect(10,10,400,300));
}
};
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe *p;
p=new myframe;
p->ShowWindow(1);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:
4) Write a program in vc++ to create a window of my own class.
#include<afxwin.h>
class myframe:public CFrameWnd
{
public:
myframe()
{
CString mywindowclass;
HBRUSH mybrush;
mybrush=(HBRUSH)::GetStockObject(LTGRAY_BRUSH);
mywindowclass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,
AfxGetApp()->LoadStandardCursor(IDC_CROSS),mybrush,
AfxGetApp()->LoadStandardIcon(IDI_EXCLAMATION));
Create(mywindowclass,"Hello MFC");
}
};
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe *p;
p=new myframe;
p->ShowWindow(3);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:
5) Write a program in vc++ to handle Windows messages in a MFC program.
#include<afxwin.h>
class myframe:public CFrameWnd
{
public:
myframe()
{
Create(0,"Message Demo");
}
int OnCreate()
{
MessageBox("OnCreate message handler","Reached");
return 0;
}
void OnPaint()
{
CFrameWnd::OnPaint();
MessageBox("OnPaint message handler","Reached");
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe, CFrameWnd)
ON_WM_CREATE()
ON_WM_PAINT()
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:
6) Write a program in vc++ to draw a line and rectangle in a window.
#include<afxwin.h>
class myframe :public CFrameWnd
{
public:
myframe()
{
Create(0,"Line And Rectangle");
}
void OnPaint()
{
CPaintDC d(this);
d.MoveTo (50,50);
d.LineTo (250,10);
d.Rectangle (50,100,250,200);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
class myapp :public CWinApp
{
public:
int InitInstance()
{
myframe*p;
p=new myframe;
p->ShowWindow (3);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:
7) Write a program in vc++ to create my own pen to draw a line and rectangle and create my own brush to fill the rectangle.
#include<afxwin.h>
class myframe :public CFrameWnd
{
public:
myframe()
{
Create(0,"MY OWN PEN AND BRUSH");
}
void OnPaint()
{
CPaintDC d(this);
CPen mypen (PS_SOLID,5, RGB(0,0,255));
CBrush mybrush (RGB(255,0,0));
d.SelectObject(&mypen);
d.SelectObject(&mybrush);
d.MoveTo (50,50);
d.LineTo (250,10);
d.Rectangle (50,100,250,200);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
class myapp :public CWinApp
{
public:
int InitInstance()
{
myframe*p;
p=new myframe;
p->ShowWindow (3);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:
8) Write a program in vc++ to draw standard shapes in a window.
#include<afxwin.h>
class myframe:public CFrameWnd
{
public:
myframe()
{
Create(0,"Standard Shapes");
}
void OnPaint()
{
CPaintDC d(this);
CBrush mybrush (RGB(344,0,0));
d.SelectObject(&mybrush);
d.MoveTo(10,10);
d.LineTo(200,10);
DeleteObject(&mybrush);
CBrush mybrush1(RGB(255,255,0));
d.SelectObject(&mybrush1);
d.Rectangle(10,20,200,100);
DeleteObject(&mybrush);
CBrush mybrush2(RGB(0,255,100));
d.SelectObject(&mybrush2);
d.RoundRect(10,120,200,220,20,20);
DeleteObject(&mybrush);
CBrush mybrush3(RGB(100,20,0));
d.SelectObject(&mybrush3);
d.Ellipse(10,240,200,340);
DeleteObject(&mybrush);
CBrush mybrush4(RGB(0,255,0));
d.SelectObject(&mybrush4);
d.Pie(250,10,350,110,350,110,350,10);
POINT pt[5]={250,150,250,300,300,350,400,300,320,190};
d.Polygon(pt,5);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe*p;
p=new myframe;
p->ShowWindow(3);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:
9) Write a program in vc++ to attach menu to a window.
#include<afxwin.h>
#include"resource.h"
class myframe:public CFrameWnd
{
public:
myframe()
{
Create(0,"HELLO MFC",WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));
}
};
class myapp:public CWinApp
{
public:
int InitInstance()
{
myframe*p;
p=new myframe;
p->ShowWindow (1);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:
10) Write a program in vc++ for building the application which display text string.
#include<afxwin.h>
class colorfont:public CFrameWnd
{
public:
colorfont()
{
Create(0,"GDI OBJECT");
}
void OnPaint()
{
CPaintDC d(this);
CFont f;
f.CreateFont(50,50,0,0,FW_BOLD,1,1,0,DEFAULT_CHARSET,0,0,0,0,"Times New Roman");
d.SelectObject(&f);
d.SetTextColor (RGB(0,0,100));
d.SetBkColor (RGB(100,100,100));
d.TextOut (100,100,"KAMLA NEHRU");
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(colorfont,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
class myapp:public CWinApp
{
public:
int InitInstance()
{
colorfont*p;
p=new colorfont;
p->ShowWindow (1);
m_pMainWnd=p;
return 1;
}
};
myapp a;
OUTPUT:
11) Write a program in vc++ for demonstrating Cbrush using different brush style.
#include<afxwin.h>
#include "resource.h"
class brushstyle:public CFrameWnd
{
public:
brushstyle()
{
Create(0,"GDI OBJECT");
}
void OnPaint()
{
CPaintDC d(this);
CBrush b;
b.CreateSolidBrush (RGB(255,0,0));
d.SelectObject (&b);
d.Rectangle (50,5,250,105);
b.DeleteObject ();
b.CreateHatchBrush (HS_CROSS,RGB(255,0,0));
d.SelectObject (&b);
d.Rectangle (50,150,250,250);
b.DeleteObject ();
CBitmap bm;
bm.LoadBitmap (IDB_BITMAP1);
b.CreatePatternBrush (&bm);
d.SelectObject (&b);
d.Rectangle (50,300,250,400);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(brushstyle,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
class myapp:public CWinApp
{
public:
int InitInstance()
{
brushstyle*p;
p=new brushstyle;
p->ShowWindow (1);
m_pMainWnd=p;
return 1;
}
};
myapp a;
12. Write a program in VC++ to generate a popup menu at the position where we click the right mouse button.
#include<afxwin.h>
class myframe:public CFrameWnd
{
public:
myframe()
{
Create(0,"Context Menu");
}
void OnContextMenu(CWnd*p,CPoint pt)
{
CRect r;
GetClientRect(&r);
ClientToScreen(&r);
if(r.PtInRect (pt))
{
CMenu mymenu;
mymenu.CreatePopupMenu ();
mymenu.AppendMenu (MF_STRING,101,"&Line");
mymenu.AppendMenu (MF_STRING,102,"&Rectangle");
mymenu.AppendMenu (MF_STRING,103,"&Circle");
mymenu.AppendMenu (MF_STRING,104,"&Triangle");
mymenu.AppendMenu (MF_STRING,105,"&Polygon");
mymenu.AppendMenu (MF_STRING,106,"&Bezier");
mymenu.TrackPopupMenu (TPM_CENTERALIGN|TPM_RIGHTBUTTON,pt.x ,pt.y ,this,0);
}
else
CFrameWnd::OnContextMenu (p,pt);
}
void line()
{
MessageBox("Draws a line","Line");
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_WM_CONTEXTMENU()
ON_COMMAND(101,line)
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: