Histogram control

histogram control  Download Source Code and Example

This custom control displays a left scrolling spike with range configurable by calling SetRange(0,100), the first parameter is the min(bottom) value and the second parameter is max(top) value.

The control is designed to work just like the progress control but with a twist. Every SetPos() will shows a spike and the next SetPos() will scroll the previous one to the left.

The CHistogramCtrl class is derived from CWnd, the header and code are in HistogramCtrl.h and HistogramCtrl.cpp files.


In your dialog box, you must create the control as static control (use "picture" from control toolbar). You must set the properties to

  • Type: Rectangle, and
  • Color: black. (I think black background looks better)

Don't worry about the height, width and border, the control will ajust itself and draw the border for you.

First add a member to your dialog class like this:

class CHistogramTestDlg : public CDialog
// Implementation
	CHistogramCtrl m_HistogramCtrl;

And in the initial dialog box...

BOOL CHistogramTestDlg::OnInitDialog()

	CRect rect;

	m_HistogramCtrl.Create(WS_VISIBLE | WS_CHILD, rect, this, 100);
	return TRUE;  // return TRUE  unless you set the focus to a control

And the rest use SetPos(UINT nValue).

The custom control uses unique painting technique which is a little bit to long to discuss here. If you want to understand more please email your FAQ to klen@bellatlantic.net thanks. (The code is not as long to read)

Here is an example of how an application looks like with the control.

histogram control

Last updated: 17 May 1998


  • How to setup Histogram code in Visual C++ 6.0

    Posted by ringram2077 on 02/14/2007 04:46pm

    Not having any luck getting Histogram set up in VC++. Could someone layout the steps to get it setup to compile ? Thanks

  • Another GOOD GDI example

    Posted by Legacy on 03/10/2003 12:00am

    Originally posted by: motty brami

    This is very usefull example, demonstraiting what can be done using GDI, fantasy and programmer's experience.
    Well done. Thanks to programmer!!!

  • Control in Task Manager

    Posted by Legacy on 12/09/2001 12:00am

    Originally posted by: jj

    In Window's Task Manager, under the "performance" sheet,
    there're two graphs showing the "cpu usage" and "memory usage".

    Anyone knows which control can be used to realise this
    kind of drawing?


  • Good job!

    Posted by Legacy on 11/04/2001 12:00am

    Originally posted by: Shiuh Liew


    You have done a good job. The code is simple, clean, and efficient.


  • How to add code

    Posted by Legacy on 08/01/2001 12:00am

    Originally posted by: winne

     Your project -HistogramTest .
    I want to add Fonts and Display in Rectangle.
    How I do?
    Please ,Tell me!
    Thank you!

  • Neat

    Posted by Legacy on 06/07/2001 12:00am

    Originally posted by: Chokri Oueslati

    It is neat and clear. Very easy to handle.

  • How ot preview and print in a doc/view program?

    Posted by Legacy on 05/15/2000 12:00am

    Originally posted by: Joshua Liu

    I create the control in a view;but it cannot preview and
    print.How should I do?
    Thanks very much!
    My Email is joshua0137@sina.com

  • about histogram......

    Posted by Legacy on 11/05/1999 12:00am

    Originally posted by: abc88

    below is Borland c++ builder source code for histogram.Now, i want to do this with visual c++ and the output r also same with what i have done with BCB. Could anyone help me here???provide me visual c++ source codes and command line for histogram as below if possible......

    void __fastcall TForm1::Open1Click(TObject *Sender)
    while (true)
    if (OpenPictureDialog1->Execute())
    POINT tPoint[256]; //coordinates
    int nHist[256];
    //clear all
    memset(nHist,0, sizeof(nHist));
    memset(tPoint,0, sizeof(tPoint));
    //create Hist: for all pixels
    for (int i = 0; i < Image1->Picture->Bitmap->Width; i++)
    for (int j = 0; j < Image1->Picture->Bitmap->Height; j++)
    long tColor1 = Image1->Picture->Bitmap->Canvas->Pixels[i][j];
    BYTE tColor = (BYTE)tColor1;
    nHist[tColor]++; //+1
    //find Max of nHist
    int nMax = nHist[0];
    for (int i = 1; i < 256; i++)
    nMax = max(nMax,nHist[i]);
    //make point with coordinates
    for (int i = 0; i < 256; i++)
    tPoint[i].x = (float)Image3->Width/ (float)(255)*(float)i;
    tPoint[i].y = (float)Image3->Height*(1. - (float)nHist[i]/(float)nMax);
    //display image on frame Image1
    //display graph
    Image3->Canvas->Polyline((const TPoint*)tPoint, 255);
    else // user cancelled
    CGauge1->Progress = 0;

  • Resizing a Custom Control

    Posted by Legacy on 04/17/1999 12:00am

    Originally posted by: M. Qureshi

    I tried to resize the control as the dialog is resized using the following code but it didn't work.

    CRect rect;

    I might use this control with little modification if I could resize it.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds