Use the webbrowser control to navigate to a page.
You can use webbrowser.doc ument. invoke to call Javascript in document.
My goal is to save the page. I started to use the document.exe ccommand method. This function accepts many parameters, corresponding to various menu commands in the browser, such as saving and printing.
The English name of these command parameters is command identifier. The specific parameter list is described in detail on the following page. I will attach them to the end by the way.
(Http://www.tutor.nsu.ru/library/default.asp? Url =/Workshop/author/DHTML/reference/commandids. asp)
I used Execcommand ("saveas", true, PATH) to save.
In this document, the saveas is described as follows: its 2nd parameters are boolean values. If it is set to true, the Save As dialog box is displayed. If it is set to false, the dialog box is not displayed.
However, whether set to true or false, a dialog box is displayed. Another article is about this to prevent mass copying of webpages and stealing of others' data.
Haha, it's funny. Can this small parameter assume such a major responsibility?
Since this window will certainly appear, how can we solve it. I want to use a program to simulate a person's click action. This is what we do. You can also use other features to find and save the dialog box by searching for the title,
After finding the dialog box, locate the Save button in the dialog box. You can use findwindow for both searches. After finding it, send a message to this dialog box. sendmessage is used, and wm_click is used for the message.
Because I use C #, while findwindow and sendmessage are not managed code, how can I use them in C?
The following article introduces this knowledge.
Use C # Call Windows API to communicate with other processes (http://www.cnblogs.com/index/archive/2005/01/16/92651.html)
The key is to introduce these unmanaged codes. Use the following statement.
# Region DLL Import
[Dllimport ("user32.dll", entrypoint = "findwindow")]
Private Static extern intptr findwindow (string lpclassname,
String lpwindowname );
[Dllimport ("user32.dll", entrypoint = "find1_wex")]
Private Static extern intptr find1_wex (intptr hwndparent,
Intptr hwndchildafter, string lpszclass, string lpszwindow );
[Dllimport ("user32.dll", entrypoint = "sendmessage")]
Private Static extern int sendmessage (intptr hwnd,
Int MSG, intptr wparam, string lparam );
# Endregion
I wrote the find window and save button and the Code for sending and clicking the message in another thread function to start a new thread to run this function.
When the program starts, it starts the thread and keeps monitoring whether such a dialog box appears.
However, this method does not work well and does not run fast. In addition, because I am not familiar with the thread, this problem always occurs during running:
Try to read or write the protected memory. This usually indicates that other memory is damaged.
It has never been solved.
Finally, I found another method. Instead of calling the Save dialog box, I saved the HTML code of the page to the file.
This idea is intuitive. In fact, I have been thinking about this for a long time, but at first I used the docuent. Write () method to write the code. I do not know whether the HTML code of the page exists.
In which attribute of the Document Object. So there is no way to do it.
The following code demonstrates how to use this method to save data. It is super simple and intuitive.
Htmldocument Doc = webbrowser1.document;
Htmlelementcollection elems = Doc. getelementsbytagname ("html ");
If (elems. Count = 1)
{
Htmlelement ELEM = elems [0];
Streamwriter WRT = new streamwriter (PATH );
WRT. Write (ELEM. outerhtml. tostring ());
}
In this way, my task is completed.
The values of some messages in sendmessage are defined in Windows API.
Const int wm_gettext = 0x000d;
Const int wm_settext = 0x000c;
Const intWm_click= 0x00f5;
Const int wm_close = 0x0010
Attach command identifiers
Command identifiers specify an action to take on the given object. Use them with the following methods:
Execcommand
Querycommandenabled
QueryCommandIndeterm
QueryCommandState
Querycommandsupported
QueryCommandValue
Command identifiers
2d-position
Allows absolutely positioned elements to be moved by dragging.
Absoluteposition
Sets an element's position property to "absolute ."
Backcolor
Sets or retrieves the background color of the current selection.
Blockdirltr
Not currently supported.
Blockdirrtl
Not currently supported.
Bold
Toggles the current selection between bold and nonbold.
Browsemode
Not currently supported.
Copy
Copies the current selection to the clipboard.
Createbookmark
Creates a bookmark anchor or retrieves the name of a bookmark anchor for the current selection or insertion point.
CreateLink
Inserts a hyperlink on the current selection, or displays a dialog box enabling the user to specify a URL to insert as a hyperlink on the current selection.
Cut
Copies the current selection to the clipboard and then deletes it.
Delete
Deletes the current selection.
Dirltr
Not currently supported.
Dirrtl
Not currently supported.
Editmode
Not currently supported.
Fontname
Sets or retrieves the font for the current selection.
Fontsize
Sets or retrieves the font size for the current selection.
Forecolor
Sets or retrieves the foreground (text) color of the current selection.
Formatblock
Sets the current block format tag.
Indent
Increases the indent of the selected text by one indentation increment.
Inlinedirltr
Not currently supported.
Inlinedirrtl
Not currently supported.
Insertbutton
Overwrites a button control on the text selection.
Insertfieldset
Overwrites a box on the text selection.
Inserthorizontalrule
Overwrites a horizontal line on the text selection.
Insertiframe
Overwrites an inline frame on the text selection.
Insertimage
Overwrites an image on the text selection.
Insertinputbutton
Overwrites a button control on the text selection.
Insertinputcheckbox
Overwrites a check box control on the text selection.
Insertinputfileupload
Overwrites a file upload control on the text selection.
Insertinputhidden
Inserts a hidden control on the text selection.
Insertinputimage
Overwrites an image control on the text selection.
Insertinputpassword
Overwrites a password control on the text selection.
Insertinputradio
Overwrites a radio control on the text selection.
Insertinputreset
Overwrites a Reset Control on the text selection.
Insertinputsubmit
Overwrites a submit control on the text selection.
Insertinputtext
Overwrites a text control on the text selection.
Insertmarquee
Overwrites an empty Marquee on the text selection.
Insertorderedlist
Toggles the text selection between an ordered list and a normal format block.
Insertparagraph
Overwrites a line break on the text selection.
Insertselectdropdown
Overwrites a drop-down Selection control on the text selection.
Insertselectlistbox
Overwrites a list box Selection control on the text selection.
Inserttextarea
Overwrites a multiline text input control on the text selection.
Insertunorderedlist
Toggles the text selection between an ordered list and a normal format block.
Italic
Toggles the current selection between italic and nonitalic.
Justifycenter
Centers the format block in which the current selection is located.
Justifyfull
Not currently supported.
Justifyleft
Left-justifies the format block in which the current selection is located.
Justifynone
Not currently supported.
Justifyright
Right-justifies the format block in which the current selection is located.
Liveresize
Causes the mshtml editor to update an element's appearance continuously during a resizing or moving operation, rather than updating only at the completion of the move or resize.
Multipleselection
Allows for the selection of more than one site selectable element at a time when the user holds down the shift or Ctrl keys.
Open
Not currently supported.
Outdent
Decreases by one increment the indentation of the format block in which the current selection is located.
Overwrite
Toggles the text-Entry Mode between insert and overwrite.
Paste
Overwrites the contents of the clipboard on the current selection.
Playimage
Not currently supported.
Print
Opens the Print dialog box so the user can print the current page.
Redo
Not currently supported.
Refresh
Refreshes the current document.
Removeformat
Removes the formatting tags from the current selection.
Removeparaformat
Not currently supported.
Saveas
Saves the current web page to a file.
Selectall
Selects the entire document.
Sizetocontrol
Not currently supported.
Sizetocontrolheight
Not currently supported.
Sizetocontrolwidth
Not currently supported.
Stop
Not currently supported.
Stopimage
Not currently supported.
Strikethrough
Not currently supported.
Subscript
Not currently supported.
Superscript
Not currently supported.
Unbookmark
Removes any bookmark from the current selection.
Underline
Toggles the current selection between underlined and not underlined.
Undo
Not currently supported.
Unlink
Removes any hyperlink from the current selection.
Unselect
Clears the current selection.