Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <Richedit.h>
- #pragma warning(disable : 4996)
- #define IDM_OPEN 201
- #define IDM_SAVE 202
- #define IDM_CLOSE 203
- #define IDM_ABOUT 204
- #define ID_RICH 301
- class error_descript
- {
- public:
- error_descript(){}
- ~error_descript(){}
- void ShowLastErrorDescription(char *comment)
- {
- LPTSTR s = NULL;
- DWORD errCode = GetLastError();
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, errCode, 0, (LPTSTR)&s, 0, NULL);
- if (s != NULL)
- {
- char *buf = new char[4096];
- wsprintfA(buf, "%s: dec: %d ; hex: %X", comment, errCode, errCode);
- MessageBoxA(0, s, buf, MB_ICONERROR);
- delete[]buf;
- LocalFree(s);
- }
- }
- };
- class base_gui : protected error_descript
- {
- protected:
- HWND this_con;
- HWND parentWnd;
- WNDCLASSEX wcex;
- char *wnd_name;
- void GetActualSize(HWND wnd,LPRECT rect)
- {
- if (GetClientRect(wnd, rect) == 0)
- {
- this->ShowLastErrorDescription("");
- }
- }
- public:
- base_gui()
- {
- wnd_name = NULL;
- }
- base_gui(char *window_name,WNDPROC callback_ptr,HINSTANCE inst)
- {
- wnd_name = new char[strlen(window_name)];
- strcpy(wnd_name, window_name);
- parentWnd = 0;
- ZeroMemory(&wcex, sizeof(WNDCLASSEX));
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = callback_ptr;
- wcex.hInstance = inst;
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wcex.lpszClassName = wnd_name;
- if (RegisterClassEx(&wcex) == 0)
- {
- this->ShowLastErrorDescription("");
- }
- }
- base_gui(HWND parent, char *window_name,int id, HINSTANCE inst)
- {
- ZeroMemory(&wcex, sizeof(WNDCLASSEX));
- wnd_name = new char[strlen(window_name)];
- strcpy(wnd_name, window_name);
- wcex.hInstance = inst;
- }
- ~base_gui()
- {
- }
- void ShowLikeParentWindow(char *wnd_title)
- {
- this->this_con = CreateWindowA(wcex.lpszClassName, wnd_title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, wcex.hInstance, NULL);
- if (this_con == 0)
- {
- this->ShowLastErrorDescription("");
- }
- ShowWindow(this_con, SW_SHOW);
- if (UpdateWindow(this_con) == 0)
- {
- this->ShowLastErrorDescription("");
- }
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- virtual void ShowLikeChild(){}
- virtual void ShowLikeChild(char *label){}
- virtual void ShowLikeChild(int sX, int sY, int eX, int eY, char *label){}
- HWND GetCtrlHandle()
- {
- return this->this_con;
- }
- };
- class menu_builder :private error_descript
- {
- private:
- HWND parentWnd;
- HMENU menubar;
- HMENU current_menu;
- public:
- menu_builder(HWND parent)
- {
- parentWnd = parent;
- menubar = CreateMenu();
- if (menubar == 0)
- {
- this->ShowLastErrorDescription("");
- }
- }
- ~menu_builder(){}
- void BeginCreateMenu()
- {
- current_menu = CreateMenu();
- if (current_menu == 0)
- {
- this->ShowLastErrorDescription("");
- }
- }
- void FinishMenu(char *menu_name)
- {
- if (AppendMenu(menubar, MF_POPUP, (UINT_PTR)current_menu, menu_name) == 0)
- {
- this->ShowLastErrorDescription("");
- }
- }
- void SetMenu()
- {
- if (::SetMenu(parentWnd, menubar) == 0)
- {
- this->ShowLastErrorDescription("");
- }
- }
- void AddMenuLabel(char *label,int id)
- {
- if (AppendMenu(current_menu, MF_STRING, id, label) == 0)
- {
- this->ShowLastErrorDescription("");
- }
- }
- void AddSeparator()
- {
- if (AppendMenu(current_menu, MF_SEPARATOR, 0, NULL) == 0)
- {
- this->ShowLastErrorDescription("");
- }
- }
- };
- class richedit_using : private base_gui
- {
- private:
- int ctrl_id;
- public:
- richedit_using(HWND parent, char *window_name, int id, HINSTANCE inst) :base_gui(parent, window_name, id, inst)
- {
- if (LoadLibrary("riched32.dll") == 0)
- {
- this->ShowLastErrorDescription("");
- }
- ctrl_id = id;
- this->parentWnd = parent;
- this->wnd_name = new char[strlen(window_name)];
- strcpy(wnd_name, window_name);
- this->wcex.hInstance = inst;
- }
- ~richedit_using(){}
- void ShowLikeChild(char *label)
- {
- this->this_con = CreateWindow(this->wnd_name, label, ES_MULTILINE | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_AUTOVSCROLL,
- 0, 0, 50, 50, this->parentWnd, (HMENU)ctrl_id, this->wcex.hInstance, 0);
- SendMessage(this->this_con, EM_SHOWSCROLLBAR, (WPARAM)SB_VERT, (LPARAM)TRUE);
- SendMessage(this->this_con, EM_SHOWSCROLLBAR, (WPARAM)SB_HORZ, (LPARAM)TRUE);
- if (this->this_con == 0)
- {
- this->ShowLastErrorDescription("");
- }
- }
- void AlignSize()
- {
- RECT rect = { 0 };
- this->GetActualSize(this->parentWnd, &rect);
- SetWindowPos(this->this_con, 0, 0, 0, rect.right, rect.bottom, SWP_SHOWWINDOW);
- }
- void SetText(char *text)
- {
- if (text != 0)
- {
- SetWindowTextA(this->this_con, text);
- }
- }
- char *GetText()
- {
- int len = GetWindowTextLengthA(this->this_con);
- if (len == 0)
- {
- return 0;
- }
- char *to_ret = new char[len + 1];
- to_ret[len] = 0;
- if (GetWindowTextA(this->this_con, to_ret, len) == 0)
- {
- this->ShowLastErrorDescription("");
- }
- return to_ret;
- }
- };
- class text_operator : private error_descript
- {
- private:
- HWND parent;
- char* ReadFromDisk(char *patch)
- {
- HANDLE f = CreateFile(patch, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- if (f == INVALID_HANDLE_VALUE)
- {
- this->ShowLastErrorDescription("");
- return 0;
- }
- LARGE_INTEGER sz = { 0 };
- GetFileSizeEx(f, &sz);
- char *ret_data = new char[sz.QuadPart + 1];
- ret_data[ret_data, 0, sz.QuadPart] = 0;
- DWORD r = 0;
- ReadFile(f, ret_data, sz.QuadPart, &r, 0);
- CloseHandle(f);
- return ret_data;
- }
- void WriteToDisk(char *data, char *patch)
- {
- HANDLE f = CreateFile(patch, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- if (f == INVALID_HANDLE_VALUE)
- {
- this->ShowLastErrorDescription("");
- return;
- }
- DWORD w = 0;
- WriteFile(f, data, strlen(data), &w, 0);
- CloseHandle(f);
- }
- void RemoveBOM(char *data)
- {
- char *buf = 0;
- if( ((unsigned short*)data)[0] == 0xEFBB )
- {
- data += 3;
- int textLen = strlen(data);
- buf = new char[textLen + 1];
- memset(buf, 0, textLen + 1);
- strcpy(buf, data);
- data -= 3;
- memset(data, 0, textLen + 3);
- strcpy(data, buf);
- delete[]buf;
- return;
- }
- if (((unsigned short*)data)[0] == 0xFEFF || ((unsigned short*)data)[0] == 0xFFFE)
- {
- data += 2;
- int textLen = strlen(data);
- buf = new char[textLen + 1];
- memset(buf, 0, textLen + 1);
- strcpy(buf, data);
- data -= 2;
- memset(data, 0, textLen + 3);
- strcpy(data, buf);
- delete[]buf;
- return;
- }
- }
- char* StartOpenDialog()
- {
- char *fname = new char[4096];
- OPENFILENAME ofn = { 0 };
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = parent;
- ofn.lpstrFile = fname;
- ofn.lpstrFile[0] = '\0';
- ofn.nMaxFile = MAX_PATH;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
- GetOpenFileName(&ofn);
- return fname;
- }
- char* StartSaveDialog()
- {
- char *fname = new char[4096];
- OPENFILENAME ofn = { 0 };
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = parent;
- ofn.lpstrFile = fname;
- ofn.lpstrFile[0] = '\0';
- ofn.nMaxFile = MAX_PATH;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
- GetSaveFileName(&ofn);
- return fname;
- }
- public:
- text_operator(HWND parent)
- {
- this->parent = parent;
- }
- ~text_operator(){}
- char *OpenFile()
- {
- char *out_tex;
- char* patch = this->StartOpenDialog();
- if (strlen(patch) == 0)
- {
- delete[]patch;
- return 0;
- }
- out_tex = this->ReadFromDisk(patch);
- this->RemoveBOM(out_tex);
- delete[]patch;
- return out_tex;
- }
- void SaveFile(char *in_text)
- {
- if (in_text != NULL)
- {
- char* patch = this->StartSaveDialog();
- if (strlen(patch) == 0)
- {
- delete[]patch;
- return;
- }
- this->WriteToDisk(in_text, patch);
- delete[]patch;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement