RchEdit.h
资源名称:RichEdit.zip [点击查看]
上传用户:dimled
上传日期:2007-02-08
资源大小:2367k
文件大小:4k
源码类别:
RichEdit
开发平台:
Visual C++
- #ifndef RCHEDIT_H
- #define RCHEDIT_H
- int GetFileName(char*, int, HWND);
- #define MENU 101
- #define FILE_OPEN 1001
- #define FILE_EXIT 1002
- #define OPTIONS_COPY 1003
- #define OPTIONS_PASTE 1004
- #define OPTIONS_CLEAR 1005
- #define ABOUT_ABOUT 1006
- #include <richedit.h>
- #include <fstream>
- using std::fstream;
- #include <string>
- using std::string;
- using std::ios;
- #define StreamType_File 1
- #define StreamType_Buffer 2
- #define StreamType_NA 0
- int StreamType = StreamType_NA;
- void SetStreamType(int st)
- {
- //1 = file, 2 = buffer
- //StreamType_File, StreamType_Buffer
- StreamType = st;
- }
- int GetStreamType(void)
- {
- return StreamType;
- }
- static DWORD CALLBACK EditStreamCallback(DWORD dwCookie,LPBYTE pbBuff,LONG cb,LONG *pcb)
- {
- /*
- DWORD CALLBACK EditStreamCallback(
- DWORD dwCookie, // application-defined value (i use this to pass file handle or TCHAR buffer address)
- LPBYTE pbBuff, // pointer to a buffer (you write/read to/from this)
- LONG cb, // number of bytes to read or write (# of bytes it will allow you to read/write)
- LONG *pcb // pointer to number of bytes transferred
- );
- */
- if(StreamType==StreamType_NA)
- return 0;
- else if(StreamType==StreamType_File) //were we passed a file object?
- {
- fstream *fpntr = (fstream*)dwCookie; //cast it as a file object
- fpntr->read((char *)pbBuff,(unsigned int)cb); //read data
- *pcb = (long)fpntr->gcount(); //tells windows when to stop calling the callback function
- }
- else if(StreamType==StreamType_Buffer) //were we passed a string object?
- {
- string *writes = (string *)dwCookie; //cast as string
- if(writes->size()<cb) //check if we are allowed to write rest or not
- {//since the size of the string is less then what were allowed
- *pcb = (long)writes->size(); //set size to string size
- memcpy(pbBuff,(void *)writes->data(),*pcb); //write rest
- writes->erase(); //erase whole string
- }
- else
- { //string is too big to write
- *pcb = cb; //set size to allowed transfer size
- memcpy(pbBuff,(void *)writes->data(),*pcb); //write allowed amount
- writes->erase(0,cb); //erase only what we wrote
- }
- }
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////
- static DWORD CALLBACK EditStreamCallbackRead(DWORD dwCookie,LPBYTE pbBuff,LONG cb,LONG *pcb)
- {
- string *reads = (string *)dwCookie; //here we read into a string
- //try changing this to write to a file
- if(cb>0) //as long as theres something to get
- {
- *pcb = cb; //set to size of what we can get
- reads->append((char *)pbBuff,cb); //get
- }
- return 0;
- }
- ////////////////////////////////////////////////////////////////
- int GetFileName(char *filename, int len, HWND hWnd)
- {
- //this will open that nice windows dialog for opening/saving files
- //no need to reinvent the wheel (yea even I said that!)
- //plus these look very professional
- OPENFILENAME openpic;
- ZeroMemory(&openpic, sizeof(OPENFILENAME));
- openpic.lStructSize = sizeof(OPENFILENAME);
- openpic.hwndOwner = hWnd;
- openpic.lpstrFilter = "Text Files (*.txt/*.dat/*.log) *.txt *.dat *.log "; //constant for this example
- openpic.lpstrFile = filename;
- openpic.lpstrInitialDir = "c:\"; //this is default, no need to hold onto
- //last directory for an example
- openpic.nMaxFile = len;
- openpic.lpstrTitle = ("PlanetCpp richedit example.");
- openpic.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;
- return GetOpenFileName(&openpic); //get and return status
- }
- ////////////////////////////////////////////////////////////////
- #endif