Draw a circle on a Windows form

vegaseat 0 Tallied Votes 2K Views Share

This snippet shows how to draw a red circle on a Windows form. Original code via BCX, modified to compile with Dev C++. The GUI code looks a little complex. Gets simpler, once you get past the required overhead.

For those who need some hand holding with the Dev C++ IDE:
In the IDE go to FILE, then NEW, then Project, select Windows Application, give it a name (eg. Circle1) click OK. A filesave dialog box comes up, create a new folder and save Circle1.dev there. The DevCpp IDE comes up with a template, select and delete that and cut and paste this code into the empty editor page. Now compile and run.

// draw a simple circle given center coordinates and radius 
// inside a Windows form,  uses Win API function  Ellipse()
/*
BOOL Ellipse(
  HDC hdc,      // handle to device context 
  int nLeftRect,  // x-coord. of bounding rectangle's upper-left corner 
  int nTopRect,  // y-coord. of bounding rectangle's upper-left corner  
  int nRightRect, // x-coord. of bounding rectangle's lower-right corner  
  int nBottomRect // y-coord. bounding rectangle's f lower-right corner  
);	
*/ 
// BCX output modified for Dev-C++
// a Dev-C++ tested Windows Application by  vegaseat  05nov2004

#include <windows.h>

static HINSTANCE BCX_hInstance;
static int     BCX_ScaleX;
static int     BCX_ScaleY;
static char    BCX_ClassName[2048];
static HWND    Form1;

#define Show(Window) RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);
#define Red  RGB(255,0,0)

HWND    BCX_Form(char*,int=0,int=0,int=250,int=150,int=0,int=0);
int     BCX_Circle (HWND,int,int,int,int=0,int=0,HDC=0);

void    FormLoad (void);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);


// standard main for GUI programs
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int CmdShow)
{
 WNDCLASS Wc;
 MSG      Msg;
 // *****************************
 strcpy(BCX_ClassName,"DRAW_CIRCLE");
 // ***************************************
 // Programmer has selected to use pixels
 // (adjust the scaling to 1 accordingly) 
 // ***************************************
 BCX_ScaleX       = 1;
 BCX_ScaleY       = 1;
 BCX_hInstance    =  hInst;
 // ******************************************************
 Wc.style         =  CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
 Wc.lpfnWndProc   =  WndProc;
 Wc.cbClsExtra    =  0;
 Wc.cbWndExtra    =  0;
 Wc.hInstance     =  hInst;
 Wc.hIcon         =  LoadIcon(NULL,IDI_WINLOGO);
 Wc.hCursor       =  LoadCursor(NULL,IDC_ARROW);
 Wc.hbrBackground =  (HBRUSH)(COLOR_BTNFACE+1);
 Wc.lpszMenuName  =  NULL;
 Wc.lpszClassName =  BCX_ClassName;
 RegisterClass(&Wc);

 FormLoad();
  // message loop for events
 while(GetMessage(&Msg,NULL,0,0))
 {
   HWND hActiveWindow = GetActiveWindow();
    if (!IsWindow(hActiveWindow) || !IsDialogMessage(hActiveWindow,&Msg))
    {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
    }
  }
 return Msg.wParam;
}


// creates the form (window)
HWND BCX_Form(char *Caption, int X, int Y, int W, int H, int Style, int Exstyle)
{
   HWND  A;
   // assign default style if none is given
   if (!Style)
   {
     Style = WS_MINIMIZEBOX  |
     WS_SIZEBOX      |
     WS_CAPTION      |
     WS_MAXIMIZEBOX  |
     WS_POPUP        |
     WS_SYSMENU;
   }
   A = CreateWindowEx(Exstyle,BCX_ClassName,Caption,
    Style,
    X*BCX_ScaleX,
    Y*BCX_ScaleY,
    (4+W)*BCX_ScaleX,
    (12+H)*BCX_ScaleY,
    NULL,(HMENU)NULL,BCX_hInstance,NULL);
   SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
     (LPARAM)MAKELPARAM(FALSE,0));
   return A;
}


// notice conversion from circle(centerX,centerY,radius) to
// ellipse(ulcX,ulcY,lrcX,lrcY)
int BCX_Circle (HWND Wnd,int X,int Y,int R,int Pen,int Fill,HDC DrawHDC)
{
  int a,b=0;
  if (!DrawHDC) 
  {
    DrawHDC=GetDC(Wnd);
    b=1;
  }
  HPEN   hNPen=CreatePen(PS_SOLID,1,Pen);
  HPEN   hOPen=(HPEN)SelectObject(DrawHDC,hNPen);
  HBRUSH hOldBrush;
  HBRUSH hNewBrush;
  if (Fill)
  {
     hNewBrush=CreateSolidBrush(Pen);
     hOldBrush=(HBRUSH)SelectObject(DrawHDC,hNewBrush);
  }
  else
  {
     hNewBrush=(HBRUSH)GetStockObject(NULL_BRUSH);
     hOldBrush=(HBRUSH)SelectObject(DrawHDC,hNewBrush);
  }
  // Win API function
  a = Ellipse(DrawHDC,X-R,Y+R,X+R,Y-R);
  DeleteObject(SelectObject(DrawHDC,hOPen));
  DeleteObject(SelectObject(DrawHDC,hOldBrush));
  if (b) ReleaseDC(Wnd,DrawHDC);
  return a;
}


// form (window) details
// title,upper left corner x,y  width,height
void FormLoad (void)
{
  Form1 = BCX_Form("Draw a circle",50,50,210,210);
  Show(Form1);
}


// standard GUI message handling
LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
  while(1)
  {
    if (Msg == WM_PAINT)
    {
      //  hwin, centerX, centerY, radius, pencolor 
      BCX_Circle(Form1,100,100,55,Red);
      // you can play around here, add more circles or whatever
      // BCX_Circle(Form1,120,80,55,RGB(0,173,0));  // green
      // BCX_Circle(Form1,80,120,55,RGB(0,0,255));  // blue
      // to draw a circle filled with the pencolor use
      // BCX_Circle(Form1,100,100,55,Red,1);
    }
    break;
  }
  // clean up and exit program
  if (Msg == WM_DESTROY)
  {
     UnregisterClass(BCX_ClassName,BCX_hInstance);
     PostQuitMessage(0);
  }
  return DefWindowProc(hWnd,Msg,wParam,lParam);
}

// ************** credit to Kevin ******************************
//   Created with BCX -- The BASIC To C Translator (ver 5.02)
//  BCX (c) 1999, 2000, 2001, 2002, 2003, 2004 by Kevin Diggins
// *************************************************************
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.