?
Solved

How to write a program in borland C++, such like notepad can open the file directly

Posted on 2003-02-26
14
Medium Priority
?
1,043 Views
Last Modified: 2012-05-05
How to write a program, which the description shows as below, in Borland C++ 5.0 or above?

The description is follow:

When you want to open a text file, such as abc.txt,

1. How to write a program when you double click the text file, the file will be opened in the program.

And

2. How to write a program when you highlight the text file, and then right click to select open with, the file will be opened in the program.

The program is something like notepad, which the file can directly open by drag and drop into notepad or open with notepad.
0
Comment
Question by:georgelcf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8024971
It is a direct violation of the EE membership agreement for experts to do homework problems.
0
 

Expert Comment

by:kruemelmo
ID: 8025061
Exceter, how do you come to the conclusion this is homework?

-- Moritz
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 8027816
1)

To get you going..

Two bits. Registry settings to get it to launch your application. Then your application needs to read command line arguments and open the file supplied.
Registry settings can be put in place by right-click and open-with, then choosing your application and checking the 'always with this one...'. Alternatively installers are good at setting up these connections, alternativley you need to create entries in LOCAL_MACHINE in the registry - comment back if this is the way to go.
To get your application to read the command line, use ... ParamCount and ParamStr or whatever to look at the command line...

You'll need to be able to get it working so you can drag/drop a file onto your executable in Explorer. That also uses command line argument passing. Then worry about getting the file association worked out..

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Exceter
ID: 8028230
>> Exceter, how do you come to the conclusion this is homework?

To be honest I do not even remeber posting here. It was late at night and since I keep three or four browser windows open at any given time it is quite possible I posted on the worng thread. :-(

Sorry about that.

Exceter
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8028420
>> worng

wrong
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8029166

Hi Georgeclf,

Borland's C++ will do a lot of this work for you.  It's been a while since I've used it, so please bear with me.

Create a new application -- probably an SDI app.  C++ will allow you to select quite a few options pertaining to the application.  One of them that you'll want to select is "Drag and Drop".

You can then use the Resource Editor to create the file bar, put a Tedit or TRichEdit object in your application, etc.

I don't recall the steps that you have to take to create a TOpenDialogBox.  But if you can master the steps above this one should fall into place.


All that being said, have you tried to upgrade from C++ to their C++ Builder?  It's a completely different environment.  The simple program that you're trying to develop with C++ can be created in the Builder in 2 or 3 minutes.


Kdo
0
 

Author Comment

by:georgelcf
ID: 8030583
I can drag & drop and open the file when i executed my program. Refer to your comment and my question.

1. How to write into registry? which location should be written in registry?

2. Besides writing in registry, have any method can do it?

There is my code.

Unit1.cpp
//----------------------------
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WMDropFiles (TWMDropFiles &message)
{
    String filename ;
    filename.SetLength (MAX_PATH) ;
    int length = DragQueryFile ((HDROP) message.Drop,
                                 0,
                                 filename.c_str (), filename.Length ()) ;
    filename.SetLength (length) ;

    // Extract the file extension.
    String extension ;
    extension = filename.UpperCase ().SubString (filename.Length () - 3, 4) ;
    if (extension == ".TXT")
    {
        RichEdit1->Lines->LoadFromFile (filename) ;
    }
  DragFinish ((HDROP) message.Drop) ;
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  DragAcceptFiles (Handle, True) ;
}
//---------------------------

Unit1.h

//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include <vcl\Menus.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:     // IDE-managed Components
        TRichEdit *RichEdit1;
  void __fastcall FormCreate(TObject *Sender);

private:     // User declarations
  void virtual __fastcall WMDropFiles (TWMDropFiles &message);
  public:          // User declarations
  __fastcall TForm1(TComponent* Owner);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DROPFILES,TWMDropFiles,WMDropFiles)
END_MESSAGE_MAP (TForm) ;
};
//---------------------------------------------------------------------------
extern TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
// ------------------------

project.cpp

//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("Unit1.cpp", Form1);
USERES("DragFiles.res");
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
  try
  {
    Application->Initialize();
    Application->CreateForm(__classid(TForm1), &Form1);
                 Application->Run();
  }
  catch (Exception &exception)
  {
    Application->ShowException(&exception);
  }
  return 0;
}
//------------------------------------------



0
 
LVL 12

Expert Comment

by:andrewjb
ID: 8032613
No - don't need the DragAccept stuff. You need to look at the command line, as I said.
You need to be able to handle drag/drop in EXPLORER, onto the executable FILE - NOT the running application.
Explorer then launches the .exe with the dropped file on the command line.

So, in FormShow, something like:

if ( FirstShow )
{
  FirstShow = false;
  if ( ParamCount() > 1 )
  {
    if ( FileExists( ParamStr(1)) )
    {
      RichEdit->Lines->LoadFromFile( ParamStr(1));
    }
  }
}

Roughly - without trying it!
Can't rememeber whether ParamCount and ParamStr include the executable name - you'll need to look it up in help!


Get that going first, then we'll get the stuff into the registry for you.


Andrew
0
 

Author Comment

by:georgelcf
ID: 8038888
The above of code seems to be not work.
Even i changed ParamStr(1) to ParamStr(0).

How to we know this FormShow is executed?

regards,

George
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8039029

Borland provides several classes for reading / writing the registry file and ini files.  The classes are structured so that you can easily change a program from using an ini file to using the registry by instantiating a different class.  The member functions are very similar in the different classes and in some cases are the same.

Classes to look for include TIniFile, TMemIniFile, TRegistryIniFile, TRegIniFile, and TCustomIniFile.


Here's how to use them:

// Set the default "section"
// In an ini file sections are separated with brackets
// We're looking for [Defaults]

// To search the registry instead of an ini file, set
// SectionName to the directory name in the registry
// and instantiate the correct class.

  char *SectionName = "Defaults"

// Open the ini file for the current application.
 
  TIniFile *R = new TIniFile ("MyApplication.ini");

// Set the default file name into a TEditBox

  LoadFileBox->Text = R->ReadString (SectionName, "LastDeadstartFile", "");

// Set a default integer value

  RunCount = R->ReadInteger (SectionName, "RunCount", 0);

// Increment and save the run count

  R->WriteInteger (SectionName, "RunCount", ++RunCount);

// Close the file

  delete R;


Good Luck,
Kent
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 8041059
The code needs to go in the Forms OnShow event handler...
You'll also need a boolean variable FirstShow which you set to 'true' in the OnCreate event handler of the form...
0
 

Author Comment

by:georgelcf
ID: 8046127
How do i do in registry which can use "open with" operation to open file?
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 280 total points
ID: 8056053
0
 

Author Comment

by:georgelcf
ID: 8056808
Thanks for helping my problem.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question