Thread: Help winth menu commands!

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    Help winth menu commands!

    How do i make the file new command work?
    How do I make the edit commands work?
    How can I add a print function?

    Here's the source code

    Code:
    /* main.c */
    
    #include <windows.h>
    
    #pragma hdrstop
    
    #include "Main.h"
    
    static char g_szClassName[] = "MyWindowClass";
    static HINSTANCE g_hInst = NULL;
    
    #define IDC_MAIN_TEXT   1001
    
    BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
    {
       HANDLE hFile;
       BOOL bSuccess = FALSE;
    
       hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
          OPEN_EXISTING, 0, 0);
       if(hFile != INVALID_HANDLE_VALUE)
       {
          DWORD dwFileSize;
          dwFileSize = GetFileSize(hFile, NULL);
          if(dwFileSize != 0xFFFFFFFF)
          {
             LPSTR pszFileText;
             pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
             if(pszFileText != NULL)
             {
                DWORD dwRead;
                if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
                {
                   pszFileText[dwFileSize] = 0; // Null terminator
                   if(SetWindowText(hEdit, pszFileText))
                      bSuccess = TRUE; // It worked!
                }
                GlobalFree(pszFileText);
             }
          }
          CloseHandle(hFile);
       }
       return bSuccess;
    }
    
    BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
    {
       HANDLE hFile;
       BOOL bSuccess = FALSE;
    
       hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,
          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
       if(hFile != INVALID_HANDLE_VALUE)
       {
          DWORD dwTextLength;
          dwTextLength = GetWindowTextLength(hEdit);
          if(dwTextLength > 0)// No need to bother if there's no text.
          {
             LPSTR pszText;
             pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
             if(pszText != NULL)
             {
                if(GetWindowText(hEdit, pszText, dwTextLength + 1))
                {
                   DWORD dwWritten;
                   if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                      bSuccess = TRUE;
                }
                GlobalFree(pszText);
             }
          }
          CloseHandle(hFile);
       }
       return bSuccess;
    }
    
    BOOL DoFileOpenSave(HWND hwnd, BOOL bSave)
    {
       OPENFILENAME ofn;
       char szFileName[MAX_PATH];
    
       ZeroMemory(&ofn, sizeof(ofn));
       szFileName[0] = 0;
    
       ofn.lStructSize = sizeof(ofn);
       ofn.hwndOwner = hwnd;
       ofn.lpstrFilter = "AJF My Own Files (*.ajf)\0*.ajf\0Text Files (*.txt)\0*.txt\0HTML Web Docs (*.html)\0*.html\0HTM Old Web Docs (*.htm)\0*.htm\0MHT Web Archive (*.mht)\0*.mht\0SHTML Super Web Docs (*.shtml)\0*.shtml\0PHP Web Docs (*.php)\0*.php\0PL Perl Script (*.pl)\0*.pl\0CGI Common Gateway Interface (*.cgi)\0*.cgi\0JS Java Script (*.js)\0*.js\0CSS Cascadeing Style Sheet (*.css)\0*.css\0JSS Java Style Sheet (*.jss)\0*.jss\0AS Action Script File For Flash (*.as)\0*.as\0ASP Microsoft Active Server Pages (*.asp)\0*.asp\0PY Python Script (*.py)\0*.py\0DAT Database File (*.dat)\0*.dat\0All Files (*.*)\0*.*\0\0";
       ofn.lpstrFile = szFileName;
       ofn.nMaxFile = MAX_PATH;
       ofn.lpstrDefExt = "txt";
    
         if(bSave)
       {
          ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
             OFN_OVERWRITEPROMPT;
             
          if(GetSaveFileName(&ofn))
          {
             if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
             {
                MessageBox(hwnd, "Save file failed.", "Error",
                   MB_OK | MB_ICONEXCLAMATION);
                return FALSE;
             }
          }
       }
       else
       {
          ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
          if(GetOpenFileName(&ofn))
          {
             if(!LoadFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
             {
                MessageBox(hwnd, "Load of file failed.", "Error",
                   MB_OK | MB_ICONEXCLAMATION);
                return FALSE;
             }
          }
       }
       return TRUE;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
       switch(Message)
       {
          case WM_CREATE:
             CreateWindow("EDIT", "",
                WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE |
                   ES_WANTRETURN,
                CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL);
    
             SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT,
                (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
          break;
          case WM_SIZE:
             if(wParam != SIZE_MINIMIZED)
                MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam),
                   HIWORD(lParam), TRUE);
          break;
          case WM_SETFOCUS:
             SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT));
          break;
          case WM_COMMAND:
             switch(LOWORD(wParam))
             {
                case CM_FILE_NEW:
                   DoFileOpenSave(hwnd, FALSE);
                break;
                case CM_FILE_OPEN:
                   DoFileOpenSave(hwnd, FALSE);
                break;
                case CM_FILE_SAVEAS:
                   DoFileOpenSave(hwnd, TRUE);
                break;
                case CM_FILEPRINT:
                break;
                case CM_FILE_EXIT:
                   PostMessage(hwnd, WM_CLOSE, 0, 0);
                break;
                case CM_EDIT_UNDO:
                break;
                case CM_EDIT_CUT:
                break;
                case CM_EDIT_COPY:
                break;
                case CM_EDIT_PASTE:
                break;
                case CM_ABOUT:
                   MessageBox (NULL, "File Editor for Windows !\n Made For You By Andrew Ferguson" , "About...", 0);
             }
          break;
          case WM_CLOSE:
             DestroyWindow(hwnd);
          break;
          case WM_DESTROY:
             PostQuitMessage(0);
          break;
          default:
             return DefWindowProc(hwnd, Message, wParam, lParam);
       }
       return 0;
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
       LPSTR lpCmdLine, int nCmdShow)
    {
       WNDCLASSEX WndClass;
       HWND hwnd;
       MSG Msg;
    
       g_hInst = hInstance;
    
       WndClass.cbSize        = sizeof(WNDCLASSEX);
       WndClass.style         = 0;
       WndClass.lpfnWndProc   = WndProc;
       WndClass.cbClsExtra    = 0;
       WndClass.cbWndExtra    = 0;
       WndClass.hInstance     = g_hInst;
       WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
       WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
       WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
       WndClass.lpszMenuName  = "MAINMENU";
       WndClass.lpszClassName = g_szClassName;
       WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
       if(!RegisterClassEx(&WndClass))
       {
          MessageBox(0, "Window Registration Failed!", "Error!",
             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
          return 0;
       }
    
       hwnd = CreateWindowEx(
          WS_EX_CLIENTEDGE,
          g_szClassName,
          "AJF Editor",
          WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
          NULL, NULL, g_hInst, NULL);
    
       if(hwnd == NULL)
       {
          MessageBox(0, "Window Creation Failed!", "Error!",
             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
          return 0;
       }
    
       ShowWindow(hwnd, nCmdShow);
       UpdateWindow(hwnd);
    
       while(GetMessage(&Msg, NULL, 0, 0))
       {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
       }
       return Msg.wParam;
    }
    
    
    
    
    
    
    /* main.h*/
    
    
    #define CM_FILE_NEW 	9073
    #define CM_FILE_SAVEAS	9072
    #define CM_FILE_EXIT	9071
    #define CM_FILE_OPEN	9070
    #define CM_FILEPRINT	9069
    #define CM_EDIT_UNDO    9068
    #define CM_EDIT_REDO    9067
    #define CM_EDIT_CUT     9066
    #define CM_EDIT_COPY    9065
    #define CM_EDIT_PASTE   9064
    #define CM_ABOUT        9063
    
    
    
    
    
    /* Rsrc.rc*/
    
    
    #include "Main.h"
    
    
    MAINMENU MENU 
    {
     POPUP "&File"
     {
      MENUITEM "&New", CM_FILE_NEW
      MENUITEM "&Open...", CM_FILE_OPEN
      MENUITEM "Save &As...", CM_FILE_SAVEAS
      MENUITEM SEPARATOR
      MENUITEM "E&xit", CM_FILE_EXIT
     }
    
     POPUP "&Edit"
     {
      MENUITEM "&Undo\tCtrl+Z", CM_EDIT_UNDO
      MENUITEM SEPARATOR
      MENUITEM "Cu&t\tCtrl+X", CM_EDIT_CUT
      MENUITEM "&Copy\tCtrl+C", CM_EDIT_COPY
      MENUITEM "&Paste\tCtrl+V", CM_EDIT_PASTE
     }
    
     POPUP "&Help"
     {
      MENUITEM "&About", CM_ABOUT
     }
    
    }

    &#91;code]&#91;/code]tagged by Salem
    Andrew Ferguson!

  2. #2
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    whats up? hehehe, you dont know that much, you got that the same way i made DarkType. hehehe. you slapped a few source codes together. HAHAHAHAHA!

    lol. but dont worry. i still think that looks good.

    im learning to actually program in windows and stuff andrew.

    btw

    congrats on joining the board.
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Now that the laughter has subsided...

    Read the FAQ regarding code tags and not posting a gazillion lines of source code to the Board.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    you know so your code looks like this:
    Code:
    this is my code
    the tags are in the menu with the # sighn.

    or type in [ CODE ] [/ CODE ] without the spaces.
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM