Thread: Unknown bug

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    42

    Unknown bug

    I use MFC to build the interface of a program and I write a new function to re-draw the window style and dialog style (similar to XP style). It's all ok when I run the program at the initial window size, but if I let the window to be max size, the menu bar becomes something wrong, I must double click each menu item, the single click is unavailable.

    Code:
    void CClassname::DrawTitleBar(CDC *pDC)
    {
    	if (m_hWnd)
    	{
    		CBrush Brush(RGB(0,100,255));
    		CBrush* pOldBrush = pDC->SelectObject(&Brush);
    
    		CRect rtWnd, rtTitle, rtButtons;
    		GetWindowRect(&rtWnd); 
    		//Get the position of title
    		rtTitle.left = GetSystemMetrics(SM_CXFRAME);
    		rtTitle.top = GetSystemMetrics(SM_CYFRAME);
    		rtTitle.right = rtWnd.right - rtWnd.left - GetSystemMetrics(SM_CXFRAME);
    		rtTitle.bottom = rtTitle.top + GetSystemMetrics(SM_CYSIZE);
    
    		CPoint point;
    		//draw the top of the frame
    		point.x = rtWnd.Width();
    		point.y = GetSystemMetrics(SM_CYSIZE) + GetSystemMetrics(SM_CYFRAME)+20;
    		pDC->PatBlt(0, 0, point.x, point.y, PATCOPY);
    		//draw the left of the frame
    		point.x = GetSystemMetrics(SM_CXFRAME) + 1;
    		point.y = rtWnd.Height();
    		pDC->PatBlt(0, 0, point.x, point.y, PATCOPY);
    		//draw the bottom of the frame
    		point.x = rtWnd.Width(); 
    		point.y = GetSystemMetrics(SM_CYFRAME) + 1;
    		pDC->PatBlt(0, rtWnd.Height()-point.y, point.x, point.y, PATCOPY);
    		//draw the right of the frame
    		point.x = GetSystemMetrics(SM_CXFRAME) + 1;
    		point.y = rtWnd.Height();
    		pDC->PatBlt(rtWnd.Width()-point.x, 0, point.x, point.y, PATCOPY);
    
    		
    		//Re-draw icon
    		m_rtIcon.left = rtWnd.Width() - 135;
    		m_rtIcon.top = GetSystemMetrics(SM_CYFRAME);
    		m_rtIcon.right = m_rtIcon.left + 32;
    		m_rtIcon.bottom = m_rtIcon.top + 32;
    		::DrawIconEx(pDC->m_hDC, m_rtIcon.left, m_rtIcon.top, m_hIcon, 
    			m_rtIcon.Width(), m_rtIcon.Height(), 0, NULL, DI_NORMAL);
    		m_rtIcon.OffsetRect(rtWnd.TopLeft()); 
    
    
    		//Prepare for the XP sytle button
    		CBitmap* pBitmap = new CBitmap;
    		CBitmap* pOldBitmap;
    		CDC* pDisplayMemDC=new CDC;
    		pDisplayMemDC->CreateCompatibleDC(pDC);
    
    		//Re-draw close button
    		rtButtons.left = rtTitle.right - 19;
    		rtButtons.top = rtTitle.top;
    		rtButtons.right = rtButtons.left + 19;
    		rtButtons.bottom = rtButtons.top + 19;
    		pBitmap->LoadBitmap(IDB_EXIT_NORMAL);
    		pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
    		pDC->BitBlt(rtButtons.left, rtButtons.top, rtButtons.Width(), rtButtons.Height(), pDisplayMemDC, 0, 0, SRCCOPY);
    		pDisplayMemDC->SelectObject(pOldBitmap);
    		m_rtButtExit = rtButtons;
    		m_rtButtExit.OffsetRect(rtWnd.TopLeft()); 
    		pBitmap->DeleteObject();
    
    		//Re-draw restore button
    		rtButtons.right = rtButtons.left - 3;
    		rtButtons.left = rtButtons.right - 19;
    		if (IsZoomed())
    			pBitmap->LoadBitmap(IDB_RESTORE_NORMAL);
    		else
    			pBitmap->LoadBitmap(IDB_MAX_NORMAL);
    		pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
    		pDC->BitBlt(rtButtons.left, rtButtons.top, rtButtons.Width(), rtButtons.Height(), pDisplayMemDC, 0, 0, SRCCOPY);
    		pDisplayMemDC->SelectObject(pOldBitmap);
    		m_rtButtMax = rtButtons;
    		m_rtButtMax.OffsetRect(rtWnd.TopLeft());
    		pBitmap->DeleteObject();
    
    		//Re-draw minisize button
    		rtButtons.right = rtButtons.left - 3;
    		rtButtons.left = rtButtons.right - 19;
    		pBitmap->LoadBitmap(IDB_MIN_NORMAL);
    		pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
    		pDC->BitBlt(rtButtons.left, rtButtons.top, rtButtons.Width(), rtButtons.Height(), pDisplayMemDC, 0, 0, SRCCOPY);
    		pDisplayMemDC->SelectObject(pOldBitmap);
    		m_rtButtMin = rtButtons;
    		m_rtButtMin.OffsetRect(rtWnd.TopLeft());
    		pBitmap->DeleteObject();
    	
    				
    		//Re-draw caption
    		int nOldMode = pDC->SetBkMode(TRANSPARENT);
    		COLORREF clOldText=pDC->SetTextColor(RGB(255, 255, 255));
    		pDC->SelectStockObject(SYSTEM_FIXED_FONT);
    		rtTitle.left += 2;
    		rtTitle.top += GetSystemMetrics(SM_CYSIZE);
    		rtTitle.bottom = rtTitle.top + 30;
    		CString m_strTitle;
    		GetWindowText(m_strTitle);
    		pDC->DrawText(m_strTitle, &rtTitle, DT_LEFT);
    		pDC->SetBkMode(nOldMode);
    		pDC->SetTextColor(clOldText);
    
    		ReleaseDC(pDisplayMemDC);
    		delete pDisplayMemDC;
    		delete pBitmap;
    	}
    }
    And you must add a DefWindowProc function and override it

    Code:
    LRESULT CClassname::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
    {
    	LRESULT lrst=CDialog::DefWindowProc(message, wParam, lParam);
    	
    	if (!::IsWindow(m_hWnd))
    		return lrst;
    	
    	if (message == WM_MOVE||message == WM_PAINT||message==WM_NCPAINT||message==WM_NCACTIVATE ||message == WM_NOTIFY)
    	{
    		CDC* pWinDC = GetWindowDC();
    		if (pWinDC)
    			DrawTitleBar(pWinDC);
    		ReleaseDC(pWinDC);
    	}
    	return lrst;
    }

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    42
    PS. The additional declaration in header file:

    Code:
    public:
    	void DrawTitleBar(CDC *pDC);
    	CRect m_rtButtExit;	
    	CRect m_rtButtMax;	
    	CRect m_rtButtMin;		
    	CRect m_rtIcon;	
    protected:
    	HICON m_hIcon;
    You can directly use these code in your program for testing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 02:47 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM