1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
//Main.cpp
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "StrDemo.h"
#include "frmOutput.h"
#include "Strings.h"
EVENTHANDLER EventHandler[3];
long fnWndProc_OnCreate(lpWndEventArgs Wea)
{
TCHAR szClassName[]=_T("frmOutput");
WNDCLASSEX wc;
HWND hCtrl;
Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;
wc.lpszClassName=szClassName; wc.lpfnWndProc=frmOutput;
wc.cbSize=sizeof (WNDCLASSEX); wc.style=CS_DBLCLKS;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hInstance=Wea->hIns;
wc.hIconSm=NULL; wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.cbWndExtra=0;
wc.lpszMenuName=NULL; wc.cbClsExtra=0;
RegisterClassEx(&wc);
hCtrl=CreateWindow(_T("button"),_T("String Concatenation"),WS_CHILD|WS_VISIBLE,45,20,250,25,Wea->hWnd,(HMENU)IDC_CONCATENATION,Wea->hIns,0);
hCtrl=CreateWindow(_T("button"),_T("String Parsing And Trimming"),WS_CHILD|WS_VISIBLE,45,55,250,25,Wea->hWnd,(HMENU)IDC_PARSE,Wea->hIns,0);
hCtrl=CreateWindow(_T("button"),_T("Good Old Left, Right, And Mid!"),WS_CHILD|WS_VISIBLE,45,90,250,25,Wea->hWnd,(HMENU)IDC_LEFT_RIGHT_MID,Wea->hIns,0);
hCtrl=CreateWindow(_T("button"),_T("But Led's Not Forget InStr( )!"),WS_CHILD|WS_VISIBLE,45,125,250,25,Wea->hWnd,(HMENU)IDC_INSTR,Wea->hIns,0);
return 0;
}
void btnConcatenation_OnClick(void)
{
HWND hOutput;
String s;
int iY=0;
HDC hDC;
hOutput=CreateWindow(_T("frmOutput"),_T("String Class Minipulations In C++"),WS_OVERLAPPEDWINDOW,450,20,305,480,0,0,GetModuleHandle(0),0);
ShowWindow(hOutput,SW_SHOWNORMAL);
hDC=GetDC(hOutput);
for(unsigned int i=65; i<91; i++)
{
s=s+i; //65 Is Capital 'A'
TextOut(hDC,0,iY,s.lpStr(),s.LenStr()); //TextOut() is the Windows Version Of Basic's 'Print'
iY=iY+16; //A Spacing Of 16 Pixels Works Good For Most Normal Sized Fonts
}
ReleaseDC(hOutput,hDC);
}
void btnParse_OnClick(void)
{
String* ptrStrings;
unsigned int iCt;
HWND hOutput;
String s;
int iY=0;
HDC hDC;
hOutput=CreateWindow(_T("frmOutput"),_T("String Class Minipulations In C++"),WS_OVERLAPPEDWINDOW,50,400,310,180,0,0,GetModuleHandle(0),0);
ShowWindow(hOutput,SW_SHOWNORMAL);
hDC=GetDC(hOutput);
s=_T("Frederick, Mary, James, Samuel, Edward, Richard, Michael, Joseph"); //Create comma delimited text string
iCt=s.ParseCount(_T(',')); //Count delimited substrings
ptrStrings = new String[iCt]; //Create array of String Pointers
s.Parse(ptrStrings,_T(',')); //Parse 's' based on comma delimiter
for(unsigned int i=0; i<iCt; i++)
{
ptrStrings[i].LTrim(); //Comment This Out To See Effect. //There are some spaces in delimited strings so remove them
TextOut(hDC,0,iY,ptrStrings[i].lpStr(),ptrStrings[i].LenStr()); //Output/Print array of strings one at a time
iY=iY+16;
}
delete [] ptrStrings;
ReleaseDC(hOutput,hDC);
}
void btnLeftRightMid_OnClick(void)
{
HWND hOutput;
String s1,s2;
HDC hDC;
hOutput=CreateWindow(_T("frmOutput"),_T("String Class Minipulations In C++"),WS_OVERLAPPEDWINDOW,800,100,325,250,0,0,GetModuleHandle(0),0);
ShowWindow(hOutput,SW_SHOWNORMAL);
hDC=GetDC(hOutput);
s1=_T("George Washington Carver");
s2=s1.Left(6);
TextOut(hDC,20,20,s1.lpStr(),s1.LenStr());
TextOut(hDC,20,40,s2.lpStr(),s2.LenStr());
s2=s1.Mid(8,10);
TextOut(hDC,20,60,s2.lpStr(),s2.LenStr());
s2=s1.Right(6);
TextOut(hDC,20,80,s2.lpStr(),s2.LenStr());
ReleaseDC(hOutput,hDC);
}
void btnInStr_OnClick(void)
{
String s1,s2,s3;
HWND hOutput;
int iOffset;
HDC hDC;
hOutput=CreateWindow(_T("frmOutput"),_T("String Class Minipulations In C++"),WS_OVERLAPPEDWINDOW,550,575,600,240,0,0,GetModuleHandle(0),0);
ShowWindow(hOutput,SW_SHOWNORMAL);
hDC=GetDC(hOutput);
s1=_T("InStr('Some Text') Locates The One Based Offset Of Where Something Is At.");
TextOut(hDC,0,0,s1.lpStr(),s1.LenStr());
s1=_T("C++ Can Be A Real Pain In The Butt To Learn!");
TextOut(hDC,0,16,s1.lpStr(),s1.LenStr());
iOffset=s1.InStr(_T("real"),false);
s2=iOffset;
s3 =_T("The Offset Of 'real' In The Above String Is ");
s3 = s3 + s2 + _T('.');
TextOut(hDC,0,32,s3.lpStr(),s3.LenStr());
s1=_T("Note In The Above Code We Used The Case Insensitive Version Of InStr().");
TextOut(hDC,0,48,s1.lpStr(),s1.LenStr());
s1=_T("We Also Learned How Easy It Is To Convert Numbers To Strigs!");
TextOut(hDC,0,64,s1.lpStr(),s1.LenStr());
s1=_T("Let's See If We Can Remove() 'Real' From Our 1st String!");
TextOut(hDC,0,80,s1.lpStr(),s1.LenStr());
s1=_T("C++ Can Be A Real Pain In The Butt To Learn!");
TextOut(hDC,0,96,s1.lpStr(),s1.LenStr());
s1.Remove(_T("Real"),true);
TextOut(hDC,0,112,s1.lpStr(),s1.LenStr());
s1=_T("Dieses Program funktioniert aber sehr gut, nicht wahr?");
TextOut(hDC,0,128,s1.lpStr(),s1.LenStr());
s2=22.0/7.0;
s1=_T("dblPi = ");
s1=s1+s2;
TextOut(hDC,0,144,s1.lpStr(),s1.LenStr());
s2.GetModulePath();
s1=_T("String::GetModulePath() Returns ") + s2;
TextOut(hDC,0,160,s1.lpStr(),s1.LenStr());
ReleaseDC(hOutput,hDC);
}
long fnWndProc_OnCommand(lpWndEventArgs Wea)
{
switch(LOWORD(Wea->wParam))
{
case IDC_CONCATENATION:
btnConcatenation_OnClick();
break;
case IDC_PARSE:
btnParse_OnClick();
break;
case IDC_LEFT_RIGHT_MID:
btnLeftRightMid_OnClick();
break;
case IDC_INSTR:
btnInStr_OnClick();
break;
}
return 0;
}
long fnWndProc_OnClose(lpWndEventArgs Wea)
{
DestroyWindow(Wea->hWnd);
PostQuitMessage(0);
return 0;
}
void AttachEventHandlers(void)
{
EventHandler[0].Code=WM_CREATE, EventHandler[0].fnPtr=fnWndProc_OnCreate;
EventHandler[1].Code=WM_COMMAND, EventHandler[1].fnPtr=fnWndProc_OnCommand;
EventHandler[2].Code=WM_CLOSE, EventHandler[2].fnPtr=fnWndProc_OnClose;
}
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam,LPARAM lParam)
{
WndEventArgs Wea;
for(unsigned int i=0; i<3; i++)
{
if(EventHandler[i].Code==msg)
{
Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(&Wea);
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
TCHAR szClassName[]=_T("StrDemo");
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
AttachEventHandlers();
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnWndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.style=CS_DBLCLKS;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hInstance=hIns;
wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW; wc.cbWndExtra=0;
wc.lpszMenuName=NULL; wc.cbClsExtra=0;
RegisterClassEx(&wc);
hWnd=CreateWindow(szClassName,_T("StrDemo"),WS_OVERLAPPEDWINDOW,20,20,350,210,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
|