Click here to Skip to main content
13,591,259 members
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 2 Jun 2006

Custom Control Graph and Process Bar

, 13 Dec 2006
Rate this:
Please Sign up or sign in to vote.
An article on a Win32 custom graph control and process bar creation.

Sample Image


In my project, I needed a graph to display data. My colleague suggested me to go for a custom graph control. I surfed the net but didn’t find any good and interesting material on creating my own custom controls in Win32. So finally, I decided to take a step and wrote this article. To make things more interesting, I have created a DLL so as to reuse the same control in future projects. The control is developed in C. The main aim of the project is re-usability. You can draw any number of graphs / process bars by differentiating the graph number and the custom control ID.


This is a very simple application and I have given step by step instructions on how to create your custom control. This project contains two applications.

  1. Graph_dll - A DLL which is used to draw the graph and the process bar.
  2. Sun_Check_dll - A sample application which describes how to use the DLL.

Graph_dll Application

The Graph_dll is a DLL which provides several functionalities.

Graph Creation Routine

void DrawGraph(HDC hdc, RECT Rect);

The DrawGraph routine is used to draw a graph using the specified device context. The graph size is specified using the Rect parameter. Also, the graph’s vertical movement is implemented in this routine.

void UpdateGraph(HDC hdc, RECT Rect, unsigned long RxValue,
                 unsigned long TxValue, int GraphNo);

The UpdateGraph routine will plot the graph for 30 points horizontal. This can plot only two lines. The value which is specified as ‘RxValue’ will be plotted with green color and the ‘TxValue’ value with red color. The parameter ‘GraphNo’ is nothing but the graph ID, used to identify the graph and to plot the value on the specified graph.

Processbar Creation Routine

void DrawBar(HDC hdc, RECT Rect, int Process_Value);

The DrawBar routine is used to design the process bar in a percentage (%) basis. The second parameter ‘Process_Value’ contains the percentage value. Based on that, the process bar will be filled with green color.

Registering the Global Class

void InitCustGraph();

This routine will register a class name called "Custom_Control", globally, using the RegisterClassEx API, with a callback function called CustGraphWindowProc. This callback function contains two user defined messages, WM_USER_GRAPH and WM_USER_PROCESS_BAR. When you want to draw the graph, you have to send the WM_USER_GRAPH message. The WM_USER_PROCESS_BAR message has to be sent when we need to draw the process bar.

Sun_Check_dll Application

Sun_Check_dll, a simple demo application, explains the use of the custom graph control and the process bar DLL. Some garbage values are selected randomly to display the animated graph and the process bar in this demo application. In this application, two custom controls (IDC_CUSTOM1 and IDC_CUSTOM2) are placed in the dialog and given the same class name “Custom_Control”. For IDC_CUSTOM1, the WM_USER_GRAPH message is sent with the data structure (User_Data) as a reference with the LPARAM parameter. And for IDC_CUSTOM2, the WM_USER_PROCESS_BAR message is sent to update the process bar with the percentage value. This message is sent on a timer interval of 1000 ms.

Using the Code

To use the above functionalities (DLL), first copy the DLL and place it in your workspace folder. Then, create a dialog, select the custom control from the control toolbar, and place it in the dialog. Select the properties of the custom control, and change the "class" attribute to "Custom_Control".

Now, send the user defined message WM_USER_GRAPH / WM_USER_PROCESS_BAR using the SendDlgItemMessage API.

Message for Drawing the Graph

                  (WPARAM) NULL, (LPARAM) &User_Data);

Before sending the above message, the data has to be formatted with the data structure shown below:

struct lp
    Unsigned long RxValue;
    Unsigned long TxValue;
    Unsigned int  GraphNo;
  • RxValue - Plot graph in green color.
  • TxValue - Plot graph in red color.
  • GraphNo - Graph ID (Identifies the graph to plot the current data).


//(Specified the current value for Graph No : 0)
User_Data.GraphNo = 0;
//(Dynamically changeable)
User_Data.RxValue = 10;
//(Dynamically changeable)
User_Data.TxValue = 100;

This data has to be send with the LPARAM parameter as a reference.

Message for Drawing the Processbar

SendDlgItemMessage(hdlg, (int) IDC_CUSTOM1, WM_USER_PROCESS_BAR ,
                  (WPARAM) NULL, (LPARAM) Process_Value);

Process_Value is the data for drawing the process bar. This also should be send with LPARAM. Since it is an ‘int’ value, we can directly send the data, not the reference.

  • Note 1: To redraw / repaint the custom control, the message has to be sent again with the latest values.
  • Note 2: A custom control can also be drawn with the ‘CreateWindow’ API by using the "Custom_Contol" class name.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Shanmuga Sundar.V
India India
Module Lead working in leading MNC company, india - bangalore.

You may also be interested in...


Comments and Discussions

QuestionHow to create a graph? Pin
sankars32-Aug-17 22:44
membersankars32-Aug-17 22:44 
QuestionWant to show continuous sine waves in windows application Pin
Member 1286844228-Dec-16 0:11
memberMember 1286844228-Dec-16 0:11 
GeneralMy vote of 5 Pin
Nirav Chauhan16-Jul-13 6:05
memberNirav Chauhan16-Jul-13 6:05 
GeneralGraph goes blank Pin
vishwas gurung1-Nov-07 17:55
membervishwas gurung1-Nov-07 17:55 
GeneralMissing ReleaseDC Pin
Jeff Glatt15-Dec-06 3:12
memberJeff Glatt15-Dec-06 3:12 
GeneralRe: Missing ReleaseDC [modified] Pin
Shanmuga Sundar.V15-Dec-06 22:17
memberShanmuga Sundar.V15-Dec-06 22:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180618.1 | Last Updated 13 Dec 2006
Article Copyright 2006 by Shanmuga Sundar.V
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid