Use mfc to open txt, web pages and office

I encountered some problems and learned some new things. Record them.

Recently, a new function needs to be developed in the project, which accepts client instructions and then opens some commonly used software: text, web pages, and office software.

I have done a project before, which is the operation of the web page. At that time, I used the ShellExecute function to solve it. The ShellExecute function is used to run an external program. The second parameter is set to operation, we set it to "open", and the third parameter For the path where the application is located, what we want to open here is the web page, you can directly enter the website name, for example, if you want to open Baidu, set it to "http://www.baidu.com", and the last parameter is set to SW_SHOWNORMAL to let the program display, i.e.:

ShellExecute(NULL, "open", "http://www.baidu.com", NULL, NULL, SW_SHOWNORMAL);

This will open a browser.

I also want to use this function to create and open txt, so my idea is like this:

First, use CFile to create a .txt file. In order to create multiple times, we need to avoid duplicating names. In the dialog class, declare a member variable int TxtNum to record the number of txt that has been created. The name of the first created txt is "New" Text document.txt", the second is called "New text document (2).txt", and so on, you can use the ShellExecute function to open it, and it works well.

The last is Office, so can Office also use this simple method? There is a reason why I have this doubt. There have been several times before that the .m file suffix created by CFile is correct, but it cannot be opened with MatLab. So I did an experiment on my computer. I installed office2010 on my computer. The suffix of Word is ".docx", ppt is ".pptx", and Exel is ".xlsx". I use the above method to create files And open the following discovery: word, ppt can be, but when Exel tries to open it, it shows that the file is damaged, and the suffix name is changed to ".xls" and a dialog box pops up: "The format of the file xxxx. The format specified by the extension is inconsistent, and it is still easy to use after clicking "Yes".

However, I think this is still a bug. It is not good for the customer to see, so I changed the method to achieve it, using COM to achieve it. It is actually very simple, but it is very troublesome to load. It took a long time to find the method here. Take exel as an example, everything else is similar.

Before we do it, we need to be clear about the purpose, just to complete a simple open operation, no other functions are required, so just use the two classes CApplication and CWorkbooks, so we need to add the corresponding header file, it is recommended not to manually add it, I use The development environment is vs2010, please translate the English version yourself, click Project -> Add Class, a dialog box will pop up, double-click the MFC class in TypeLib, and then select the registry as the source in the pop-up dialog box, and you can find Microsoft Exel on the right. . . . . . . , (the operation of word and ppt is the same, the name is the corresponding word and powerpoint), the interface only needs _Application and workbooks, click ">" and then click Finish. Open the corresponding .h file and import the first sentence. . . . . . . . . . . To remove it, add the header file where it is used.

Then implement the initialization, and add AfxOleInit() to the InitInstance() function in the app class (this has to be rewritten, many online will not be repeated here).

We need to accept the instructions in the thread that reads the socket, and then send a message to the main thread to open the exel, define a message by ourselves, and then send the message to SendMessage. The corresponding function of the message implements the opening function. The code is as follows:

CApplication app;  
CWorkbooks books;  
 if(!app.CreateDispatch("Excel.Application"))  
  {  
 AfxMessageBox("Unable to start Excel server!");  
return -1;  
   } 

app.put_Visible(TRUE); books.AttachDispatch(app.get_Workbooks());  books.Add(vtMissing); return 0;





app.put_Visible(TRUE);This sentence is necessary, otherwise it will not be displayed, and the effect will not be seen. The vtMissing is literally well understood: the default value, the parameter in the Add function is a template you want to open exel, we don't need to be so complicated, You can directly use the system default template, so use vtMissing, which is included in the header file, do not declare it yourself.

The effect is a pop-up dialog box "Cannot start Excel server!", Ben tears.

A closer look at the debug output shows:

Most likely exception at 0x7691c54f: The outgoing call cannot be performed because the application is sending an incoming sync call.

Warning: CreateDispatch returning scode = RPC_E_CANTCALLOUT_ININPUTSYNCCALL ($8001010D).

This error is obvious. When SendMessage sends a message, it will not return immediately, blocking the action of CreateDispatch, so you can change SendMesaage to PostMessage. PostMessage is only responsible for sending the message, and then returns immediately without blocking .

After running, the effect is much better, no error dialog box will appear, and don't open the app and bookRealse or Close, otherwise Exel will be turned off immediately after opening, and cannot be operated.

There may be many ideas to solve the same problem. Different ideas solve the problem efficiently and have different effects. Some ideas are very simple to implement and have high efficiency, but they may only be suitable for some situations, and other complex situations may not be able to. Processing, some are more complicated to implement, but because the consideration is more comprehensive, there are fewer errors, so you need to think about different ideas when programming in the future, so as to improve the applicability of the program.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529049&siteId=291194637