Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.48 KB | None | 0 0
  1. // word processor.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "word processor.h"
  6. #include <Windows.h>
  7. #include <Commdlg.h>
  8. #include <iostream>
  9. #include <fstream>
  10. #include "Windowsx.h"
  11.  
  12.  
  13. #define MAX_LOADSTRING 100
  14.  
  15. // Global Variables:
  16. HINSTANCE hInst;                                // current instance
  17. TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
  18. TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
  19.  
  20. // Forward declarations of functions included in this code module:
  21. ATOM                MyRegisterClass(HINSTANCE hInstance);
  22. BOOL                InitInstance(HINSTANCE, int);
  23. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  24. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  25.  
  26. RECT coords;
  27. HWND textboxhandle;
  28. OPENFILENAME ofn;       // common dialog box structure
  29. wchar_t szFile[260];       // buffer for file name
  30. HWND hwnd;              // owner window
  31. HANDLE hf;              // file handle
  32.  
  33. int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
  34.                      _In_opt_ HINSTANCE hPrevInstance,
  35.                      _In_ LPTSTR    lpCmdLine,
  36.                      _In_ int       nCmdShow)
  37. {
  38.    
  39.     // Initialize OPENFILENAME
  40.     ZeroMemory(&ofn, sizeof(ofn));
  41.     ofn.lStructSize = sizeof(ofn);
  42.     ofn.hwndOwner = hwnd;
  43.     ofn.lpstrFile = szFile;
  44.     // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
  45.     // use the contents of szFile to initialize itself.
  46.     ofn.lpstrFile[0] = '\0';
  47.     ofn.nMaxFile = sizeof(szFile);
  48.     ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0";
  49.     ofn.nFilterIndex = 1;
  50.     ofn.lpstrFileTitle = NULL;
  51.     ofn.nMaxFileTitle = 0;
  52.     ofn.lpstrInitialDir = NULL;
  53.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  54.    
  55.     UNREFERENCED_PARAMETER(hPrevInstance);
  56.     UNREFERENCED_PARAMETER(lpCmdLine);
  57.  
  58.     // TODO: Place code here.
  59.     MSG msg;
  60.     HACCEL hAccelTable;
  61.  
  62.     // Initialize global strings
  63.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  64.     LoadString(hInstance, IDC_WORDPROCESSOR, szWindowClass, MAX_LOADSTRING);
  65.     MyRegisterClass(hInstance);
  66.  
  67.     // Perform application initialization:
  68.     if (!InitInstance (hInstance, nCmdShow))
  69.     {
  70.         return FALSE;
  71.     }
  72.  
  73.     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WORDPROCESSOR));
  74.  
  75.     // Main message loop:
  76.     while (GetMessage(&msg, NULL, 0, 0))
  77.     {
  78.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  79.         {
  80.             TranslateMessage(&msg);
  81.             DispatchMessage(&msg);
  82.         }
  83.     }
  84.  
  85.     return (int) msg.wParam;
  86. }
  87.  
  88.  
  89.  
  90. //
  91. //  FUNCTION: MyRegisterClass()
  92. //
  93. //  PURPOSE: Registers the window class.
  94. //
  95. ATOM MyRegisterClass(HINSTANCE hInstance)
  96. {
  97.     WNDCLASSEX wcex;
  98.  
  99.     wcex.cbSize = sizeof(WNDCLASSEX);
  100.  
  101.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  102.     wcex.lpfnWndProc    = WndProc;
  103.     wcex.cbClsExtra     = 0;
  104.     wcex.cbWndExtra     = 0;
  105.     wcex.hInstance      = hInstance;
  106.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WORDPROCESSOR));
  107.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  108.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  109.     wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_WORDPROCESSOR);
  110.     wcex.lpszClassName  = szWindowClass;
  111.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  112.  
  113.     return RegisterClassEx(&wcex);
  114. }
  115.  
  116. //
  117. //   FUNCTION: InitInstance(HINSTANCE, int)
  118. //
  119. //   PURPOSE: Saves instance handle and creates main window
  120. //
  121. //   COMMENTS:
  122. //
  123. //        In this function, we save the instance handle in a global variable and
  124. //        create and display the main program window.
  125. //
  126. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  127. {
  128.    HWND hWnd;
  129.  
  130.    hInst = hInstance; // Store instance handle in our global variable
  131.  
  132.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  133.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  134.  
  135.    if (!hWnd)
  136.    {
  137.       return FALSE;
  138.    }
  139.  
  140.    ShowWindow(hWnd, nCmdShow);
  141.    UpdateWindow(hWnd);
  142.  
  143.    return TRUE;
  144. }
  145.  
  146. //
  147. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  148. //
  149. //  PURPOSE:  Processes messages for the main window.
  150. //
  151. //  WM_COMMAND  - process the application menu
  152. //  WM_PAINT    - Paint the main window
  153. //  WM_DESTROY  - post a quit message and return
  154. //
  155. //
  156. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  157. {
  158.     int wmId, wmEvent;
  159.     PAINTSTRUCT ps;
  160.     HDC hdc;
  161.  
  162.     switch (message)
  163.     {
  164.     case WM_SIZE:
  165.    
  166.        
  167.         MoveWindow(textboxhandle, 0, 0, LOWORD(lParam), HIWORD(lParam), true);
  168.         break;
  169.    
  170.     case WM_CREATE:
  171.     {
  172.         GetClientRect(hWnd, &coords);
  173.         int width = (coords.right - coords.left);
  174.         int height = (coords.bottom - coords.top);
  175.         textboxhandle =CreateWindowEx(NULL, L"EDIT", NULL, WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL | ES_AUTOVSCROLL, 0, 0, width, height, hWnd, NULL, hInst, NULL);
  176.         break;
  177.     }
  178.     case WM_COMMAND:
  179.         wmId    = LOWORD(wParam);
  180.         wmEvent = HIWORD(wParam);
  181.         // Parse the menu selections:
  182.         switch (wmId)
  183.         {
  184.         case IDM_SAVE:
  185.         {
  186.            
  187.             int size = Edit_GetTextLength(textboxhandle);
  188.             wchar_t *textboxdataptr = new wchar_t[size];
  189.             Edit_GetText(textboxhandle, textboxdataptr, size);
  190.             if (GetSaveFileName(&ofn) == true)
  191.              {
  192.                  std::wofstream myfile;
  193.                  myfile.open(ofn.lpstrFile);
  194.                  myfile.write(textboxdataptr, size);
  195.                  myfile.close();
  196.              }
  197.              delete[]textboxdataptr;
  198.              break;
  199.         }
  200.         case IDM_OPEN:
  201.         {
  202.            
  203.            
  204.             if (GetOpenFileName(&ofn) == true)
  205.             {
  206.                 std::wifstream myfile;
  207.                 myfile.open(ofn.lpstrFile);
  208.                 myfile.seekg(0, myfile.end);
  209.                 int length = myfile.tellg();
  210.                 myfile.seekg(0, myfile.beg);
  211.                 wchar_t *filebuf = new wchar_t[length];
  212.                 myfile.read(filebuf, length);
  213.                 Edit_SetText(textboxhandle, filebuf);
  214.                 delete[]filebuf;
  215.             }
  216.             break;
  217.         }
  218.         case IDM_ABOUT:
  219.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  220.             break;
  221.         case IDM_EXIT:
  222.             DestroyWindow(hWnd);
  223.             break;
  224.         default:
  225.             return DefWindowProc(hWnd, message, wParam, lParam);
  226.         }
  227.         break;
  228.     case WM_PAINT:
  229.         hdc = BeginPaint(hWnd, &ps);
  230.         // TODO: Add any drawing code here...
  231.         EndPaint(hWnd, &ps);
  232.         break;
  233.     case WM_DESTROY:
  234.         PostQuitMessage(0);
  235.         break;
  236.     default:
  237.         return DefWindowProc(hWnd, message, wParam, lParam);
  238.     }
  239.     return 0;
  240. }
  241.  
  242. // Message handler for about box.
  243. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  244. {
  245.     UNREFERENCED_PARAMETER(lParam);
  246.     switch (message)
  247.     {
  248.     case WM_INITDIALOG:
  249.         return (INT_PTR)TRUE;
  250.  
  251.     case WM_COMMAND:
  252.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  253.         {
  254.             EndDialog(hDlg, LOWORD(wParam));
  255.             return (INT_PTR)TRUE;
  256.         }
  257.         break;
  258.     }
  259.     return (INT_PTR)FALSE;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement