⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainwnd.cpp

📁 wince下internet explorer的浏览器的简单实现
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    _hWndStatus = NULL;
    _hWndProgress = NULL;
#endif	//NOSTATUSBAR

    _bTyped = FALSE;

    _szTitle[0] = 0;
    _fEmpty = FALSE;
#ifndef NO_FAVORITES
        _hMenu = NULL;
	_nNumFavorites = 0;
	_nLastFavoriteID = ID_FAVORITE_FIRST;
	_pIFavStorage = (CRegFavorites*) new CRegFavorites;
#endif
	
}

CMainWnd::~CMainWnd()
{

RETAILMSG(1,(L"IESAMPLE Exiting ~CMainWnd\r\n"));
        if(_himlCmdBand)
                ImageList_Destroy(_himlCmdBand);

        if(_pBrowser)
            _pBrowser->Release();

#ifndef NO_FAVORITES    
	if (_pIFavStorage)
		delete _pIFavStorage;
#endif	
}   

/////注册主窗口。在winmain()中
BOOL RegisterMainWnd()
{
    WNDCLASS        wc;
    wc.style         = 0;
    wc.lpfnWndProc   = (WNDPROC)CMainWnd::MainWndProc;//主窗口处理函数
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = g_hInstance;
    wc.hIcon         = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IE));

    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_WINDOW);//背景色
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = TEXT("IESAMPLE");  //窗口类名

    if (!(RegisterClass(&wc)))
        return FALSE;

    return TRUE;
}


//主窗口的创建函数
BOOL CMainWnd::Create()
{
    RECT rcArea, rcWindow;

    DWORD dwStyle = WS_VISIBLE;/*|WS_OVERLAPPED|WS_BORDER|WS_CAPTION   //窗口风格
                                 |WS_SYSMENU|WS_MINIMIZEBOX*/
    DWORD dwExStyle = 0/*WS_EX_OVERLAPPEDWINDOW*/;                     //扩展窗口风格

    HMENU hMenu = NULL;


    _hWnd = ::CreateWindowEx(dwExStyle,                               //创建主窗口
                             TEXT("IESAMPLE"),
                             LOADSTRING(IDS_IE),
                             dwStyle,
                             CW_USEDEFAULT,
                             0,
                             CW_USEDEFAULT,
                             0,
                             NULL, hMenu, g_hInstance, 0);
    if (!_hWnd)
        return FALSE;

    SetWindowLong(_hWnd, GWL_USERDATA, (DWORD)this);   //保存变量this,也就是CMainWnd的指针

    SystemParametersInfo(SPI_GETWORKAREA, 0, &rcArea, 0);
    GetWindowRect(_hWnd, &rcWindow);                   //得到窗口尺寸到_rcWnd

#ifndef NOCOMMANDBAR
    if (!CreateCommandbar())
        return FALSE;

    _pthrdAnimation = new CAnimThread(this);
    if (!_pthrdAnimation)
        return FALSE;

    _pthrdAnimation->StartThread();

#endif	//NOCOMMANDBAR

#ifndef NOSTATUSBAR
    if (!CreateStatusBar())
        return FALSE;
#endif	//NOSTATUSBAR

    if (!(_hWndBrowser = CreateBrowser()))    //创建浏览器控件窗口
        return FALSE;

    LONG lStyle = GetWindowLong(_hWndBrowser, GWL_STYLE);
    SetWindowLong(_hWndBrowser, GWL_STYLE, lStyle|WS_BORDER);
 
	SetFocus(_hWnd);
    if (_pIPActiveObj) // by yuyang
    {
        _pIPActiveObj->OnFrameWindowActivate(TRUE);
    }

    _hAccelTbl = LoadAccelerators(g_hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR));

    return TRUE;
}
  
#ifndef NOCOMMANDBAR

BOOL CMainWnd::CreateCommandbar()  //建立命令工具条
{
    TBBUTTON tbSep = {0, IDC_STATIC, 0, TBSTYLE_SEP, 0, 0, 0, -1};

    RECT rc;
    GetClientRect(_hWnd, &rc);
    

    HBITMAP hbm;
    
    _himlCmdBand = ImageList_Create(11, 13, ILC_COLOR, 1, 0);
	    hbm = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_COOLBARIMAGES));	
    if (hbm) {
           ImageList_Add(_himlCmdBand, hbm, NULL);
	    DeleteObject(hbm);
    }

    _hWndCmdband = CommandBands_Create(g_hInstance, _hWnd, (UINT)ID_COMMANDBAND,
                                       RBS_SMARTLABELS|RBS_BANDBORDERS|RBS_AUTOSIZE, _himlCmdBand);
    REBARBANDINFO rbbi[3];


    // Create the Address window
    _hWndAddressCombo = CreateWindowEx(0, TEXT("combobox"), TEXT(""), 
										 WS_VISIBLE | WS_CHILD |WS_TABSTOP| CBS_AUTOHSCROLL,
										 0, 0, 20, 110, _hWnd, 
										 (HMENU)ID_ADDRESSBAR,
										 g_hInstance, NULL);

    
    LoadTypedUrls();

    _hWndAddressEdit = GetDlgItem(_hWndAddressCombo, ID_COMBOEDIT);
    if(_hWndAddressEdit)
    {
    	ghWndAddressEdit = _hWndAddressEdit;

    	SetWindowLong(_hWndAddressEdit, GWL_USERDATA, (DWORD)this);

    	_lpfnAddressEditProc = (WNDPROC )SetWindowLong(_hWndAddressEdit,
                                                   GWL_WNDPROC, (LONG)AddressEditProc);
    	SendMessage(_hWndAddressEdit, EM_LIMITTEXT, MAXLEN-1, 0L);
    }

    // Menubar
    rbbi[0].cbSize = sizeof(REBARBANDINFO);
    rbbi[0].fMask = RBBIM_ID|RBBIM_IMAGE|RBBIM_SIZE; 
    rbbi[0].cx = 250;  // want to be able to size this automatically
    rbbi[0].wID = ID_BAND_CMD;
    rbbi[0].iImage = 1;

    // Toolbar
    rbbi[1].cbSize = sizeof(REBARBANDINFO);
    rbbi[1].fMask = RBBIM_ID|RBBIM_IMAGE|RBBIM_SIZE;
    rbbi[1].wID = ID_BAND_TOOL;
    rbbi[1].iImage = 0;
    rbbi[1].cx = rc.right - (ANIMATIONWIDTH + 250);
  // want to be able to size this automatically

    // Address
    WCHAR szAddressLabel[40];
    RECT rcComboBox;
    GetWindowRect(_hWndAddressCombo, &rcComboBox);
    

    LoadString(g_hInstance, IDS_ADDRESS, szAddressLabel, 40);		
    rbbi[2].cbSize = sizeof(REBARBANDINFO);
    rbbi[2].fMask = RBBIM_CHILD | RBBIM_TEXT | RBBIM_ID | RBBIM_STYLE | 
						 RBBIM_CHILDSIZE | RBBIM_IMAGE;
    rbbi[2].wID = ID_BAND_ADDR;
    rbbi[2].fStyle = RBBS_BREAK;
    rbbi[2].cxMinChild = 0;
    rbbi[2].cyMinChild = rcComboBox.bottom - rcComboBox.top; 
    rbbi[2].iImage = 2;
    rbbi[2].hwndChild = _hWndAddressCombo;
    rbbi[2].lpText = (LPWSTR)szAddressLabel;
    
    if (!CommandBands_AddBands(_hWndCmdband, g_hInstance, 3, rbbi))
        return FALSE;

    _hWndMenuBar = CommandBands_GetCommandBar(_hWndCmdband, 0);


    if (!CommandBar_InsertMenubar(_hWndMenuBar, g_hInstance, IDR_MAINMENU, 0))
        return FALSE;
        
#ifndef NO_FAVORITES

	PopulateFavoritesMenu();
	// erase the separator and gray delete menu item if there are no favorites
	if (_nNumFavorites == 0)
	{
		HMENU hMenu = GetSubMenu(CommandBar_GetMenu(_hWndMenuBar, 0), FAVORITES_MENU_POSITION);
		EnableMenuItem(hMenu, ID_FAV_PLACEHOLDER, MF_BYCOMMAND | MF_GRAYED);
	} // if
	else
		DeleteMenu (GetSubMenu(CommandBar_GetMenu(_hWndMenuBar, 0), FAVORITES_MENU_POSITION),
						ID_FAV_PLACEHOLDER, MF_BYCOMMAND);
#endif

     _hWndToolBar = CommandBands_GetCommandBar(_hWndCmdband, 1);
    if ((CommandBar_AddBitmap(_hWndToolBar, g_hInstance, IDB_TBBITMAP,
                              sizeof(tbBtns)/sizeof(TBBUTTON), 16, 16)) < 0)
        return FALSE;

    if (!CommandBar_AddButtons(_hWndToolBar, _countof(tbBtns), tbBtns))
        return FALSE;

    if (!CommandBar_InsertButton(_hWndToolBar, 3, &tbSep))
        return FALSE;

    return TRUE;


}

#ifndef NO_PRINT
//页设置处理 
BOOL HandlePageSetup(HWND hwnd)
{
    HKEY                    hKey;
    PAGESETUPDLG     printdlg;
    RECT                    rcMargin;
    
    DWORD dwMargin, dwSize, dwType; 
	//得到注册表中的页设置
    if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Internet Explorer\\PageSetup", 0, 0, &hKey))
        {
                dwSize = sizeof(DWORD);
               if(ERROR_SUCCESS == RegQueryValueEx(hKey, L"margin_bottom", 0, &dwType, (LPBYTE)&dwMargin, &dwSize))
               {
                    rcMargin.bottom = dwMargin;
               }
               else
                    rcMargin.bottom = 750;

               dwSize = sizeof(DWORD);
               if(ERROR_SUCCESS == RegQueryValueEx(hKey, L"margin_left", 0, &dwType, (LPBYTE)&dwMargin, &dwSize))
               {
                    rcMargin.left = dwMargin;
               }
               else
                    rcMargin.left = 750;

               dwSize = sizeof(DWORD);
               if(ERROR_SUCCESS == RegQueryValueEx(hKey, L"margin_right", 0, &dwType,(LPBYTE)&dwMargin, &dwSize))
               {
                    rcMargin.right = dwMargin;
               }
               else
                    rcMargin.right = 750;     

               dwSize = sizeof(DWORD);
               if(ERROR_SUCCESS == RegQueryValueEx(hKey, L"margin_top", 0, &dwType,(LPBYTE)&dwMargin, &dwSize))
               {
                    rcMargin.top = dwMargin;
               }
               else
                    rcMargin.top = 750;

              RegCloseKey(hKey);
        }

// CE's page setup/print dialog will remember most of its own settings
// we will get the IE margins from the registry

    ::ZeroMemory(&printdlg, sizeof(printdlg));
    printdlg.lStructSize        = sizeof(printdlg);
    printdlg.hwndOwner          = hwnd;       
    printdlg.Flags        |= PSD_MARGINS;
// the print template does not currently support these:	
    printdlg.Flags	  |= PSD_DISABLEORIENTATION;
    printdlg.Flags        |= PSD_DISABLEPRINTRANGE;
    printdlg.rtMargin = rcMargin;  // copy margin rect
    if(PageSetupDlg(&printdlg))
    {
         
           if((rcMargin.left != printdlg.rtMargin.left || 
             rcMargin.right != printdlg.rtMargin.right ||
             rcMargin.top != printdlg.rtMargin.top ||
             rcMargin.bottom != printdlg.rtMargin.bottom))
          {
            DWORD dwMargin; 
            HKEY hKey;
            
            if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Internet Explorer\\PageSetup", 0, 0, &hKey))
            {
                   dwMargin = printdlg.rtMargin.bottom;
                   RegSetValueEx(hKey, L"margin_bottom", 0, REG_DWORD, (LPBYTE)&dwMargin, sizeof(DWORD));

                   dwMargin = printdlg.rtMargin.left;
                   RegSetValueEx(hKey, L"margin_left", 0, REG_DWORD, (LPBYTE)&dwMargin, sizeof(DWORD));

                   dwMargin = printdlg.rtMargin.right;
                   RegSetValueEx(hKey, L"margin_right", 0, REG_DWORD, (LPBYTE)&dwMargin, sizeof(DWORD));
 
                   dwMargin = printdlg.rtMargin.top;
                   RegSetValueEx(hKey, L"margin_top", 0, REG_DWORD, (LPBYTE)&dwMargin, sizeof(DWORD));

                   RegCloseKey(hKey);
            }
          }
          return TRUE;
    }
    return FALSE;
}
#endif

#ifndef NO_FAVORITES
//收藏夹菜单
UINT CMainWnd::PopulateFavoritesMenu()
{
	UINT  uID	   = ID_FAVORITE_FIRST;
	HMENU hFavMenu = NULL;

	if (!_pIFavStorage)
		return 0;

	// Continue only if Favorites registry key is present 
	if (!_pIFavStorage->IsFavAvailable())
		return 0;

    if (NULL == (hFavMenu = GetSubMenu(CommandBar_GetMenu(_hWndMenuBar, 0), 
									   FAVORITES_MENU_POSITION)))
		return 0;

	if (!InsertFavoriteIntoMenu(hFavMenu,_pIFavStorage->GetRootFolder(),uID))
		return 0;

	return _nNumFavorites;
} // PopulateFavoritesMenu
#endif  // ndef NO_FAVORITES
#endif	//NOCOMMANDBAR

#ifndef NO_FAVORITES
//把收藏夹插入到菜单
BOOL CMainWnd::InsertFavoriteIntoMenu(HMENU hMenu, LPTSTR szPath, UINT uID, UINT uPos)
{
	HTREEITEM   hParent     = NULL;
	DWORD       dwFolders   = 0;
	DWORD       dwRegKeyLen = 0;
	DWORD       idx			= 0;
	DWORD       dwValues    = 0;
	static UINT  _uID	    = uID;
	BOOL		bRetVal     = TRUE;
	TCHAR       szRegKey1[MAX_REGKEY_LEN];
	TCHAR       szRegKey [FAV_NAME_LEN];

	if (!_pIFavStorage)
		return FALSE;

	INIT_STR (szRegKey);
	INIT_STR (szRegKey1);

	if (ERROR_SUCCESS != _pIFavStorage->GetFolderInfo (szPath,
													    dwFolders,
														dwValues))
		goto ERR_RET;

	if (!dwFolders)		// If there are no folders
	{
		// If this item has no values also, it is assumed to be an empty folder.
		// In this case, an empty sub folder is added to this menu item.
		if (!dwValues)
			GOERROR(hMenu = SetMenuInfo (hMenu,_uID,uPos));
		
		goto RETURN;
	}
	else
		if (_uID != ID_FAVORITE_FIRST)
			GOERROR(hMenu = SetMenuInfo (hMenu,_uID,uPos));

	for (idx=0; idx < dwFolders; idx++)
	{
		dwRegKeyLen = FAV_NAME_LEN-1;
		if (ERROR_SUCCESS != _pIFavStorage->EnumFolders (szPath,
														  idx,
														  szRegKey,
														  dwRegKeyLen))
			continue;

		GOERROR (AddMenuItem(hMenu,_uID,szRegKey,szPath));
		++_uID;
		wsprintf(szRegKey1,L"%s\\%s",szPath,szRegKey);
		InsertFavoriteIntoMenu(hMenu,szRegKey1,_uID,idx);
	}
	goto RETURN;

ERR_RET:

	bRetVal = FALSE;
	
RETURN:
	if (!_tcscmp (_pIFavStorage->GetRootFolder(), szPath))
		_uID = ID_FAVORITE_FIRST;

	return bRetVal;
}

BOOL CMainWnd::DeleteMenuItem (HMENU hMenu, UINT uPos)//删除菜单项
{
	MENUITEMINFO menuInfo = {0};

	for (UINT idx=uPos;;idx++)
	{
		menuInfo.cbSize = sizeof (menuInfo);
		menuInfo.fMask  = MIIM_SUBMENU;
		if (GetMenuItemInfo (hMenu, idx, TRUE, &menuInfo))
		{
			if (menuInfo.hSubMenu)
				DestroyMenu (menuInfo.hSubMenu);
			DeleteMenu (hMenu, idx, MF_BYPOSITION);
			idx -= 1;
		}
		else
			break;
	}
	return TRUE;
}

BOOL CMainWnd::AddMenuItem (HMENU hMenu, UINT &uID, LPCTSTR szText, LPCTSTR szInfo)
{
	MENUITEMINFO menuInfo;
	BOOL		 bSetInfo = FALSE;
	LPTSTR		 szStr    = NULL;
	BOOL		 bRetVal  = TRUE;

	_nNumFavorites++;

	menuInfo.cbSize = sizeof (menuInfo);
	menuInfo.fMask  = MIIM_STATE;
	if (GetMenuItemInfo (hMenu, 0, TRUE, &menuInfo))
	{
		if (menuInfo.fState == MF_GRAYED)
		{
			GOERROR(DeleteMenu (hMenu, 0, MF_BYPOSITION));
			uID -= 1;
			bSetInfo = TRUE;
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -