win32 api text editor errors

Apr 14, 2015 at 5:41pm
i need to make a text editor this is my code but there are some errors like when i re-size my window all the text is erased also scroll bar does not move when my words go further from client area please help
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#define WINVER 0x0500
#define _WIN32_WINNT 0x0500 // for Mouse Wheel support
#include <windows.h>
#include "resource.h"

#define BUFFER(x,y) *(pBuffer + y * cxBuffer + x)

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

TCHAR szAppName[] = TEXT ("MenuDemo") ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

wndclass.style = NULL ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, szAppName) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, TEXT ("SCRIPT"),
WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static DWORD dwCharSet = DEFAULT_CHARSET ;
static int cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer,
xCaret, yCaret ;
static int iDeltaPerLine, iAccumDelta ; // for mouse wheel logic
static TCHAR * pBuffer = NULL ;
HMENU hMenu ;
HDC hdc ;
int x, y, i, iVertPos, iHorzPos ;
PAINTSTRUCT ps ;
SCROLLINFO si ;
TEXTMETRIC tm ;
ULONG ulScrollLines ; // for mouse wheel logic

switch (message)
{
case WM_COMMAND:
hMenu = GetMenu (hwnd) ;

switch (LOWORD (wParam))
{
case IDM_FILE_NEW:
case IDM_FILE_OPEN:
case IDM_FILE_SAVE:
case IDM_FILE_SAVE_AS:
MessageBeep (0) ;
return 0 ;

case IDM_APP_EXIT:
SendMessage (hwnd, WM_CLOSE, 0, 0) ;
return 0 ;

case IDM_EDIT_UNDO:
case IDM_EDIT_CUT:
case IDM_EDIT_COPY:
case IDM_EDIT_PASTE:
case IDM_EDIT_CLEAR:
MessageBeep (0) ;
return 0 ;

case IDM_APP_HELP:
MessageBox (hwnd, TEXT ("Help not yet implemented!"),
szAppName, MB_ICONEXCLAMATION | MB_OK) ;
return 0 ;

case IDM_APP_ABOUT:
MessageBox (hwnd, TEXT ("Menu Demonstration Program\n")
TEXT ("(c) Charles Petzold, 1998"),
szAppName, MB_ICONINFORMATION | MB_OK) ;
return 0 ;
}
break ;

case WM_INPUTLANGCHANGE:
dwCharSet = wParam ;

case WM_CREATE:
hdc = GetDC (hwnd) ;
SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;

GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight ;

DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
ReleaseDC (hwnd, hdc) ;
// fall through
case WM_SIZE:
// obtain window size in pixels

if (message == WM_SIZE)
{
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
}

cxBuffer = 1000;
cyBuffer = 1000;

// allocate memory for buffer and clear it

if (pBuffer != NULL)
free (pBuffer) ;

pBuffer = (TCHAR *) malloc (cxBuffer * cyBuffer * sizeof (TCHAR)) ;

for (y = 0 ; y < cyBuffer ; y++)
for (x = 0 ; x < cxBuffer ; x++)
BUFFER(x,y) = ' ' ;

// set caret to upper left corner

xCaret = 0 ;
yCaret = 0 ;

if (hwnd == GetFocus ())
SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;

InvalidateRect (hwnd, NULL, TRUE) ;
si.cbSize = sizeof (si) ;
si.fMask = SIF_RANGE | SIF_PAGE ;
si.nMin = 0 ;
si.nMax = cyBuffer;
si.nPage = cyClient / cyChar ;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;

// Set horizontal scroll bar range and page size

si.cbSize = sizeof (si) ;
si.fMask = SIF_RANGE | SIF_PAGE ;
si.nMin = 0 ;
si.nMax = cxBuffer ;
si.nPage = cxClient / cxChar ;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
return 0 ;

case WM_SETFOCUS:
// create and show the caret

CreateCaret (hwnd, NULL, cxChar, cyChar) ;
SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
ShowCaret (hwnd) ;
return 0 ;

case WM_KILLFOCUS:
// hide and destroy the caret

HideCaret (hwnd) ;
DestroyCaret () ;
return 0 ;
Apr 14, 2015 at 5:42pm
case WM_VSCROLL:
// Get all the vertical scroll bar information

si.cbSize = sizeof (si) ;
si.fMask = SIF_ALL ;
GetScrollInfo (hwnd, SB_VERT, &si) ;

// Save the position for comparison later on

iVertPos = si.nPos ;

switch (LOWORD (wParam))
{
case SB_TOP:
si.nPos = si.nMin ;
break ;

case SB_BOTTOM:
si.nPos = si.nMax ;
break ;

case SB_LINEUP:
si.nPos -= 1 ;
break ;

case SB_LINEDOWN:
si.nPos += 1 ;
break ;

case SB_PAGEUP:
si.nPos -= si.nPage ;
break ;

case SB_PAGEDOWN:
si.nPos += si.nPage ;
break ;

case SB_THUMBTRACK:
si.nPos = si.nTrackPos ;
break ;

default:
break ;
}
// Set the position and then retrieve it. Due to adjustments
// by Windows it might not be the same as the value set.

si.fMask = SIF_POS ;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
GetScrollInfo (hwnd, SB_VERT, &si) ;

// If the position has changed, scroll the window and update it

if (si.nPos != iVertPos)
{
ScrollWindow (hwnd, 0, cyChar * (iVertPos - si.nPos),
NULL, NULL) ;
UpdateWindow (hwnd) ;
}
return 0 ;

case WM_HSCROLL:
// Get all the vertical scroll bar information

si.cbSize = sizeof (si) ;
si.fMask = SIF_ALL ;

// Save the position for comparison later on

GetScrollInfo (hwnd, SB_HORZ, &si) ;
iHorzPos = si.nPos ;

switch (LOWORD (wParam))
{
case SB_LINELEFT:
si.nPos -= 1 ;
break ;

case SB_LINERIGHT:
si.nPos += 1 ;
break ;

case SB_PAGELEFT:
si.nPos -= si.nPage ;
break ;

case SB_PAGERIGHT:
si.nPos += si.nPage ;
break ;

case SB_THUMBPOSITION:
si.nPos = si.nTrackPos ;
break ;

default:
break ;
}
// Set the position and then retrieve it. Due to adjustments
// by Windows it might not be the same as the value set.

si.fMask = SIF_POS ;
SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
GetScrollInfo (hwnd, SB_HORZ, &si) ;

// If the position has changed, scroll the window

if (si.nPos != iHorzPos)
{
ScrollWindow (hwnd, cxChar * (iHorzPos - si.nPos), 0,
NULL, NULL) ;
}
return 0 ;

case WM_KEYDOWN:
switch (wParam)
{
case VK_HOME:
xCaret = 0 ;
break ;

case VK_END:
xCaret = cxBuffer - 1 ;
break ;

case VK_PRIOR:
yCaret = 0 ;
break ;

case VK_NEXT:
yCaret = cyBuffer - 1 ;
break ;

case VK_LEFT:
if(xCaret==cxClient / cxChar)
{
xCaret = max (xCaret - 1, 0) | SendMessage (hwnd, WM_HSCROLL, SB_PAGEUP, 0) ;
}
xCaret = max (xCaret - 1, 0) ;
break ;

case VK_RIGHT:
if(xCaret==cxClient / cxChar)
{
xCaret = min (xCaret + 1, cxBuffer - 1) | SendMessage (hwnd, WM_HSCROLL, SB_PAGEDOWN, 0) ;
}
xCaret = min (xCaret + 1, cxBuffer - 1) ;
break ;

case VK_UP:
if(yCaret==cyClient / cyChar)
{
yCaret = max (yCaret - 1, 0) | SendMessage (hwnd, WM_VSCROLL, SB_LINEUP, 0) ;
}
yCaret = max (yCaret - 1, 0) ;
break ;

case VK_DOWN:
if(yCaret==cyClient / cyChar)
{
yCaret = min (yCaret + 1, cyBuffer - 1) | SendMessage (hwnd, WM_VSCROLL, SB_LINEDOWN, 0) ;
}
yCaret = min (yCaret + 1, cyBuffer - 1) ;
break ;

case VK_DELETE:
for (x = xCaret ; x < cxBuffer - 1 ; x++)
BUFFER (x, yCaret) = BUFFER (x + 1, yCaret) ;

BUFFER (cxBuffer - 1, yCaret) = ' ' ;

HideCaret (hwnd) ;
hdc = GetDC (hwnd) ;

SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;

TextOut (hdc, xCaret * cxChar, yCaret * cyChar,
& BUFFER (xCaret, yCaret),
cxBuffer - xCaret) ;

DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
ReleaseDC (hwnd, hdc) ;
ShowCaret (hwnd) ;
break ;
}
SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
return 0 ;
Apr 14, 2015 at 5:42pm
case WM_CHAR:
for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
{
switch (wParam)
{
case '\b': // backspace
if (xCaret > 0)
{
xCaret-- ;
SendMessage (hwnd, WM_KEYDOWN, VK_DELETE, 1) ;
}
break ;

case '\t': // tab
do
{
SendMessage (hwnd, WM_CHAR, ' ', 1) ;
}
while (xCaret % 8 != 0) ;
break ;

case '\n': // line feed
if (++yCaret == cyBuffer)
yCaret = 0 ;
break ;

case '\r': // carriage return
xCaret = 0 ;

if (++yCaret == cyBuffer)
yCaret = 0 ;
break ;

case '\x1B': // escape
for (y = 0 ; y < cyBuffer ; y++)
for (x = 0 ; x < cxBuffer ; x++)
BUFFER (x, y) = ' ' ;

xCaret = 0 ;
yCaret = 0 ;

InvalidateRect (hwnd, NULL, FALSE) ;
break ;

default: // character codes
BUFFER (xCaret, yCaret) = (TCHAR) wParam ;

HideCaret (hwnd) ;
hdc = GetDC (hwnd) ;

SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;

TextOut (hdc, xCaret * cxChar, yCaret * cyChar,
& BUFFER (xCaret, yCaret), 1) ;

DeleteObject (
SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
ReleaseDC (hwnd, hdc) ;
ShowCaret (hwnd) ;

if (++xCaret == cxBuffer)
{
xCaret = 0 ;

if (++yCaret == cyBuffer)
yCaret = 0 ;
}
break ;
}
}

SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
return 0 ;

case WM_MOUSEWHEEL:
if (iDeltaPerLine == 0)
break ;

iAccumDelta += (short) HIWORD (wParam) ; // 120 or -120

while (iAccumDelta >= iDeltaPerLine)
{
SendMessage (hwnd, WM_VSCROLL, SB_LINEUP, 0) ;
iAccumDelta -= iDeltaPerLine ;
}

while (iAccumDelta <= -iDeltaPerLine)
{
SendMessage (hwnd, WM_VSCROLL, SB_LINEDOWN, 0) ;
iAccumDelta += iDeltaPerLine ;
}

return 0 ;

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;

SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;

for (y = 0 ; y < cyBuffer ; y++)
TextOut (hdc, 0, y * cyChar, & BUFFER(0,y), cxBuffer) ;


EndPaint (hwnd, &ps) ;
return 0 ;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
Apr 14, 2015 at 5:43pm
this is my whole code
Apr 14, 2015 at 5:59pm
Thats way too much code Man. Learn how to debug your program to find out whats going wrong.

Also, edit your post and put all of your code between [code]code tags[/code]

http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.