![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
That completes our overview of CFtpConnection. Next we look at the Web browser control.
Creating a Web BrowserIn Chapter 2, Multiple Documents, Multiple Views, we used HTML views to create a quick Web browser. We can use the Web browser control to do the same thing using controls, not views, and we do that now. Create a new project named Browser, making it a dialog-based project. Next, install the Web browser control in the dialog box that makes up our main window. Installing the Web Browser ControlOpen the main dialog, IDD_BROWSER_DIALOG, in the Dialog Editor, as shown in Figure 8.3. To add the Web browser control, select the Project menus Add to Project item, opening the Components and Controls submenu item. This opens the Visual C++ Components and Controls Gallery. Double-click the folder marked Registered ActiveX Controls, find the Microsoft Web Browser item, and click the Insert button. This inserts the Web Browser tool in the control toolbox. Use that tool now to add a new browser control to the dialog window, sizing it so that it matches the one in Figure 8.3. In addition, we use a text box so the user can specify the URL to navigate to and a button to start the navigation. Add those controls, giving the button the caption Navigate, as shown in Figure 8.3. Finally, use ClassWizard to connect a member variable, m_text, to the text box; add a handler function to the button, OnButton1(); and connect a member variable, m_browser, to the Web browser control, IDC_EXPLORER1.
We start the code by updating the member variable m_text with UpdateData(). This variable will hold the URL to navigate to, as entered by the user. void CBrowserDlg::OnButton1() { UpdateData(true); ⇐ . . . } Now that we have the URL to navigate to, we use the m_browser controls Navigate() function to navigate to that new URL. (The last four parameters are pointers to headers and flags that we wont use, so set to 0). void CBrowserDlg::OnButton1() { UpdateData(true); m_browser.Navigate(m_text, 0, 0, 0, 0); ⇐ } Thats all it takes. Run the Web browser now and navigate to the Visual C++ home page by entering its URL and clicking the Navigate button. As you can see in Figure 8.4, our navigation is successful. Were now using the Web browser control and writing Web browsers. The code for this example, BrowserDlg.h and BrowserDlg.cpp, appears in Listing 8.3. Listing 8.3 BrowserDlg.h and BrowserDlg.cpp // BrowserDlg.h : header file // //{{AFX_INCLUDES() #include webbrowser2.h //}}AFX_INCLUDES #if !defined(AFX_BROWSERDLG_H__B788EA07_A5FC_11D1_887F_D42B07C10710__INCLUDED_) #define AFX_BROWSERDLG_H__B788EA07_A5FC_11D1_887F_D42B07C10710__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 ///////////////////////////////////////////////////////////////////////////// // CBrowserDlg dialog class CBrowserDlg : public CDialog { // Construction public: CBrowserDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CBrowserDlg) enum { IDD = IDD_BROWSER_DIALOG }; CWebBrowser2 m_browser; CString m_text; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CBrowserDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CBrowserDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); virtual void OnOK(); afx_msg void OnButton1(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_BROWSERDLG_H__B788EA07_A5FC_11D1_887F_D42B07C10710__INCLUDED_) // BrowserDlg.cpp : implementation file // #include stdafx.h #include Browser.h #include BrowserDlg.h #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CBrowserDlg dialog CBrowserDlg::CBrowserDlg(CWnd* pParent /*=NULL*/) : CDialog(CBrowserDlg::IDD, pParent) { //{{AFX_DATA_INIT(CBrowserDlg) m_text = _T(); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CBrowserDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CBrowserDlg) DDX_Control(pDX, IDC_EXPLORER1, m_browser); DDX_Text(pDX, IDC_EDIT1, m_text); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CBrowserDlg, CDialog) //{{AFX_MSG_MAP(CBrowserDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnButton1) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CBrowserDlg message handlers BOOL CBrowserDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add About... menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CBrowserDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CBrowserDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CBrowserDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CBrowserDlg::OnOK() { // TODO: Add extra validation here CDialog::OnOK(); } void CBrowserDlg::OnButton1() { UpdateData(true); m_browser.Navigate(m_text, 0, 0, 0, 0); } Whats AheadIn the next chapter, we start working with another popular aspect of Visual C++ programming: multithreaded, multitasking programs. Multitasking allows your program to appear to do several things at once, handling tasks in the background while the user is doing something else. We turn to that topic now.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |