I have written a small Windows application that draws India's flag in the window. I am very new to using Visual C++ and I want to understand if my code can be improved further.
#include "afxwin.h"
#include <math.h>
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance ();
};
class CMainWindow : public CFrameWnd
{
public:
CMainWindow();
protected:
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP();
};
CMyApp myAPP;
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
CMainWindow::CMainWindow ()
{
Create(NULL,_T("India's Flag"), WS_OVERLAPPEDWINDOW );
}
void IndiaFlag(CDC &dc, int x, int y)
{
dc.SetBkMode(TRANSPARENT);
CRect rect;
CPen pen(PS_SOLID, 1, RGB(0,0,0));
CPen *oldPen = dc.SelectObject(&pen);
{
CBrush brush(RGB(255,130,0));
CBrush *oldBrush = dc.SelectObject(&brush);
dc.Rectangle(x,y,x+200,(y+50));
}
{
CBrush brush(RGB(255,255,255));
CBrush *oldBrush = dc.SelectObject(&brush);
dc.Rectangle(x,(y+50),(x+200),(y+100));
CPen pen2(PS_SOLID, 1,RGB(0,0,255));
CPen *oldPen = dc.SelectObject(&pen2);
dc.Ellipse((x+75),(y+50),(x+125),(y+100));
double Nx,Ny;
for (int angle=0;angle<360; angle+=15)
{
int angle2 = angle;
double length = 25*(cos(double(angle2 *(3.14159265 / 180))));
double len2 = 25*(sin(double(angle2 *(3.14159265 / 180))));
int originX = (x+100);
int originY = (y+75);
Nx = originX + length;
Ny = originY - len2;
dc.MoveTo(originX,originY);
dc.LineTo(Nx,Ny);
}
}
{
CBrush brush(RGB(34,139,34));
CBrush *oldBrush = dc.SelectObject(&brush);
dc.Rectangle(x,(y+100),(x+200),(y+150));
}
}
void CMainWindow::OnPaint ()
{
CPaintDC dc(this);
IndiaFlag(dc, 150, 150);
}
In case you wondered, this is what the Indian flag looks like: