Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.87 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <Richedit.h>
  3. #pragma warning(disable : 4996)
  4.  
  5. #define IDM_OPEN 201
  6. #define IDM_SAVE 202
  7. #define IDM_CLOSE 203
  8. #define IDM_ABOUT 204
  9.  
  10. #define ID_RICH 301
  11.  
  12. class error_descript
  13. {
  14. public:
  15. error_descript(){}
  16. ~error_descript(){}
  17. void ShowLastErrorDescription(char *comment)
  18. {
  19. LPTSTR s = NULL;
  20. DWORD errCode = GetLastError();
  21. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, errCode, 0, (LPTSTR)&s, 0, NULL);
  22. if (s != NULL)
  23. {
  24. char *buf = new char[4096];
  25. wsprintfA(buf, "%s: dec: %d ; hex: %X", comment, errCode, errCode);
  26. MessageBoxA(0, s, buf, MB_ICONERROR);
  27. delete[]buf;
  28. LocalFree(s);
  29. }
  30. }
  31. };
  32.  
  33. class base_gui : protected error_descript
  34. {
  35. protected:
  36. HWND this_con;
  37. HWND parentWnd;
  38. WNDCLASSEX wcex;
  39. char *wnd_name;
  40.  
  41. void GetActualSize(HWND wnd,LPRECT rect)
  42. {
  43. if (GetClientRect(wnd, rect) == 0)
  44. {
  45. this->ShowLastErrorDescription("");
  46. }
  47. }
  48. public:
  49. base_gui()
  50. {
  51. wnd_name = NULL;
  52. }
  53. base_gui(char *window_name,WNDPROC callback_ptr,HINSTANCE inst)
  54. {
  55. wnd_name = new char[strlen(window_name)];
  56. strcpy(wnd_name, window_name);
  57. parentWnd = 0;
  58.  
  59. ZeroMemory(&wcex, sizeof(WNDCLASSEX));
  60. wcex.cbSize = sizeof(WNDCLASSEX);
  61. wcex.style = CS_HREDRAW | CS_VREDRAW;
  62. wcex.lpfnWndProc = callback_ptr;
  63. wcex.hInstance = inst;
  64. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  65. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  66. wcex.lpszClassName = wnd_name;
  67. if (RegisterClassEx(&wcex) == 0)
  68. {
  69. this->ShowLastErrorDescription("");
  70. }
  71. }
  72. base_gui(HWND parent, char *window_name,int id, HINSTANCE inst)
  73. {
  74. ZeroMemory(&wcex, sizeof(WNDCLASSEX));
  75. wnd_name = new char[strlen(window_name)];
  76. strcpy(wnd_name, window_name);
  77. wcex.hInstance = inst;
  78. }
  79. ~base_gui()
  80. {
  81.  
  82. }
  83. void ShowLikeParentWindow(char *wnd_title)
  84. {
  85. this->this_con = CreateWindowA(wcex.lpszClassName, wnd_title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, wcex.hInstance, NULL);
  86. if (this_con == 0)
  87. {
  88. this->ShowLastErrorDescription("");
  89. }
  90. ShowWindow(this_con, SW_SHOW);
  91. if (UpdateWindow(this_con) == 0)
  92. {
  93. this->ShowLastErrorDescription("");
  94. }
  95. MSG msg;
  96. while (GetMessage(&msg, NULL, 0, 0))
  97. {
  98. TranslateMessage(&msg);
  99. DispatchMessage(&msg);
  100. }
  101. }
  102. virtual void ShowLikeChild(){}
  103. virtual void ShowLikeChild(char *label){}
  104. virtual void ShowLikeChild(int sX, int sY, int eX, int eY, char *label){}
  105. HWND GetCtrlHandle()
  106. {
  107. return this->this_con;
  108. }
  109. };
  110.  
  111. class menu_builder :private error_descript
  112. {
  113. private:
  114. HWND parentWnd;
  115. HMENU menubar;
  116. HMENU current_menu;
  117. public:
  118. menu_builder(HWND parent)
  119. {
  120. parentWnd = parent;
  121. menubar = CreateMenu();
  122. if (menubar == 0)
  123. {
  124. this->ShowLastErrorDescription("");
  125. }
  126. }
  127. ~menu_builder(){}
  128. void BeginCreateMenu()
  129. {
  130. current_menu = CreateMenu();
  131. if (current_menu == 0)
  132. {
  133. this->ShowLastErrorDescription("");
  134. }
  135. }
  136. void FinishMenu(char *menu_name)
  137. {
  138. if (AppendMenu(menubar, MF_POPUP, (UINT_PTR)current_menu, menu_name) == 0)
  139. {
  140. this->ShowLastErrorDescription("");
  141. }
  142. }
  143. void SetMenu()
  144. {
  145. if (::SetMenu(parentWnd, menubar) == 0)
  146. {
  147. this->ShowLastErrorDescription("");
  148. }
  149. }
  150. void AddMenuLabel(char *label,int id)
  151. {
  152. if (AppendMenu(current_menu, MF_STRING, id, label) == 0)
  153. {
  154. this->ShowLastErrorDescription("");
  155. }
  156. }
  157. void AddSeparator()
  158. {
  159. if (AppendMenu(current_menu, MF_SEPARATOR, 0, NULL) == 0)
  160. {
  161. this->ShowLastErrorDescription("");
  162. }
  163. }
  164. };
  165.  
  166. class richedit_using : private base_gui
  167. {
  168. private:
  169. int ctrl_id;
  170. public:
  171. richedit_using(HWND parent, char *window_name, int id, HINSTANCE inst) :base_gui(parent, window_name, id, inst)
  172. {
  173. if (LoadLibrary("riched32.dll") == 0)
  174. {
  175. this->ShowLastErrorDescription("");
  176. }
  177. ctrl_id = id;
  178. this->parentWnd = parent;
  179. this->wnd_name = new char[strlen(window_name)];
  180. strcpy(wnd_name, window_name);
  181. this->wcex.hInstance = inst;
  182. }
  183. ~richedit_using(){}
  184. void ShowLikeChild(char *label)
  185. {
  186. this->this_con = CreateWindow(this->wnd_name, label, ES_MULTILINE | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_AUTOVSCROLL,
  187. 0, 0, 50, 50, this->parentWnd, (HMENU)ctrl_id, this->wcex.hInstance, 0);
  188. SendMessage(this->this_con, EM_SHOWSCROLLBAR, (WPARAM)SB_VERT, (LPARAM)TRUE);
  189. SendMessage(this->this_con, EM_SHOWSCROLLBAR, (WPARAM)SB_HORZ, (LPARAM)TRUE);
  190. if (this->this_con == 0)
  191. {
  192. this->ShowLastErrorDescription("");
  193. }
  194. }
  195. void AlignSize()
  196. {
  197. RECT rect = { 0 };
  198. this->GetActualSize(this->parentWnd, &rect);
  199. SetWindowPos(this->this_con, 0, 0, 0, rect.right, rect.bottom, SWP_SHOWWINDOW);
  200. }
  201. void SetText(char *text)
  202. {
  203. if (text != 0)
  204. {
  205. SetWindowTextA(this->this_con, text);
  206. }
  207. }
  208. char *GetText()
  209. {
  210. int len = GetWindowTextLengthA(this->this_con);
  211. if (len == 0)
  212. {
  213. return 0;
  214. }
  215. char *to_ret = new char[len + 1];
  216. to_ret[len] = 0;
  217. if (GetWindowTextA(this->this_con, to_ret, len) == 0)
  218. {
  219. this->ShowLastErrorDescription("");
  220. }
  221. return to_ret;
  222. }
  223. };
  224.  
  225. class text_operator : private error_descript
  226. {
  227. private:
  228. HWND parent;
  229. char* ReadFromDisk(char *patch)
  230. {
  231. HANDLE f = CreateFile(patch, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  232. if (f == INVALID_HANDLE_VALUE)
  233. {
  234. this->ShowLastErrorDescription("");
  235. return 0;
  236. }
  237. LARGE_INTEGER sz = { 0 };
  238. GetFileSizeEx(f, &sz);
  239. char *ret_data = new char[sz.QuadPart + 1];
  240. ret_data[ret_data, 0, sz.QuadPart] = 0;
  241. DWORD r = 0;
  242. ReadFile(f, ret_data, sz.QuadPart, &r, 0);
  243. CloseHandle(f);
  244. return ret_data;
  245. }
  246. void WriteToDisk(char *data, char *patch)
  247. {
  248. HANDLE f = CreateFile(patch, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  249. if (f == INVALID_HANDLE_VALUE)
  250. {
  251. this->ShowLastErrorDescription("");
  252. return;
  253. }
  254. DWORD w = 0;
  255. WriteFile(f, data, strlen(data), &w, 0);
  256. CloseHandle(f);
  257. }
  258. void RemoveBOM(char *data)
  259. {
  260. char *buf = 0;
  261. if( ((unsigned short*)data)[0] == 0xEFBB )
  262. {
  263. data += 3;
  264. int textLen = strlen(data);
  265. buf = new char[textLen + 1];
  266. memset(buf, 0, textLen + 1);
  267. strcpy(buf, data);
  268. data -= 3;
  269. memset(data, 0, textLen + 3);
  270. strcpy(data, buf);
  271. delete[]buf;
  272. return;
  273. }
  274. if (((unsigned short*)data)[0] == 0xFEFF || ((unsigned short*)data)[0] == 0xFFFE)
  275. {
  276. data += 2;
  277. int textLen = strlen(data);
  278. buf = new char[textLen + 1];
  279. memset(buf, 0, textLen + 1);
  280. strcpy(buf, data);
  281. data -= 2;
  282. memset(data, 0, textLen + 3);
  283. strcpy(data, buf);
  284. delete[]buf;
  285. return;
  286. }
  287.  
  288. }
  289. char* StartOpenDialog()
  290. {
  291. char *fname = new char[4096];
  292. OPENFILENAME ofn = { 0 };
  293. ofn.lStructSize = sizeof(ofn);
  294. ofn.hwndOwner = parent;
  295. ofn.lpstrFile = fname;
  296. ofn.lpstrFile[0] = '\0';
  297. ofn.nMaxFile = MAX_PATH;
  298. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  299.  
  300. GetOpenFileName(&ofn);
  301. return fname;
  302. }
  303.  
  304. char* StartSaveDialog()
  305. {
  306. char *fname = new char[4096];
  307. OPENFILENAME ofn = { 0 };
  308. ofn.lStructSize = sizeof(ofn);
  309. ofn.hwndOwner = parent;
  310. ofn.lpstrFile = fname;
  311. ofn.lpstrFile[0] = '\0';
  312. ofn.nMaxFile = MAX_PATH;
  313. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  314.  
  315. GetSaveFileName(&ofn);
  316. return fname;
  317. }
  318.  
  319. public:
  320. text_operator(HWND parent)
  321. {
  322. this->parent = parent;
  323. }
  324. ~text_operator(){}
  325. char *OpenFile()
  326. {
  327. char *out_tex;
  328. char* patch = this->StartOpenDialog();
  329. if (strlen(patch) == 0)
  330. {
  331. delete[]patch;
  332. return 0;
  333. }
  334. out_tex = this->ReadFromDisk(patch);
  335. this->RemoveBOM(out_tex);
  336. delete[]patch;
  337. return out_tex;
  338. }
  339. void SaveFile(char *in_text)
  340. {
  341. if (in_text != NULL)
  342. {
  343. char* patch = this->StartSaveDialog();
  344. if (strlen(patch) == 0)
  345. {
  346. delete[]patch;
  347. return;
  348. }
  349. this->WriteToDisk(in_text, patch);
  350. delete[]patch;
  351. }
  352. }
  353. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement