RichEdit.cpp
上传用户:dimled
上传日期:2007-02-08
资源大小:2367k
文件大小:9k
源码类别:

RichEdit

开发平台:

Visual C++

  1. #define STRICT
  2. /*
  3. Rich Edit example
  4. Coded by: weijixian
  5. */
  6. #include <windows.h>
  7. #include "rchedit.h" //this is my header
  8. void CenterWindow(HWND);
  9. HWND hWnd,richedit;
  10. HINSTANCE relib;
  11. LRESULT CALLBACK WndProc(HWND hWnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
  12. {
  13. HDC hdc;
  14. PAINTSTRUCT ps;
  15. static HMENU hmenu;
  16. EDITSTREAM editstream;
  17. RECT clientrect; //just for cosmetic purposes
  18. int height = 0;
  19. char fname[MAX_PATH] = {""};
  20. fstream fileio;
  21. string clipboard(""); //for clipboard functions
  22. HANDLE globalmem = NULL; //for clipboard functions
  23. LPSTR stringpntr = NULL; //for clipboard functions
  24. HANDLE cliphandle; //for paste
  25. string cliptext(""); //for paste
  26. NMHDR *notify; //for drag/drop (see wm_notify)
  27. ENDROPFILES *dropfiles; //for drag/drop (see wm_notify)
  28. HANDLE hdrop;
  29. char dropped[MAX_PATH] = {""};
  30. switch (iMsg)
  31. {
  32. case WM_CREATE:
  33. hmenu = LoadMenu(GetModuleHandle(NULL),MAKEINTRESOURCE(MENU));
  34. SetMenu(hWnd,hmenu);
  35. DrawMenuBar(hWnd);
  36. //To make up for titlebar and menu's affects on the height
  37. //we can use the client's height only for richedit window
  38. GetClientRect(hWnd,&clientrect);
  39. height = clientrect.bottom-clientrect.top;
  40. relib = LoadLibrary("riched32.dll"); //load the dll don't forget this
  41. //and don't forget to free it (see wm_destroy)
  42. if(relib==NULL)
  43. MessageBox(NULL,"Failed to load riched32.dll!","Error",MB_ICONEXCLAMATION);
  44. //might as well abort the program here but ill let you look at an empty window..
  45. //why wouldn't you have riched32.dll anyway!? :o ...
  46. richedit = CreateWindowEx(NULL,RICHEDIT_CLASS,"Open file or drag/drop into window",
  47. WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL|WS_HSCROLL|WS_VSCROLL,
  48. 0,0,390,height,hWnd,NULL,GetModuleHandle(NULL),NULL);
  49. //a richedit left as normal is stripped down
  50. //we have to enable things such as drag/drop capabilities
  51. SendMessage(richedit,EM_SETEVENTMASK,0,(LPARAM)(DWORD)ENM_DROPFILES);
  52. DragAcceptFiles(richedit,true); //allows the drag/drop
  53. break;
  54. case WM_SIZE:
  55. SetWindowPos(richedit,0,0,0,LOWORD(lParam),HIWORD(lParam),SWP_NOMOVE|SWP_NOZORDER);
  56. break;
  57. case WM_COMMAND:
  58. switch(LOWORD(wParam))
  59. {
  60. case FILE_OPEN:
  61. //here we get a filename and open the file to stream
  62. //the data in, this is actually very easy
  63. //refer to the callback to follow what's happening
  64. if(GetFileName(fname,MAX_PATH,hWnd)) //get filename to open
  65. {
  66. fileio.open(fname,ios::in); //open as you normally would
  67. if(fileio.fail()) //checks
  68. {
  69. fileio.close();
  70. MessageBox(hWnd,"Error opening file","Error",MB_OK);
  71. break;
  72. }
  73. SetStreamType(StreamType_File); //set stream type
  74. //setting the stream type is OUR way of knowing if it's
  75. //a buffer from a string or if it's a file object
  76. //this isn't window's code, this is our makeshift way
  77. editstream.pfnCallback = EditStreamCallback; //tell it the callback function
  78. editstream.dwCookie = (unsigned long)&fileio; //pass the file object through cookie
  79. SendMessage(richedit,EM_STREAMIN,SF_TEXT,(LPARAM)&editstream); //tell it to start stream in
  80. fileio.close(); //close file
  81. }
  82. break;
  83. case FILE_EXIT:
  84. SendMessage(hWnd,WM_CLOSE,0,0);
  85. break;
  86. case OPTIONS_COPY:
  87. if(OpenClipboard(hWnd)==false)
  88. {
  89. MessageBox(hWnd,"Another program is using the clipboard, please wait until that program finishes.","Error",MB_OK|MB_ICONEXCLAMATION);
  90. return false;
  91. }
  92. EmptyClipboard();
  93. editstream.pfnCallback = EditStreamCallbackRead;
  94. editstream.dwCookie = (unsigned long)&clipboard;
  95. SendMessage(richedit,EM_STREAMOUT,SF_TEXT,(LPARAM)&editstream);
  96. globalmem = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,clipboard.size()+1);
  97. stringpntr = (LPSTR)GlobalLock(globalmem);
  98. strcpy(stringpntr,clipboard.data());
  99. GlobalUnlock(globalmem);
  100. SetClipboardData(CF_TEXT,globalmem);
  101. CloseClipboard();
  102. break;
  103. case OPTIONS_PASTE:
  104. if(OpenClipboard(hWnd)==false)
  105. {
  106. MessageBox(hWnd,"Another program is using the clipboard, please wait until that program finishes.","Error",MB_OK|MB_ICONEXCLAMATION);
  107. return false;
  108. }
  109. cliphandle = GetClipboardData(CF_TEXT);
  110. if(cliphandle==0)//no text
  111. {
  112. CloseClipboard();
  113. return false;
  114. }
  115. stringpntr = (LPSTR)GlobalLock(cliphandle);
  116. cliptext = stringpntr;
  117. GlobalUnlock(cliphandle);
  118. CloseClipboard();
  119. SetStreamType(StreamType_Buffer); //set stream as buffer
  120. editstream.pfnCallback = EditStreamCallback; //set callback
  121. editstream.dwCookie = (unsigned long)&cliptext; //pass address of buffer
  122. SendMessage(richedit,EM_STREAMIN,SF_TEXT,(LPARAM)&editstream); //stream
  123. break;
  124. case OPTIONS_CLEAR:
  125. SendMessage(richedit,WM_SETTEXT,0,(LPARAM)(LPCSTR)""); //easy, old way
  126. //this would be a good time to mention that the reason we use STREAM_IN/OUT
  127. //is because the normal method of WM_SETTEXT has limitations
  128. //a normal edit control has a limit on the amount of text it can hold
  129. //but the limit a rich edit has depends on your system memory
  130. break;
  131. case ABOUT_ABOUT:
  132. MessageBox(hWnd,"RichText control example, WIN32 APInhttp://www.PlanetCpp.com","About",MB_OK);
  133. break;
  134. };
  135. break;
  136. case WM_NOTIFY:
  137. notify = (NMHDR*)lParam;
  138. if(notify->hwndFrom==richedit) 
  139. {
  140. switch(notify->code)
  141. {
  142. case EN_DROPFILES:
  143. dropfiles = (ENDROPFILES FAR *)lParam;
  144. hdrop = dropfiles->hDrop;
  145. DragQueryFile((HDROP)hdrop,0,dropped,MAX_PATH);//only first one will count
  146. DragFinish((HDROP)hdrop);
  147. for(int l=0;l<strlen(dropped);l++)
  148. if(isupper(dropped[l]))
  149. dropped[l]=tolower(dropped[l]);
  150. if(strstr(dropped,".txt")!=NULL || strstr(dropped,".dat")!=NULL
  151. || strstr(dropped,".log")!=NULL)
  152. {
  153. //same as open
  154. fileio.open(dropped,ios::in);
  155. if(fileio.fail()){
  156. fileio.close();
  157. MessageBox(hWnd,"Error opening file","Error",MB_OK);
  158. break;}
  159. SetStreamType(StreamType_File);
  160. editstream.pfnCallback = EditStreamCallback; //tell it the callback function
  161. editstream.dwCookie = (unsigned long)&fileio; //pass the file object through cookie
  162. SendMessage(richedit,EM_STREAMIN,SF_TEXT,(LPARAM)&editstream); //tell it to start stream in
  163. fileio.close(); //close file
  164. }
  165. else
  166. MessageBox(hWnd,"Only *.txt ,*.dat, and *.log files accepted.","Error",MB_OK);
  167. };
  168. }
  169. break;
  170. case WM_PAINT:
  171. hdc = BeginPaint(hWnd,&ps);
  172. EndPaint(hWnd,&ps);
  173. break;
  174. case WM_CLOSE:
  175. SendMessage(richedit,WM_CLOSE,0,0); //close richedit control
  176. if(relib!=NULL)
  177. FreeLibrary(relib); //tell DLL it's free to go
  178. break;
  179. case WM_DESTROY:
  180. PostQuitMessage(0); //i'm outta here
  181. break;
  182. }
  183. return DefWindowProc(hWnd,iMsg,wParam,lParam);
  184. }
  185. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  186.    PSTR szCmdLine, int iCmdShow)
  187. {
  188. const char *ClassName = "PCPP - RichEdit";
  189. WNDCLASSEX wclass; //here we set up a variable for our window class
  190. MSG msg; //this will hold the window messages later on
  191. //Next we set up the properties of the window class
  192. wclass.cbSize = sizeof(wclass); //the size
  193. wclass.style = CS_HREDRAW | CS_VREDRAW; //style
  194. wclass.lpfnWndProc = WndProc; //this tells windows the function to send messages to
  195. wclass.cbClsExtra = 0;
  196. wclass.cbWndExtra = 0;
  197. wclass.hInstance = hInstance; //the instance of your program
  198. wclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); //which icon to use
  199. wclass.hCursor = LoadCursor(NULL,IDC_ARROW); //the cursor to use
  200. wclass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); //background color
  201. wclass.lpszMenuName = NULL; //set the menu
  202. wclass.lpszClassName = ClassName; //set classname
  203. wclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION); //set small icon
  204. RegisterClassEx(&wclass); //this registers your window with windows
  205. hWnd = CreateWindow(ClassName, "PlanetCpp - RichEdit example",
  206. WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0, 0, 400, 300,NULL, NULL, hInstance, NULL);
  207. CenterWindow(hWnd);
  208. while(GetMessage(&msg,NULL,0,0))//getmessage loops until a message is on the queue
  209. { //then it returns focus,peekmessage does the same except returns focus no matter what
  210. TranslateMessage(&msg); //translate the message into its char equivelent
  211. DispatchMessage(&msg); //dispatch to the window
  212. }
  213. return msg.wParam;
  214. }
  215. ////////////////////////////////////////////
  216. void CenterWindow(HWND cwin)
  217. {
  218. int width = 0,height = 0;
  219. RECT workarea,winrect;
  220. GetWindowRect(cwin,&winrect);
  221. width = winrect.right-winrect.left;
  222. height = winrect.bottom - winrect.top;
  223. SystemParametersInfo(SPI_GETWORKAREA,0,&workarea,0);
  224. SetWindowPos(cwin,0,(workarea.right/2)-(width/2),(workarea.bottom/2)-(height/2),
  225. 0,0,SWP_NOSIZE|SWP_NOZORDER);
  226. }