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;
#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);
int OnCreate(const HWND,CREATESTRUCT*);
//non-message function declarations
HWND CreateEdit(const HWND,const HINSTANCE,DWORD,const RECT&,const int, const ustring&);
void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc,HBRUSH hbrush, PAINTSTRUCT pntS, const RECT& rc);
void PaintObject(HWND hwnd, const RECT& rc, RGBColor *c, int p);
RECT initRecArea(int l,int r,int t,int b);
inline int ErrMsg(const ustring&);
//setup some edit control id's
enum {
IDCE_SINGLELINE=200,
IDCE_MULTILINE,
};
//=============================================================================
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 = reinterpret_cast<HICON>(LoadImage(0,IDI_APPLICATION,IMAGE_ICON,0,0,LR_SHARED));
wcx.hCursor = reinterpret_cast<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
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
/*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;
RGBColor colors[MAXCOLOR] = {{0x80,0x80,0x80},{0x00,0xFF,0x00},
{0xFF,0xFF,0xFF},{0x00,0x00,0x00}};
switch (uMsg)
{
case WM_CREATE:
//return OnCreate(hwnd,reinterpret_cast<CREATESTRUCT*>(lParam));
return 0;
case WM_PAINT:
// Draw rectangle area 1
PaintObject(hwnd,initRecArea(5,5,300,350),colors,pos);
// Draw rectangle area 2
PaintObject(hwnd,initRecArea(400,5,800,250),colors,pos);
return 0;
case WM_TIMER:
/* Force the WM_PAINT */
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);
}
//=============================================================================
int OnCreate(const HWND hwnd,CREATESTRUCT *cs)
{
RECT rc={10,10,200,30};
//the various edit control types are created by simply varying the style bits
CreateEdit(hwnd,cs->hInstance,0,rc,IDCE_SINGLELINE,_T(""));
rc.top+=40;
rc.bottom+=30;
CreateEdit(hwnd,cs->hInstance,ES_MULTILINE|WS_VSCROLL,rc,IDCE_MULTILINE,
_T("Multi\r\nline"));
return 0;
}
//=============================================================================
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
//control's ID
reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
hInst, //application instance
0); //user defined info
}
//=============================================================================
// Purpose: Draws a rectangle which will have text boxes for input data
void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc,
HBRUSH hbrush, PAINTSTRUCT pntS, const RECT& rc)
{
HPEN hpenOld;
HBRUSH hbrushOld;
// Select the new pen and brush, and then draw.
hpenOld = (HPEN)SelectObject(hdc, hpen);
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);
}
// Purpose: Draws an object on the window
void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, int index)
{
HDC hdc;
HPEN hpen;
HBRUSH hbrush;
PAINTSTRUCT pntS;
hdc = BeginPaint(hwnd, &pntS);
hpen = CreatePen(PS_SOLID, 2, RGB(color[index+3].R, color[index+3].G, color[index+3].B));
hbrush = CreateSolidBrush(RGB(color[index].R, color[index].G, color[index].B));
DrawARectangle(hwnd, hpen, hdc, hbrush, pntS, rc);
EndPaint(hwnd, &pntS);
}
// Initialize rectangle area
RECT initRecArea(int left, int top, int width, int height)
{
RECT rec = {left, top, width, height};
return rec;
}