?
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,079 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

578 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