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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
#include <windows.h>
#define UNICODE
#include <tchar.h>
#include "WinTypes.h"
#define BUFFER_LENGTH 40
#define IDC_CELL1 1500
#define IDC_BUTTON 1510
long fnCellProc_OnCreate(lpWndEventArgs Wea)
{
int x,cxChar,cyChar;
TEXTMETRIC tm;
TCHAR* pBuffer;
HANDLE hHeap;
HDC hdc;
hHeap=GetProcessHeap();
pBuffer=(TCHAR*)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,40*sizeof(TCHAR));
for(x=0;x<BUFFER_LENGTH;x++)
pBuffer[x]=_T(' ');
SetWindowLong(Wea->hWnd,0,(long)pBuffer); //0 -3 pBuffer for text typed
hdc=GetDC(Wea->hWnd); //4 -7 cxChar - width of char
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT)); //8 -11 cyChar - height of char
GetTextMetrics(hdc,&tm); //12-15 cxClient - width of client area
cxChar=tm.tmAveCharWidth; //16-19 cyClient - height of client area
cyChar=tm.tmHeight; //20-23 count of chars that fit in cxClient
SetWindowLong(Wea->hWnd,4,cxChar); //24-27 caret position in cxClient (zero based)
SetWindowLong(Wea->hWnd,8,cyChar);
ReleaseDC(Wea->hWnd,hdc);
return 0;
}
long fnCellProc_OnSetFocus(lpWndEventArgs Wea)
{
int xCaret,cxChar,cyChar;
xCaret=GetWindowLong(Wea->hWnd,24);
cxChar=GetWindowLong(Wea->hWnd,4);
cyChar=GetWindowLong(Wea->hWnd,8);
CreateCaret(Wea->hWnd,NULL,cxChar,cyChar);
SetCaretPos(xCaret*cxChar,0);
ShowCaret(Wea->hWnd);
return 0;
}
long fnCellProc_OnLButtonDown(lpWndEventArgs Wea)
{
SetFocus(Wea->hWnd);
return 0;
}
long fnCellProc_OnSize(lpWndEventArgs Wea)
{
int cxClient,cyClient,cxBuffer,cxChar,xCaret;
cxChar=GetWindowLong(Wea->hWnd,4);
cxClient=LOWORD(Wea->lParam);
cyClient=HIWORD(Wea->lParam);
SetWindowLong(Wea->hWnd,12,cxClient);
SetWindowLong(Wea->hWnd,16,cyClient);
cxBuffer=cxClient/cxChar;
SetWindowLong(Wea->hWnd,20,cxBuffer);
xCaret=0;
SetWindowLong(Wea->hWnd,24,xCaret);
if(Wea->hWnd==GetFocus())
SetCaretPos(xCaret*cxChar,0);
return 0;
}
long fnCellProc_OnPaint(lpWndEventArgs Wea)
{
TCHAR* pBuffer=NULL;
PAINTSTRUCT ps;
int cxBuffer;
HDC hdc;
RECT rc;
pBuffer=(TCHAR*)GetWindowLong(Wea->hWnd,0);
cxBuffer=GetWindowLong(Wea->hWnd,20);
hdc=BeginPaint(Wea->hWnd,&ps);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
rc.bottom=18, rc.top=0, rc.left=0, rc.right=320;
DrawText(hdc,pBuffer,-1,&rc,DT_SINGLELINE);
EndPaint(Wea->hWnd,&ps);
return 0;
}
long fnCellProc_OnKeyDown(lpWndEventArgs Wea)
{
int cxBuffer,cxChar,xCaret;
TCHAR* pBuffer;
RECT rc;
HDC hdc;
pBuffer=(TCHAR*)GetWindowLong(Wea->hWnd,0);
cxBuffer=GetWindowLong(Wea->hWnd,20);
cxChar=GetWindowLong(Wea->hWnd,4);
xCaret=GetWindowLong(Wea->hWnd,24);
switch (Wea->wParam)
{
case VK_LEFT:
xCaret=max(xCaret-1,0);
break;
case VK_RIGHT:
xCaret=min(xCaret+1,cxBuffer-1);
break;
case VK_DELETE:
{
int x;
for(x=xCaret;x<cxBuffer-1;x++)
*(pBuffer+x)=*(pBuffer+x+1);
*(pBuffer+cxBuffer-1)=TEXT(' ');
HideCaret(Wea->hWnd);
hdc=GetDC(Wea->hWnd);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
rc.bottom=18, rc.top=0, rc.left=0, rc.right=320;
DrawText(hdc,pBuffer,-1,&rc,DT_SINGLELINE);
ShowCaret(Wea->hWnd);
ReleaseDC(Wea->hWnd,hdc);
break;
}
}
SetCaretPos(xCaret*cxChar,0);
SetWindowLong(Wea->hWnd,24,xCaret);
return 0;
}
long fnCellProc_OnChar(lpWndEventArgs Wea)
{
int cxChar,cxBuffer,xCaret;
TCHAR *pBuffer;
RECT rc;
HDC hdc;
pBuffer=(TCHAR*)GetWindowLong(Wea->hWnd,0);
cxChar=GetWindowLong(Wea->hWnd,4);
cxBuffer=GetWindowLong(Wea->hWnd,20);
xCaret=GetWindowLong(Wea->hWnd,24);
switch(Wea->wParam)
{
case TEXT('\b'): // backspace
if(xCaret==0)
SendMessage(Wea->hWnd,WM_KEYDOWN,VK_DELETE,1L);
else
{
xCaret-- ;
SendMessage(Wea->hWnd,WM_KEYDOWN,VK_DELETE,1L);
}
break ;
default: // character codes
{
int i=0;
HideCaret(Wea->hWnd);
for(i=BUFFER_LENGTH-2;i>xCaret;i--)
pBuffer[i]=pBuffer[i-1];
pBuffer[BUFFER_LENGTH-1]=TEXT('\0');
*(pBuffer+xCaret)=(TCHAR)Wea->wParam;
hdc=GetDC(Wea->hWnd);
SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));
rc.bottom=18, rc.top=0, rc.left=0, rc.right=320;
DrawText(hdc,pBuffer,-1,&rc,DT_SINGLELINE);
ShowCaret(Wea->hWnd);
ReleaseDC(Wea->hWnd,hdc);
if(xCaret+1<cxBuffer)
xCaret++;
break;
}
}
SetCaretPos(xCaret*cxChar,0);
SetWindowLong(Wea->hWnd,24,xCaret);
return 0;
}
long fnCellProc_OnKillFocus(lpWndEventArgs Wea)
{
HideCaret(Wea->hWnd);
DestroyCaret();
return 0;
}
long fnCellProc_OnDestroy(lpWndEventArgs Wea)
{
TCHAR* pBuffer;
pBuffer=(TCHAR*)GetWindowLong(Wea->hWnd,0);
HeapFree(GetProcessHeap(),0,pBuffer);
PostQuitMessage(0);
return 0;
}
LRESULT CALLBACK fnCellProc(HWND hWnd, unsigned int iMsg, WPARAM wParam, LPARAM lParam)
{
WndEventArgs Wea;
for(unsigned int i=0; i<dim(EventHandler); i++)
{
if(EventHandler[i].Code==iMsg)
{
Wea.hWnd=hWnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(&Wea);
}
}
return (DefWindowProc(hWnd, iMsg, wParam, lParam));
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
WNDCLASSEX wc;
TCHAR szClassName[]=TEXT("Cell");
HWND hCell,hButton;
wc.cbSize=sizeof(wc);
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=fnCellProc;
wc.cbClsExtra=0;
wc.cbWndExtra=28;
wc.hInstance=((LPCREATESTRUCT)lParam)->hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW) ;
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=szClassName;
wc.hIconSm=0;
RegisterClassEx(&wc);
hCell=CreateWindowEx(WS_EX_CLIENTEDGE,szClassName,_T(""),WS_CHILD|WS_VISIBLE|WS_BORDER,10,10,320,22,hwnd,(HMENU)IDC_CELL1,wc.hInstance,0);
hButton=CreateWindow(_T("button"),_T("Get Text"),WS_CHILD|WS_VISIBLE,135,45,80,25,hwnd,(HMENU)IDC_BUTTON,wc.hInstance,0);
SetWindowLong(hwnd,0,(long)hCell);
SetWindowLong(hCell,28,(long)40);
return 0;
}
case WM_COMMAND:
if(LOWORD(wParam)==IDC_BUTTON && HIWORD(wParam)==BN_CLICKED)
{
TCHAR* pBuffer=(TCHAR*)GetWindowLong(GetDlgItem(hwnd,IDC_CELL1),0);
MessageBox(hwnd,pBuffer,_T("Here Is What Is In The Buffer"),MB_OK);
}
return 0;
case WM_DESTROY:
DestroyWindow(GetDlgItem(hwnd,IDC_CELL1));
PostQuitMessage (0);
return 0;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
TCHAR szCaption[]=_T("Home-Made Edit Control");
TCHAR szClassName[]=_T("Main");
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
wc.lpszClassName=szClassName, wc.lpfnWndProc=WndProc;
wc.hInstance=hIns, wc.style=0;
wc.cbSize=sizeof (WNDCLASSEX), wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION), wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.lpszMenuName=NULL, wc.cbClsExtra=0;
wc.cbWndExtra=4, wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW;
RegisterClassEx(&wc);
hWnd=CreateWindow(szClassName,szCaption,WS_OVERLAPPEDWINDOW,375,300,350,120,0,0,hIns,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
|