在sdi的view中,显示背景位图
void CMainiew::OnInitialUpdate() { CView::OnInitialUpdate(); // TODO: Add your specialized code here and/or call the base class /** @note * CMainView的成员变量 * CBitmap m_bmpBG; * BITMAP m_bitmapBG; */ m_bmpBG.LoadBitmap(IDB_BITMAP_BG); m_bmpBG.GetBitmap(&m_bitmapBG); } BOOL CMainiew::OnEraseBkgnd(CDC* pDC) { CRect rc; CDC hMemDC; CBitmap* pBitmapOld = NULL; hMemDC.CreateCompatibleDC(pDC); pBitmapOld = hMemDC.SelectObject(&m_bmpBG); GetClientRect(rc); if((rc.Width() > m_bitmapBG.bmWidth) || (rc.Height() > m_bitmapBG.bmHeight)) {//显示区域 > 图片, 拉伸显示 StretchBlt( pDC->m_hDC, rc.left, rc.top, rc.Width(), rc.Height(), hMemDC, 0, 0, m_bitmapBG.bmWidth, m_bitmapBG.bmHeight, SRCCOPY); } else {//显示区域 < 图片, 原样显示 BitBlt( pDC->m_hDC, rc.left, rc.top, rc.Width(), rc.Height(), hMemDC, 0, 0, SRCCOPY); } hMemDC.SelectObject(pBitmapOld); return TRUE; } void CMainiew::OnDestroy() { m_bmpBG.DeleteObject(); CView::OnDestroy(); }
/** * HBITMAP g_hBmp = NULL;//位图句柄 * BITMAP g_bitmapBG;//位图信息 */ g_hBmp = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP_BG)); /** * 从位图句柄中得到位图信息 * g_bitmapBG.bmWidth, g_bitmapBG.bmHeight ... */ GetObject(g_hBmp, sizeof(BITMAP), &g_bitmapBG);/**< HBITMAP to BITMAP */
设置MDI的Frame背景图
//FrameworkOfXtremeMdi.cpp HBITMAP g_hBmp = NULL; BITMAP g_bitmapBG; WNDPROC g_pfnOldWndProc = NULL; LRESULT CALLBACK pfnNewWndProc(HWND hwnd, UINT uMsg, WPARAM wParam,LPARAM lParam);
BOOL CMainApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate( IDR_FRAMEWTYPE, RUNTIME_CLASS(CMainDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CMainView)); AddDocTemplate(pDocTemplate); // create main MDI Frame window CMainFrame* pMainFrame = new CMainFrame; if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) return FALSE; m_pMainWnd = pMainFrame; // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if(!ProcessShellCommand(cmdInfo)) return FALSE; /** * HBITMAP g_hBmp = NULL;//位图句柄 * BITMAP g_bitmapBG;//位图信息 */ g_hBmp = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP_BG)); /** * 从位图句柄中得到位图信息 * g_bitmapBG.bmWidth, g_bitmapBG.bmHeight ... */ GetObject(g_hBmp, sizeof(BITMAP), &g_bitmapBG);/**< HBITMAP to BITMAP */ HWND hMain = pMainFrame->GetWindow(GW_CHILD)->GetSafeHwnd(); g_pfnOldWndProc = (WNDPROC)GetWindowLong(hMain, GWL_WNDPROC); SetWindowLong(hMain, GWL_WNDPROC, (long)pfnNewWndProc); // The main window has been initialized, so show and update it. pMainFrame->ShowWindow(m_nCmdShow); pMainFrame->UpdateWindow(); return TRUE; } LRESULT CALLBACK pfnNewWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hdc = (HDC)wParam; HDC hcompdc; CRect rc; switch(uMsg) { case WM_SIZE : SendMessage(hwnd, WM_ERASEBKGND, (WPARAM)GetDC(hwnd), 0); return 1; case WM_ERASEBKGND : hcompdc = CreateCompatibleDC(hdc); SelectObject(hcompdc, g_hBmp); GetClientRect(hwnd, &rc); if((rc.Width() > g_bitmapBG.bmWidth) || (rc.Height() > g_bitmapBG.bmHeight)) {//显示区域 > 图片, 拉伸显示 StretchBlt(hdc, rc.left, rc.top, rc.right, rc.bottom, hcompdc, 0, 0, g_bitmapBG.bmWidth, g_bitmapBG.bmHeight, SRCCOPY); } else {//显示区域 < 图片, 原样显示 BitBlt(hdc, rc.left, rc.top, rc.Width(), rc.Height(), hcompdc, 0, 0, SRCCOPY); } DeleteDC(hcompdc); return 1; default : return CallWindowProc(g_pfnOldWndProc, hwnd, uMsg, wParam, lParam); } } int CMainApp::ExitInstance() { // TODO: Add your specialized code here and/or call the base class DeleteObject(g_hBmp); return CWinApp::ExitInstance(); }
在dialog中显示背景图
//Dlg.h //private: // CBitmap m_bmpBG; // BITMAP m_bitmapBG; // MainDlg.cpp : implementation file // #include "stdafx.h" #include "FrameworkOfXtremeDlg.h" #include "MainDlg.h" #include "BinRes.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMainiDlg dialog BEGIN_MESSAGE_MAP(CMainiDlg, CDialog) //{{AFX_MSG_MAP(CMainiDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_ERASEBKGND() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMainiDlg message handlers BOOL CMainiDlg::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 this->SetWindowText(G_STR_PROG_NAME); m_bmpBG.LoadBitmap(IDB_BITMAP_BG); m_bmpBG.GetBitmap(&m_bitmapBG); LoadSkinFile(GetDefaultSkinFilePathName()); CenterWindow(NULL); return TRUE; // return TRUE unless you set the focus to a control } void CMainiDlg::LoadSkinFile(CString csSkinFilePathName) { BinRes::ExtractBinResource("BIN", IDR_BIN_SKIN_DEFAULT, G_DEFAULT_SKIN_FILE_NAME); XTPSkinManager()->SetApplyOptions( XTPSkinManager()->GetApplyOptions() | xtpSkinApplyMetrics | xtpSkinApplyFrame | xtpSkinApplyColors | xtpSkinApplyMenus); XTPSkinManager()->LoadSkin((LPSTR)(LPCSTR)csSkinFilePathName); XTPSkinManager()->ApplyWindow(this->m_hWnd); } void CMainiDlg::UnLoadSkinFile() { XTPSkinManager()->LoadSkin(NULL, NULL); XTPSkinManager()->ApplyWindow(this->m_hWnd); /** * 如果多个程序都用这一个皮肤文件, 退出程序时, 不要删除 */ //::DeleteFile(GetDefaultSkinFilePathName()); } CString CMainiDlg::GetDefaultSkinFilePathName() { CString strDefaultSkinFilePathName; strDefaultSkinFilePathName = GetDefaultSkinFilePath(); strDefaultSkinFilePathName += G_DEFAULT_SKIN_FILE_NAME; return strDefaultSkinFilePathName; } CString CMainiDlg::GetDefaultSkinFilePath() { std::string strDefaultSkinFilePath; strDefaultSkinFilePath = BinRes::getAppLocationPath(); return strDefaultSkinFilePath.c_str(); } void CMainiDlg::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 CMainiDlg::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 CMainiDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } BOOL CMainiDlg::DestroyWindow() { // TODO: Add your specialized code here and/or call the base class UnLoadSkinFile(); m_bmpBG.DeleteObject(); return CDialog::DestroyWindow(); } BOOL CMainiDlg::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default return DrawBkBmp(pDC); //return CView::OnEraseBkgnd(pDC); } BOOL CMainiDlg::DrawBkBmp(CDC * pDC) { CRect rc; CDC hMemDC; CBitmap* pBitmapOld = NULL; hMemDC.CreateCompatibleDC(pDC); pBitmapOld = hMemDC.SelectObject(&m_bmpBG); GetClientRect(rc); if((rc.Width() > m_bitmapBG.bmWidth) || (rc.Height() > m_bitmapBG.bmHeight)) {//显示区域 > 图片, 拉伸显示 StretchBlt( pDC->m_hDC, rc.left, rc.top, rc.Width(), rc.Height(), hMemDC, 0, 0, m_bitmapBG.bmWidth, m_bitmapBG.bmHeight, SRCCOPY); } else {//显示区域 < 图片, 原样显示 BitBlt( pDC->m_hDC, rc.left, rc.top, rc.Width(), rc.Height(), hMemDC, 0, 0, SRCCOPY); } hMemDC.SelectObject(pBitmapOld); return TRUE; }