Code:
#include "stdafx.h"
#include <windows.h> //include all the basics
#include <tchar.h> //string and other mapping macros
#include <string>
// Define structure for colors
typedef struct rgbColors {
int R,G,B;
} RGBColor;
COLORREF g_rgbBackground = RGB(0xD3,0xD3,0xD3); // Set to grey
#define MAXCOLOR 10
//define an unicode string type alias
typedef std::basic_string<TCHAR> ustring;
//=============================================================================
//message processsing function declarations
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
// function declarations
inline int ErrMsg(const ustring&);
HWND OnCreate(const HWND,CREATESTRUCT*, RECT& rc);
RECT initRecArea(int left, int top, int width, int height);
void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC);
void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, HDC hdc, int index);
void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, HBRUSH hbrush, const RECT& rc);
HWND CreateEdit(const HWND,const HINSTANCE,DWORD,const RECT&,const int, const ustring&);
HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
const RECT& rc,const int id,const ustring& caption);
//setup some edit control id's
enum {
IDCE_SINGLELINE=200,
IDCE_MULTILINE,
IDCE_DLG,
IDBC_DEFPUSHBUTTON,
IDBC_PUSHBUTTON,
IDBC_AUTOCHECKBOX,
IDBC_AUTORADIOBUTTON,
IDBC_GROUPBOX,
IDBC_ICON,
IDBC_BITMAP
};
// Globals
PAINTSTRUCT pntS;
HWND editCntrl;
//=============================================================================
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd)
{
ustring classname=_T("RFS Range");
WNDCLASSEX wcx={0}; //used for storing information about the wnd 'class'
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.lpfnWndProc = WndProc; //wnd Procedure pointer
wcx.hInstance = hInst; //app instance
wcx.hIcon = (HICON)LoadImage(0,IDI_APPLICATION,IMAGE_ICON,0,0,LR_SHARED);
wcx.hCursor = (HCURSOR)LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED);
wcx.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE+1);
wcx.lpszClassName = classname.c_str();
if (!RegisterClassEx(&wcx))
{
ErrMsg(_T("Failed to register wnd class"));
return -1;
}
int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
int desktopheight=GetSystemMetrics(SM_CYSCREEN);
HWND hwnd=CreateWindowEx(0, //extended styles
classname.c_str(), //name: wnd 'class'
_T("RFS Range"), //wnd title
WS_OVERLAPPEDWINDOW, //wnd style
desktopwidth/4, //position:left
desktopheight/4, //position: top
desktopwidth/2, //width
desktopheight/2, //height
0, //parent wnd handle
0, //menu handle/wnd id
hInst, //app instance
0); //user defined info
if (!hwnd)
{
ErrMsg(_T("Failed to create wnd"));
return -1;
}
ShowWindow(hwnd,nCmd);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg,0,0,0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}
//=============================================================================
LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
int pos=0;
HDC hdc;
char szString[100];
int maxChars = 20;
HWND hWndDLG;
RGBColor colors[MAXCOLOR] = {{0xD3,0xD3,0xD3},{0x00,0xFF,0x00},
{0xFF,0xFF,0xFF},{0x00,0x00,0x00}};
CREATESTRUCT *cs = reinterpret_cast<CREATESTRUCT*>(lParam);
switch (uMsg)
{
case WM_CREATE:
editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));
CreateButton(hwnd,cs->hInstance,BS_DEFPUSHBUTTON,initRecArea(305,320,150,30),
IDBC_DEFPUSHBUTTON,_T("DISPLAY"));
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &pntS);
// Draw unto the client area
PaintObject(hwnd,initRecArea(10,10,300,200),colors, hdc, pos);
printText(hwnd, "Hello there", initRecArea(15,15,0,0), hdc);
PaintObject(hwnd,initRecArea(305,10,605,250),colors, hdc, pos);
PaintObject(hwnd,initRecArea(10,205,300,315),colors, hdc, pos);
PaintObject(hwnd,initRecArea(10,320,300,420),colors, hdc, pos);
EndPaint(hwnd, &pntS);
case WM_COMMAND:
if (LOWORD(wParam) == IDBC_DEFPUSHBUTTON)
{
GetDlgItemText(hwnd, IDCE_DLG, szString, 80);
printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
}
return 0;
case WM_TIMER:
/* Force the WM_PAINT */
//SetTimer(hwnd, ID_TIMER, 500, NULL);
//InvalidateRect (hwnd, NULL, TRUE);
//UpdateWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0); //signal end of application
return 0;
default:
//let system deal with msg
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
}
//=============================================================================
inline int ErrMsg(const ustring& s)
{
return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
}
//=============================================================================
// EDIT control
HWND CreateEdit(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
const RECT& rc,const int id,const ustring& caption)
{
dwStyle|=WS_CHILD|WS_VISIBLE;
return CreateWindowEx(WS_EX_CLIENTEDGE, //extended styles
_T("edit"), //control 'class' name
caption.c_str(), //control caption
dwStyle, //control style
rc.left, //position: left
rc.top, //position: top
rc.right, //width
rc.bottom, //height
hParent, //parent window handle
reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
hInst, //application instance
0); //user defined info
}
//=============================================================================
// BUTTON Control
HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
const RECT& rc,const int id,const ustring& caption)
{
dwStyle|=WS_CHILD|WS_VISIBLE;
return CreateWindowEx(0, //extended styles
_T("button"), //control 'class' name
caption.c_str(), //control caption
dwStyle, //control style
rc.left, //position: left
rc.top, //position: top
rc.right, //width
rc.bottom, //height
hParent, //parent window handle
reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
hInst, //application instance
0); //user defined info
}
// Purpose: Draws an object on the window
void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, HDC hdc, int index)
{
HPEN hpen = CreatePen(PS_SOLID, 2, RGB(color[index+3].R, color[index+3].G, color[index+3].B));
HBRUSH hbrush = CreateSolidBrush(RGB(color[index].R, color[index].G, color[index].B));
DrawARectangle(hwnd, hpen, hdc, hbrush, rc);
}
// Purpose: Draws a rectangle which will have text boxes for input data
void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, HBRUSH hbrush, const RECT& rc)
{
// Select the new pen and brush, and then draw.
HPEN hpenOld = (HPEN)SelectObject(hdc, hpen);
HBRUSH hbrushOld = (HBRUSH)SelectObject(hdc, hbrush);
Rectangle(hdc,rc.left,rc.top,rc.right,rc.bottom);
// Do not forget to clean up.
SelectObject(hdc, hpenOld);
DeleteObject(hpen);
SelectObject(hdc, hbrushOld);
DeleteObject(hbrush);
}
// Initialize rectangle area
RECT initRecArea(int left, int top, int width, int height)
{
RECT rec = {left, top, width, height};
return rec;
}
//handles the WM_CREATE message of the main, parent window; return -1 to fail
HWND OnCreate(const HWND hwnd,CREATESTRUCT *cs, RECT& rc)
{
return CreateEdit(hwnd,cs->hInstance,0,rc,IDCE_SINGLELINE,_T(""));
}
// Display text on screen
void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC)
{
HDC hdc = GetDC(NULL);
long lfHeight = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
ReleaseDC(NULL, hdc);
HFONT font = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
SelectObject(hDC, font);
SetBkColor(hDC, g_rgbBackground);
TextOut(hDC, rc.left, rc.top, txt, (int)_tcslen(txt));
DeleteObject(font);
}