Annonceindlæg fra Partnertekst
17. marts 2006 - 00:11
#3
En text editor der er ca. så simpel som det kan blive: #include <windows.h> #include <commctrl.h> #include <fstream> char CurrentFileName[256]; enum CommandEnum { FileOpenCmd = 1025, FileSaveCmd, FileCloseCmd, AboutCmd, }; enum WindowCtrlId { EditWindowId = 130 }; HINSTANCE InstanceHandle; HWND MainWindow, EditWindow; void OnFileSave(); bool ReadFile(const char *aFileName) { std::ifstream File(aFileName); if(!File) return false; std::string Line, Text; while(std::getline(File, Line)) { Text += Line.c_str(); Text += "\r\n"; } SendMessage(EditWindow, WM_SETTEXT, 0, (LPARAM )Text.c_str()); return true; } void OnFileOpen() { if(SendMessage(EditWindow, EM_GETMODIFY, 0, 0)) { if(MessageBox(MainWindow, "The current file has been modified\r\nDo you wan't to save it before closing?", "TextEd", MB_YESNO | MB_ICONQUESTION) == IDYES) { OnFileSave(); } } static char Filter[] = "txt (*.txt)\0*.txt\0All (*.*)\0*.*\0"; OPENFILENAME OpenFilename; memset(&OpenFilename, 0, sizeof(OpenFilename)); OpenFilename.lStructSize = sizeof(OpenFilename); OpenFilename.hwndOwner = MainWindow; OpenFilename.hInstance = InstanceHandle; OpenFilename.lpstrFilter = Filter; OpenFilename.lpstrFile = CurrentFileName; OpenFilename.nMaxFile = sizeof(CurrentFileName); OpenFilename.Flags = OFN_FILEMUSTEXIST; OpenFilename.lpstrDefExt = "txt"; if(GetOpenFileName(&OpenFilename)) { if(!ReadFile(CurrentFileName)) { CurrentFileName[0] = 0; std::string Msg = "Failed to open:\r\n"; Msg += CurrentFileName; MessageBox(MainWindow, Msg.c_str(), "TextEditor", MB_OK | MB_ICONWARNING); } } } void OnFileSave() { if(CurrentFileName[0] == 0) { // Need to Get A filename static char Filter[] = "txt (*.txt)\0*.txt\0All (*.*)\0*.*\0"; OPENFILENAME OpenFilename; memset(&OpenFilename, 0, sizeof(OpenFilename)); OpenFilename.lStructSize = sizeof(OpenFilename); OpenFilename.hwndOwner = MainWindow; OpenFilename.hInstance = InstanceHandle; OpenFilename.lpstrFilter = Filter; OpenFilename.lpstrFile = CurrentFileName; OpenFilename.nMaxFile = sizeof(CurrentFileName); OpenFilename.Flags = 0; OpenFilename.lpstrDefExt = "txt"; if(!GetSaveFileName(&OpenFilename)) { CurrentFileName[0] = 0; return; } } std::ofstream File(CurrentFileName, std::ios::binary); if(!File) { std::string Msg = "Failed to open:\r\n"; Msg += CurrentFileName; MessageBox(MainWindow, Msg.c_str(), "TextEditor", MB_OK | MB_ICONWARNING); CurrentFileName[0] = 0; return; } int TextLength = GetWindowTextLength(EditWindow); char *Temp = new char [TextLength + 1]; GetWindowText(EditWindow, Temp, TextLength + 1); File << Temp; delete [] Temp; } LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: if(SendMessage(EditWindow, EM_GETMODIFY, 0, 0)) { if(MessageBox(MainWindow, "The current file has been modified\r\nDo you wan't to save it before closing?", "TextEd", MB_YESNO | MB_ICONQUESTION) == IDYES) { OnFileSave(); } } PostQuitMessage(0); break; case WM_CREATE: MainWindow = hwnd; EditWindow = CreateWindow("EDIT", "", WS_VISIBLE | WS_VSCROLL | WS_CHILD | ES_MULTILINE | WS_HSCROLL, 20, 20, 100, 100, MainWindow, (HMENU )EditWindowId, InstanceHandle, 0); if(CurrentFileName[0]) { if(!ReadFile(CurrentFileName)) { std::string Msg = "Failed to open:\r\n"; Msg += CurrentFileName; MessageBox(hwnd, Msg.c_str(), "TextEd", MB_OK); CurrentFileName[0] = 0; } } SetFocus(EditWindow); break; case WM_COMMAND: switch(LOWORD(wParam)) { case FileCloseCmd: PostQuitMessage(0); break; case FileOpenCmd: OnFileOpen(); break; case FileSaveCmd: OnFileSave(); break; case AboutCmd: MessageBox(MainWindow, "TextEditor\r\nCopyright Bertel Brander 2006", "TextEditor", MB_OK); break; } break; case WM_SIZE: RECT ClientRect; GetClientRect(hwnd, &ClientRect); MoveWindow(EditWindow, ClientRect.left, ClientRect.top, ClientRect.right - ClientRect.left, ClientRect.bottom - ClientRect.top, TRUE); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } HWND CreateMainWindow() { WNDCLASS wc; memset(&wc, 0, sizeof(WNDCLASS)); wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ; wc.lpfnWndProc = (WNDPROC )MainWndProc; wc.hInstance = InstanceHandle; wc.hbrBackground = (HBRUSH )(COLOR_WINDOW + 1); wc.lpszClassName = "SimpleWinWndClass"; wc.lpszMenuName = 0; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClass(&wc)) return 0; HMENU FileMenu = CreateMenu(); AppendMenu(FileMenu, MF_STRING, FileOpenCmd, "Open"); AppendMenu(FileMenu, MF_STRING, FileSaveCmd, "Save"); AppendMenu(FileMenu, MF_SEPARATOR, 0 , 0); AppendMenu(FileMenu, MF_STRING, FileCloseCmd, "Close"); HMENU HelpMenu = CreateMenu(); AppendMenu(HelpMenu, MF_STRING, AboutCmd, "About"); HMENU MainMenu = CreateMenu(); AppendMenu(MainMenu, MF_POPUP, (UINT )FileMenu, "File"); AppendMenu(MainMenu, MF_POPUP, (UINT )HelpMenu, "Help"); return CreateWindow("SimpleWinWndClass", "Simple Text Editor", WS_MINIMIZEBOX | WS_VISIBLE | WS_MAXIMIZEBOX | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CAPTION | WS_BORDER | WS_SYSMENU | WS_THICKFRAME, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, MainMenu, InstanceHandle, 0); } int WINAPI WinMain(HINSTANCE Instance, HINSTANCE, LPSTR CmdLine, INT) { InstanceHandle = Instance; InitCommonControls(); if(CmdLine[0]) { strcpy(CurrentFileName, CmdLine); } if((MainWindow = CreateMainWindow()) == (HWND )0) { MessageBox(0, "Failed to create MainWindow!", "TextEditor", MB_OK); return 0; } ShowWindow(MainWindow, SW_SHOW); MSG Msg; while(GetMessage(&Msg, 0, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }