Efitor Tutorial

This is a discussion on Efitor Tutorial within the Windows Programming forums, part of the Platform Specific Boards category; I'm following these winapi tutorials from this book and i'm on a chapter that shows making a screen editor which ...

  1. #1
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161

    Post Editor Tutorial

    I'm following these winapi tutorials from this book and i'm on a chapter that shows making a screen editor which looks to me like a basic notepad. I've typed the code up and it compiles but everytime i run it and try to type in the edit box the window closes.

    Code:
    #include <windows.h>
    
    
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
    			   WPARAM wParam, LPARAM lParam);
    
    HINSTANCE hInstGlobal;
    HWND hEdit;
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
    {
    	MSG        Msg;
    	HWND       hWnd;
    	WNDCLASSEX WndClsEx;
    
    	// Create the application window
    	WndClsEx.cbSize        = sizeof(WNDCLASSEX);
    	WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
    	WndClsEx.lpfnWndProc   = WndProcedure;
    	WndClsEx.cbClsExtra    = 0;
    	WndClsEx.cbWndExtra    = 0;
    	WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    	WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
    	WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	WndClsEx.lpszMenuName  = NULL;
    	WndClsEx.lpszClassName = "Kazalio Studios";
    	WndClsEx.hInstance     = hInstance;
    	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
    	// Register the application
    	RegisterClassEx(&WndClsEx);
    
    	// Create the window object
    	hWnd = CreateWindow("Kazalio Studios",
    			  "KaPad",
    			  WS_OVERLAPPEDWINDOW,
    			  CW_USEDEFAULT,
    			  CW_USEDEFAULT,
    			  CW_USEDEFAULT,
    			  CW_USEDEFAULT,
    			  NULL,
    			  NULL,
    			  hInstance,
    			  NULL);
    	
    	// Find out if the window was created
    	if( !hWnd ) // If the window was not created,
    		return 0; // stop the application
    
    	// Display the window to the user
    	ShowWindow(hWnd, SW_SHOWNORMAL);
    	UpdateWindow(hWnd);
    
    	// Decode and treat the messages
    	// as long as the application is running
    	while( GetMessage(&Msg, NULL, 0, 0) )
    	{
                 TranslateMessage(&Msg);
                 DispatchMessage(&Msg);
    	}
    
    	return Msg.wParam;
    }
    
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
    			   WPARAM wParam, LPARAM lParam)
    {
        switch(Msg)
        {
                   
        case WM_CREATE:
             HMENU hMenu;
             hMenu = CreateMenu();
             MENUITEMINFO mii;
             mii.cbSize = sizeof(MENUITEMINFO);
             mii.fMask  = MIIM_TYPE | MIIM_ID;
             mii.fType  = MFT_STRING;
             char *string;
             string = new char[40];
             mii.dwTypeData = string;
             mii.cch = lstrlen (string);
             
             HMENU hMenu2;
             hMenu2 = CreatePopupMenu ();
             
             lstrcpy (string, "&New");
             mii.dwTypeData = string;
             mii.cch = lstrlen (string);
             mii.wID = 1;
             InsertMenuItem (hMenu2, 1, TRUE, &mii);
             
             lstrcpy (string, "&Open");
             mii.dwTypeData = string;
             mii.cch = lstrlen (string);
             mii.wID = 2;
             InsertMenuItem (hMenu2, 2, TRUE, &mii);
             
             lstrcpy (string, "&Save");
             mii.dwTypeData = string;
             mii.cch = lstrlen (string);
             mii.wID = 2;
             InsertMenuItem (hMenu2, 3, TRUE, &mii);
             
             mii.fMask = MIIM_TYPE;
             mii.fType = MFT_SEPARATOR;
             InsertMenuItem (hMenu2, 4, TRUE, &mii);
             mii.fMask = MIIM_TYPE | MIIM_ID;
             mii.fType = MFT_STRING;
             lstrcpy (string, "&Exit");
             mii.dwTypeData = string;
             mii.wID = 4;
             InsertMenuItem (hMenu2, 5, TRUE, &mii);
             
             lstrcpy (string, "&File");
             mii.dwTypeData = string;
             mii.cch = lstrlen (string);
             mii.fMask = MIIM_TYPE | MIIM_SUBMENU;
             mii.hSubMenu = hMenu2;
             InsertMenuItem (hMenu, 0, FALSE, &mii);
             
             HMENU hMenu3;
             hMenu3 = CreatePopupMenu ();
             
             mii.fMask = MIIM_TYPE | MIIM_ID;
             mii.fType = MFT_STRING;
             lstrcpy (string, "&Using KaPad");
             mii.dwTypeData = string;
             mii.wID = 8;
             InsertMenuItem (hMenu3, 2, TRUE, &mii);
             
             
             
             mii.fMask = MIIM_TYPE;
             mii.fType = MFT_SEPARATOR;
             InsertMenuItem (hMenu3, 4, TRUE, &mii);
             mii.fMask = MIIM_TYPE | MIIM_ID;
             mii.fType = MFT_STRING;
             lstrcpy (string, "&Credits");
             mii.dwTypeData = string;
             mii.wID = 8;
             InsertMenuItem (hMenu3, 2, TRUE, &mii);
             
             lstrcpy (string, "&Help");
             mii.fMask = MIIM_TYPE | MIIM_SUBMENU;
             mii.hSubMenu = hMenu3;
             InsertMenuItem (hMenu, 3, FALSE, &mii);
             
             SetMenu (hWnd, hMenu);
             
             RECT rect;
             GetClientRect (hWnd, &rect);
             hEdit = CreateWindow ("EDIT", "hello",
                     WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
                     0, 0, rect.right, rect.bottom,
                     hWnd, (HMENU) 10,
                     hInstGlobal, NULL);
             return 0;
             
             
        case WM_SIZE:
             GetClientRect (hWnd, &rect);
             MoveWindow (hEdit, 0, 0, rect.right,
                         rect.bottom, TRUE);
             return 0;
        case WM_COMMAND:
             if (HIWORD(wParam) == 0)
             {
                  switch LOWORD(wParam)
                  {
                         case 1:
                              char *EditString;
                              EditString = new char[80];
                              lstrcpy (EditString, "");
                              SetWindowText (hEdit, EditString);
                              delete [] EditString;
                              return 0;
                         case 2:
                              HANDLE hFile;
                              hFile = CreateFile ("c:\\text.txt",
                                                  GENERIC_READ, 0,
                                                  NULL, OPEN_EXISTING,
                                                  0, NULL);
                              
                              DWORD Size;
                              Size = GetFileSize (hFile, NULL);
                              char *FileText;
                              FileText = new char[Size+1];
                              DWORD Readd;
                              ReadFile (hWnd, FileText, Size+1, &Readd, NULL);
                              CloseHandle (hFile);
                              SetWindowText (hEdit, FileText);
                              delete [] EditString;
                              return 0;
                         case 3:
                              hFile = CreateFile ("C:\\text.txt", GENERIC_WRITE,
                                                  0, NULL, CREATE_ALWAYS, NULL,
                                                  NULL);
                              FileText = new char[GetWindowTextLength(hEdit)+1];
                              GetWindowText (hEdit, FileText, GetWindowTextLength(
                                                                 hEdit)+1);
                              WriteFile (hFile, FileText,
                                        GetWindowTextLength(hEdit)+1, &Readd, NULL);
                              CloseHandle (hFile);
                              delete [] FileText;
                              return 0;
                         case 4:
                              PostQuitMessage(0);
                              return 0;
                              }
                              }
        // If the user wants to close the application
        case WM_DESTROY:
            // then close it
            PostQuitMessage(WM_QUIT);
            break;
        default:
            // Process the left-over messages
            return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
        // If something was not done, let it go
        return 0;
    }
    Can you help me solve this problem?
    Thanks in advance
    Last edited by algi; 04-02-2005 at 09:09 AM. Reason: spelling error
    I started out with nothing and I still have most of it left.

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Code:
    #include <windows.h>
    
    
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
    			   WPARAM wParam, LPARAM lParam);
    
    HINSTANCE hInstGlobal;
    HWND hEdit;
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
    {
    	MSG        Msg;
    	HWND       hWnd;
    	WNDCLASSEX WndClsEx;
    
    	// Create the application window
    	WndClsEx.cbSize        = sizeof(WNDCLASSEX);
    	WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
    	WndClsEx.lpfnWndProc   = WndProcedure;
    	WndClsEx.cbClsExtra    = 0;
    	WndClsEx.cbWndExtra    = 0;
    	WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    	WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
    	WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	WndClsEx.lpszMenuName  = NULL;
    	WndClsEx.lpszClassName = "Kazalio Studios";
    	WndClsEx.hInstance     = hInstance;
    	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
    	// Register the application
    	RegisterClassEx(&WndClsEx);
    
    	// Create the window object
    	hWnd = CreateWindow("Kazalio Studios",
    			  "KaPad",
    			  WS_OVERLAPPEDWINDOW,
    			  CW_USEDEFAULT,
    			  CW_USEDEFAULT,
    			  CW_USEDEFAULT,
    			  CW_USEDEFAULT,
    			  NULL,
    			  NULL,
    			  hInstance,
    			  NULL);
    	
    	// Find out if the window was created
    	if( !hWnd ) // If the window was not created,
    		return 0; // stop the application
    
    	// Display the window to the user
    	ShowWindow(hWnd, SW_SHOWNORMAL);
    	UpdateWindow(hWnd);
    
    	// Decode and treat the messages
    	// as long as the application is running
    	while( GetMessage(&Msg, NULL, 0, 0) )
    	{
                 TranslateMessage(&Msg);
                 DispatchMessage(&Msg);
    	}
    
    	return Msg.wParam;
    }
    
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
    			   WPARAM wParam, LPARAM lParam)
    {
        switch(Msg)
        {
                   
        case WM_CREATE:
             HMENU hMenu;
             hMenu = CreateMenu();
             MENUITEMINFO mii;
             mii.cbSize = sizeof(MENUITEMINFO);
             mii.fMask  = MIIM_TYPE | MIIM_ID;
             mii.fType  = MFT_STRING;
             char *string;
             string = new char[40];
             mii.dwTypeData = string;
             mii.cch = lstrlen (string);
             
             HMENU hMenu2;
             hMenu2 = CreatePopupMenu ();
             
             lstrcpy (string, "&New");
             mii.dwTypeData = string;
             mii.cch = lstrlen (string);
             mii.wID = 1;
             InsertMenuItem (hMenu2, 1, TRUE, &mii);
             
             lstrcpy (string, "&Open");
             mii.dwTypeData = string;
             mii.cch = lstrlen (string);
             mii.wID = 2;
             InsertMenuItem (hMenu2, 2, TRUE, &mii);
             
             lstrcpy (string, "&Save");
             mii.dwTypeData = string;
             mii.cch = lstrlen (string);
             mii.wID = 2;
             InsertMenuItem (hMenu2, 3, TRUE, &mii);
             
             mii.fMask = MIIM_TYPE;
             mii.fType = MFT_SEPARATOR;
             InsertMenuItem (hMenu2, 4, TRUE, &mii);
             mii.fMask = MIIM_TYPE | MIIM_ID;
             mii.fType = MFT_STRING;
             lstrcpy (string, "&Exit");
             mii.dwTypeData = string;
             mii.wID = 4;
             InsertMenuItem (hMenu2, 5, TRUE, &mii);
             
             lstrcpy (string, "&File");
             mii.dwTypeData = string;
             mii.cch = lstrlen (string);
             mii.fMask = MIIM_TYPE | MIIM_SUBMENU;
             mii.hSubMenu = hMenu2;
             InsertMenuItem (hMenu, 0, FALSE, &mii);
             
             HMENU hMenu3;
             hMenu3 = CreatePopupMenu ();
             
             mii.fMask = MIIM_TYPE | MIIM_ID;
             mii.fType = MFT_STRING;
             lstrcpy (string, "&Using KaPad");
             mii.dwTypeData = string;
             mii.wID = 8;
             InsertMenuItem (hMenu3, 2, TRUE, &mii);
             
             
             
             mii.fMask = MIIM_TYPE;
             mii.fType = MFT_SEPARATOR;
             InsertMenuItem (hMenu3, 4, TRUE, &mii);
             mii.fMask = MIIM_TYPE | MIIM_ID;
             mii.fType = MFT_STRING;
             lstrcpy (string, "&Credits");
             mii.dwTypeData = string;
             mii.wID = 8;
             InsertMenuItem (hMenu3, 2, TRUE, &mii);
             
             lstrcpy (string, "&Help");
             mii.fMask = MIIM_TYPE | MIIM_SUBMENU;
             mii.hSubMenu = hMenu3;
             InsertMenuItem (hMenu, 3, FALSE, &mii);
             
             SetMenu (hWnd, hMenu);
             
             RECT rect;
             GetClientRect (hWnd, &rect);
             hEdit = CreateWindow ("EDIT", "hello",
                     WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
                     0, 0, rect.right, rect.bottom,
                     hWnd, (HMENU) 10,
                     hInstGlobal, NULL);
             return 0;
             
             
        case WM_SIZE:
             GetClientRect (hWnd, &rect);
             MoveWindow (hEdit, 0, 0, rect.right,
                         rect.bottom, TRUE);
             return 0;
        case WM_COMMAND:
             if (HIWORD(wParam) == 0)
             {
                  switch LOWORD(wParam)
                  {
                         case 1:
                              char *EditString;
                              EditString = new char[80];
                              lstrcpy (EditString, "");
                              SetWindowText (hEdit, EditString);
                              delete [] EditString;
                              return 0;
                         case 2:
                              HANDLE hFile;
                              hFile = CreateFile ("c:\\text.txt",
                                                  GENERIC_READ, 0,
                                                  NULL, OPEN_EXISTING,
                                                  0, NULL);
                              
                              DWORD Size;
                              Size = GetFileSize (hFile, NULL);
                              char *FileText;
                              FileText = new char[Size+1];
                              DWORD Readd;
                              ReadFile (hWnd, FileText, Size+1, &Readd, NULL);
                              CloseHandle (hFile);
                              SetWindowText (hEdit, FileText);
                              delete [] EditString;
                              return 0;
                         case 3:
                              hFile = CreateFile ("C:\\text.txt", GENERIC_WRITE,
                                                  0, NULL, CREATE_ALWAYS, NULL,
                                                  NULL);
                              FileText = new char[GetWindowTextLength(hEdit)+1];
                              GetWindowText (hEdit, FileText, GetWindowTextLength(
                                                                 hEdit)+1);
                              WriteFile (hFile, FileText,
                                        GetWindowTextLength(hEdit)+1, &Readd, NULL);
                              CloseHandle (hFile);
                              delete [] FileText;
                              return 0;
                         case 4:
                              PostQuitMessage(0);
                              return 0;
                              }
            }
            break;
            
        // If the user wants to close the application
        case WM_DESTROY:
            // then close it
            PostQuitMessage(WM_QUIT);
            break;
        default:
            // Process the left-over messages
            return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
        // If something was not done, let it go
        return 0;
    }

  3. #3
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    thats resolved the closing issue, but there is a problem with open and save. If i save something like "how are you" and then open it 3 random characters appear like @gh. Does anybody know where i have gone wrong.
    I started out with nothing and I still have most of it left.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21