Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC

Custom Control Graph and Process Bar

Rate me:
Please Sign up or sign in to vote.
4.35/5 (14 votes)
13 Dec 20064 min read 92.6K   6.8K   45  
An article on a Win32 custom graph control and process bar creation.
// Sun_Check_DLL.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING			100
#define WM_USER_GRAPH           0x0401
#define WM_USER_PROCESS_BAR		0x0402

struct lp
{
	unsigned long RxValue;
	unsigned long TxValue;
	unsigned int  GraphNo;
};

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

static BOOL CALLBACK DlgProc( HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam );

struct lp User_Data;
HWND	hwnd;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	LoadLibrary("Graph_dll.dll");
	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_SUN_CHECK_DLL, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SUN_CHECK_DLL);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_SUN_CHECK_DLL);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_SUN_CHECK_DLL;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }
	
   hwnd = hWnd;
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	
	BOOL rc;
	long err;

	RECT rt;

	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				case ID_FILE_NEWDLG:
				   rc = DialogBox(
							hInst,    //  application instance
							MAKEINTRESOURCE( IDD_DIALOG1 ), //  resource template
							hWnd,                         //  owner
							DlgProc          //  dialog procedure
						);
				   err = GetLastError();
					break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			GetClientRect(hWnd, &rt);
			SetTextColor(hdc, RGB(255, 000, 000));
			DrawText(hdc, "Welcome to custom control demo.,", strlen("Welcome to custom control demo.,"), &rt, DT_CENTER);
			rt.top = 30;
			SetTextColor(hdc, RGB(000, 255, 000));
			DrawText(hdc, "Click File->Demo Dialog to proceed...", strlen("Click File->Demo Dialog to proceed..."), &rt, DT_CENTER);
			EndPaint(hWnd, &ps);
			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

BOOL CALLBACK DlgProc( HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
   static int RxValue, TxValue;
   char Buf[10];

   switch( msg )
   {
   case WM_INITDIALOG:
      SetFocus( GetDlgItem( hdlg, IDCANCEL ) );
	  SetTimer(hdlg,0, 1000, NULL); 
      return( FALSE );

   case WM_DESTROY:
      return( TRUE );

   case WM_TIMER:          //  timeout - no response from NS

	  User_Data.GraphNo = 0;
	  User_Data.RxValue = RxValue;
	  User_Data.TxValue = TxValue;

	  SendDlgItemMessage(hdlg, (int) IDC_CUSTOM1, WM_USER_GRAPH,(WPARAM) NULL, (LPARAM) &User_Data);
	  SendDlgItemMessage(hdlg, (int) IDC_CUSTOM2, WM_USER_PROCESS_BAR,(WPARAM) NULL, (LPARAM) TxValue);

	  sprintf(Buf,"%d", RxValue);
	  SetDlgItemText(hdlg, (int) IDC_EDIT1, Buf);

	  sprintf(Buf, "%d", TxValue);
	  SetDlgItemText(hdlg, (int) IDC_EDIT2, Buf);

	  // Random values selectable for demo 
	  if(RxValue > 150)
		RxValue -= 27;
	  else
	 	RxValue += 12;
		
	  if(TxValue > 80)
		TxValue -= 17;
	  else
		TxValue += 12;

      return( TRUE );

   case WM_COMMAND:

      switch( LOWORD( wParam ) )
      {
		  case IDOK:
			EndDialog( hdlg, FALSE );
			return( TRUE );

		  case IDCANCEL:
			EndDialog( hdlg, FALSE );
			return( TRUE );
      }
      break;
   }
   return( FALSE );
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect Luxoft
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions