Use Socket and the MSHTML Object Model to create their own browser
Of the Central University of Finance and Management Information Department of Xue Ying
HTTP protocol and the birth of WEB browser to our network of adding more exciting. However, in practical applications, we may have different needs, rather than simply use the browser, such as in our application by adding functionality to browse the Internet. Microsoft ChtmlView class implements a convenient web browser. But it is very flexible to allow users to dynamically change the page element you want. This article discusses some ways to use Socket transport Html documents, using Microsoft's dynamic MSHTML Object Model of the internal mechanisms of some browsers.
Let us give some examples to explain the application of MSHTML. In the example, first, before I talk about Socket and HTTP protocols. TCP connect to a server via HTTP protocol and the Client, it works on port 80. Between the Client and the Server through the HTTP request / response mechanism to communicate. HTTP messages are divided into Request and Response. Message from the start of each line, the message headers and message body composition. Form are as follows:
generic-message = start-line
start-line of the form are as follows:
start-line = Request-Line | Status-Line
Request-Line for the Client to the Server to issue the request form are as follows:
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
Method, including GET, POST and so on, in this case, we only use a simple GET request to the server.
HTTP protocol in detail please refer to RFC2068.
In the vc + + to create a new EXE project single-document files, in order to use MSHTML our view class inherits from the ChtmlView. ChtmlView class encapsulates the MSHTML interface. To the project by adding Socket class.
class CHttpSocket: public CSocket
... ... ... ... ... ...
CWnd * m_pParentWnd;
Which m_pParentWnd point to our view class, used to deliver the message. In the view class is defined Socket.
class CSkhttpView: public CHtmlView
protected: / / create from serialization only
... ... ... ... ....
IHTMLDocument2 * phmDoc2;
... ... ..
phmDoc2 for IHTMLDocument2 interface. Initialization Socke, we want to log on to connect the site, assumed to be www.163.net.
CSkhttpView:: CSkhttpView ()
/ / TODO: add construction code here
BOOL bRet = m_socket.Create (0, SOCK_STREAM, NULL);
if (! bRet)
MessageBox ( "socket create error", NULL, MB_OK);
if (! m_socket.Connect (www.163.net, 80))
MessageBox ( "socket connect error", NULL, MB_OK);
Here we have made IhtmlDocument2 interface. access to this interface are generally two ways. One is to use CoCreateInstance, then call QueryInterface. Another is to use the MSHTML control object get_Document, in the ChtmlView class encapsulates this interface. We use the latter. Note that, we need to ChtmlView build IhtmlDocument object to get its interface.
void CSkhttpView:: OnInitialUpdate ()
Navigate2 ( "about: blank");
void CSkhttpView:: OnDocumentComplete (LPCTSTR lpszURL)
lpdisp = GetHtmlDocument ();
hr = lpdisp-> QueryInterface (IID_IHTMLDocument2, (void **) & phmDoc2);
The ChtmlView initialization call Navigate2 () create an empty document, the document after the election call GetHtmlDocument get a Idispatch () interface, call QueryInterface to obtain IHTMLDocument2 interface.
void CSkhttpView:: OnReceiveMessage (WPARAM wParam, LPARAM lParam)
char buf ;
IHTMLElement * pEleBody;
if (wParam == 0)
inum = m_socket.Receive (buf, sizeof (buf), 0);
buf [inum] = 0;
_bstr_t bsrBody (buf);
hr = phmDoc2-> get_body (& pEleBody);
hr = pEleBody-> put_innerHTML (bsrBody);
void CSkhttpView:: OnControlSend ()
/ / TODO: Add your command handler code here
char buf ;
wsprintf (buf, "GET http://www.163.net HTTP/1.1 \ r \ n \ r \ n");
int iRet = m_socket.Send (buf, lstrlen (buf), 0);
if (iRet == SOCKET_ERROR)
MessageBox ( "socket send error", NULL, MB_OK);
We send the HTTP protocol to the server the request "GET http://www.163.net HTTP/1.1 \ r \ n \ r \ n", it is to retrieve the specified URI address of the web page. Issued a response to our server through Socket accept. IhtmlDocument2 contains many objects an excuse by many put_, get_ way we can get the properties of these objects, events and methods. In this case we get body object, which corresponds to the HTML text block in the <body> </ body> object. By IhtmlDocument2 of get_body () to be the designated body object IHTMLElement interface, call IHTMLElement interface put_innerHTML () will receive the content into the Document in. At this point in our view, it will display the page.
This is just a brief introduction to the look MSHTML object which actually contain a lot of interfaces and functions, through any of these interfaces that we can design our own style browser. The comrades who are interested can refer to MSDN documentation.
Visual Basic Tutorial Articles
- VB5.0 create a screen saver mystery
- Mixed programming article in English voice output
- Use Socket and the MSHTML Object Model to create their own browser
- Extraction of HTML files using VB in the EMAIL address
- API defines how to program hotkeys
- To be a "Network Assistant" program
- VB and XML with the establishment of centralized applications
- Use VB to achieve Excel automatically access external data
- In VB use DirectShow to play AU files
- A simple and effective database security law
- VB in Combo constraints of an input method to check the realization of
- With VB in a window display animation icon
- The use of text in VB to read the engine (TTS) technology
- Visual Basic Environment Video / Audio playback data stream compression technology
- Using VB to achieve full-screen graphical interface and dynamic features tips
- How to open or close the CD-ROM
- Operate without borders in VB form
- VB application in the use of appropriate voice
- Visual Basic with multi-screen playback
- Using VB to achieve chat rooms and peer-to-peer session to discuss
Can't Find What You're Looking For?
Rating: Not yet rated