|
Hello All,
I am new to MFC - but have used C for around 3 years so I have the basics sorted out.
My question is I am trying to write my own saving routine and have created a SaveFile funtion in which I am using fwrite, to save information to a txt file - the problem being is it will save fine until the code reaches the line : saveArchive.Close(); in Doccore.cpp, this then clears the file.
Now being new to MFC Ihave learnt a fair amount on my own but cannot seem to solve this problem.
If this helps here is my code in my savefile function.
void CMy_newDoc :: SaveFile(const char *filename)
{
char buffer[255];
FILE *file = fopen (filename, "w+");
sprintf (buffer, "Text tobe saved into file");
fwrite( buffer, sizeof( char ), sizeof(buffer), file );
fclose(file);
}
Thanks in adavance for helping a newbie to MFC
|
|
|
|
|
The problem is that MFC has it's own code for persistence, and it is creating an empty file for you. You should look into iostreams ( which is standard C++ persistence code ) instead of using this C stuff, which is ugly and inflexible in comparison.
I'm not sure how to get the archive code to leave your file alone, but the way I usually do it is roll my own function in response to File/Save which brings up it's own CFileDialog and uses that path, that way MFC never gets to know where it is.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Are you use the doc/view architecture? If yes, make sure you disable serialization. Although I do not know how the framework knows what file to write to since you do saving inside of a separate function.
What calls function SaveFile(...)?
Kuphryn
|
|
|
|
|
Hey thanks guys for the quick respnse - its great to see that people are willing to help out, I try on OpenGL forums - now you have inspired me to try harder.
Anyway Christian - that thought totally escped me and I will go ahed and do it that way, thanks - just a small question hwo do i then capture what file has been enterd into the file box?
kuphryn - i am calling the saving code from
void CMy_newDoc::Serialize(CArchive& ar)
{
int ok;
CString filename;
filename = ar.GetFile()->GetFileName();
if (ar.IsStoring())
{
// TODO: add storing code here
SaveFile(filename);
}
Thanks again for the help
|
|
|
|
|
Implement handlers for Save and Save File As instead of called the save function from inside Serialize().
Kuphryn
|
|
|
|
|
Dinner@6 wrote:
just a small question hwo do i then capture what file has been enterd into the file box?
If you create your own CFileDialog, you can call GetFile(), or GetPath(). Something like that.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
You're basically overriding the overall Document/View architecture in MFC if you write your own archiving streams. CArchive is an already opened archive stream, wired up by MFC, CDocument and CFileDialog (to get the file name to save to.) All you have to do in Serialize is use the C++ insertion/extraction operators to get data into and out of the file:
void CMy_newDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << buffer;
}
else
{
ar >> buffer;
}
}
If your data is stored in a class, you can derive it from CObject , and override the Serialize method to store class-specific data. That way you can use the C++ insertion/extraction operators on an object instance:
class CMyObject : public CObject
{
protected:
int m_nOne;
float m_fTwo;
CString m_strData;
public:
CMyObject() : m_nOne(0), m_fTwo(0.0) { }
int GetOne(void) const { return m_nOne; }
void SetOne(int n) { m_nOne = n; }
float GetTwo(void) const { return m_fTwo; }
void SetTwo(float f) { m_fTwo = f; }
LPCTSTR GetData(void) const { return m_strData; }
void SetData(LPCTSTR s) { m_strData = s; }
DECLARE_SERIAL(CMyObject)
virtual void Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << m_nOne << m_fTwo << m_strData;
}
else
{
ar >> m_nOne >> m_fTwo >> m_strData;
}
}
};
IMPLEMENT_SERIAL(CMyObject, CObject, VERSIONABLE_SCHEMA | 1)
void CMy_newDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << m_oMyObj;
}
else
{
ar >> m_oMyObj;
}
}
There you go. CDocument 's OnSaveDocument() and OnOpenDocument() (which you don't have to mess with) end up calling CDocument's base class CObject::Serialize . CObject reference[^] Here's an MFC Object Serialization referance[^]
-- ian
http://www.ian-space.com/
|
|
|
|
|
I want to use CImage to save a dib as a jpg file. However,
whenever I save it produces a black rectangle. Any suggestions?
Here is my code:
CImage img;
int h = 292;
int w = 248;
// Create a dib section
img.Create(w, h, 8);
// Get a DC and draw in it (a red rectangle)
HDC hdc = img.GetDC();
RECT r;
r.top = r.left = 0;
r.bottom = h;
r.right = w;
HBRUSH br = ::CreateSolidBrush(RGB(255,0,0));
::FillRect(hdc, &r, br);
// Release the DC
img.ReleaseDC();
// now save the image
img.Save("c:/temp/testimg.jpg", Gdiplus::ImageFormatJPEG);
I get a black rectangle file with the correct dimensions.
I am sure I am doing something dumb, but any help would be
appreciated.
TIA.
|
|
|
|
|
If CImage is a GDI+ class ( I don't remember, but it looks like it, I thought it was just called Image ), then you need to select it into a Graphics object and draw on it there.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Hi, I'm trying to create a simple Listbox, using Win32, no MFC.
void CreateSpilList()
{
ProgramList = CreateWindow (TEXT ("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
5, 5,
100,
100,
hwnd, (HMENU)10,
GetModuleHandle(NULL),
NULL) ;
}
I've created a simple function that should create the listbox.
case WM_CREATE:
{
CreateSpilList();
break;
}
The WM_CREATE Message should create my Listbox, but it wont, nothing happens.
But, the strange thing is, if i remove CreateSpilList(); and inserts whats in the function:
like so.
case WM_CREATE:
{ ProgramList = CreateWindow (TEXT ("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
5, 5,
100,
100,
hwnd, (HMENU)10,
GetModuleHandle(NULL),
NULL) ;
break;
}
It works, why can't I create my listbox by using a function like my CreateSpilList() ???
It make no sense, even if i inline the CreateSpilList() nothing happens.
Can anyone give me an answer on this strange "bug" ??
Thanks
|
|
|
|
|
is the hwnd parent window "global" or only local to the function that contains the WM_CREATE ?
Max.
|
|
|
|
|
The hwnd is global i think, it's the parent.
|
|
|
|
|
I am trying to write a Control-Panel-like application with a background bitmap, kind of like a desktop picture. I want to put Icons on it, but when I do, I can still see the tan background of the window in the Icon background. By the way, the application is dialog-based. How do I get rid of that?
-- Steve
|
|
|
|
|
you need to use a color key and either use TransparentBlt, or ( if you need W95 support ), check out the replacement I wrote for Windows Developers Journal a year or two ago.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
I searched all over the web for a code to mute the microphone and I can't find one.
I need a code that will mute the microphone just like muting on the Mixer
Any idea?
Thanks.
|
|
|
|
|
|
I'm writing a practice-program that sends key press to another window (obtained HWND of that window already). I've looked into MSDN for WM_KEYDOWN and WM_KEYUP messages but got a little confused by the sheer complexity of wParam and lParam. I think I just need a little real example (which I failed to find any) to get ahold of that.
Could someone please gimme the sample code which will send key press for character '?'(question mark) to a window, suppose hTestWnd is the valid HWND of that window. Thanks a lot.
|
|
|
|
|
::SendMessage(hTestWnd, WM_CHAR, (WPARAM) '?', (LPARAM) 0);
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Great, how about sending those special keys, say, VK_F12?
Thank you again.
|
|
|
|
|
I'm relying on the implicit conversion of int to char here, I suspect the second parameter is where you'd specify if it was an F key, unless that's one of the keys sent with the SYSKEY stuff. I really don't know - how would you see an F12 in an edit box anyhow ?
http://msdn.microsoft.com has full docs on WM_CHAR, that's where I looked in the first place.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Yeah I know i can't see F12... but as I said at beginning I was doing some practice so it's just that, a practice.
Anyway, I will dig into MSDN again and thanks very much for your help.
|
|
|
|
|
Can someone show me how to use MCI control on a dialog application? I need to:
(a) Play an avi file with movie window frame within the dialog itself...
(b) And I also need to STOP at the last frame.
I downloaded a sample from MSDN, but it's neither an SDI nor dialog application. I know that the VB code for the control is just a few lines - hope it's the same for VC.
Help!
norm
|
|
|
|
|
I'm trying to find away to print the flexgrid with the lines. Is there away of doing this.
|
|
|
|
|
Hi All,
I'm developing an MFC application using Doc/View and within this application I am making calls to a DLL. My problem is the DLL is asking for the:
Current window handle
Parent window handle
current HINSTANCE
How the heck do I find these things in MFC? I'm trying to get this information in a class that is derived from CFormView. Any help in this matter would be extremely helpful. Thank you in advance
Danielson
Slower is Faster
|
|
|
|
|
Current Window Handle -> *this or this->m_hWnd
Parent Window Handle -> *(this->GetParent());
Current HInstance -> AfxGetInstanceHandle();
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|