2012-02-08 42 views
-1

好的我在某些參數大於另一個參數時能夠更改位圖時遇到了一些問題。我是一個巨大的新手,我的編碼不是很好(根本)。我有代碼讀取的限制(參數)和顯示爲文本,其是這樣的:當達到限制時獲取位圖顏色變化

CFont* def_font = argDC->SelectObject(&m_Font); 
    CString csText; 
    int StartPos = WindowRect.Width()/5; 
    CRect TextRect(StartPos, WindowRect.top + 5, StartPos + 100, WindowRect.top + 35); 
    csText.Format(_T("%.2ft"), argSystemDataPtr->GetMaxSWL()); 
    int32_t iSWLDigits = csText.GetLength(); 
    if (iSWLDigits < m_SWLDigitsNum) 
    { 
     m_RedPanelBitmap.LoadBitmapW(IDB_BITMAP_PANEL_RED); 
     //argDC->FillSolidRect(TextRect, RGB(255, 255, 255)); 
    } 
    m_SWLDigitsNum = iSWLDigits; 
    argDC->DrawText(csText, TextRect, DT_LEFT); 

了通常顯示的位圖是綠色的,但如果有限制被破壞等的一個以上然後我想位圖來改變到一個紅色的。這是我爲綠色所做的。

CRect PanelRect1, PanelRect2; 

     CRect PanelsRect(WindowRect); 

     const int BarHeight = 30; 
     PanelsRect.OffsetRect(0,m_bShowTitleBar?BarHeight:-BarHeight); 
     PanelsRect.DeflateRect(0,m_bShowTitleBar?BarHeight*-1:BarHeight); 


     m_GreenPanelBitmap.Detach(); 


     m_GreenPanelBitmap.LoadBitmapW(IDB_BITMAP_PANEL_GREEN); 

     CBitmap* pOld = memDC.SelectObject(&m_GreenPanelBitmap); 

     BITMAP bits; 

     m_GreenPanelBitmap.GetObject(sizeof(BITMAP),&bits); 

     PanelRect1.SetRect(0,PanelsRect.top, PanelsRect.right /2 , PanelsRect.Height()/3); 
     PanelRect2.SetRect(0,PanelsRect.top+PanelRect1.Height(), PanelsRect.right /2 ,(PanelsRect.Height()/3) + PanelRect1.Height()); 


     //Now draw the Panels 
     if (pOld != NULL) 
     { 

      argDC->StretchBlt(PanelRect1.left ,PanelRect1.top,PanelRect1.Width(),PanelRect1.Height(), 
      &memDC,0,0,bits.bmWidth-1, bits.bmHeight-1, SRCCOPY); 

      argDC->StretchBlt(PanelRect2.left,PanelRect2.top,PanelRect2.Width(),PanelRect2.Height(), 
      &memDC,0,0,bits.bmWidth-1, bits.bmHeight-1, SRCCOPY); 


      memDC.SelectObject(pOld); 

我將是任何幫助非常感謝,我知道有可能是一個簡單的答案,但我一直在抓我的頭在它似乎並不能就如何改變m_GreenPanelBitmap找到答案其他地方當此語句爲true時,將其轉換爲m_RedPanelBitmap。

`if (iSWLDigits < m_SWLDigitsNum).` 
+0

問題是? – 2012-02-08 12:32:08

+0

如何讓位圖更改爲另一個?如果(iSWLDigits bigbaz34 2012-02-08 12:44:35

回答

2

嗯,我覺得你的問題是有點混亂,但...

在第二代碼片段你貼(我從OnPaint方法假設在一個對話框中)正在顯示綠色的位圖通過使用StretchBlt。 如果你的問題是你需要顯示一個位圖或另一個位圖取決於一個條件,你應該加載這兩個圖像(也許你可以做到這一點,以避免每次對話框被加載圖像),然後顯示一個你真正需要基於條件。類似的東西:也許有更詳細的問題

bool bCondition = /*whatever*/ 

m_GreenPanelBitmap.LoadBitmapW(IDB_BITMAP_PANEL_GREEN); 
m_RedPanelBitmap.LoadBitmapW(IDB_BITMAP_PANEL_RED); 

CBitmap* pBitmapToDisplay = bCondition ? &m_GreenPanelBitmap : &m_RedPanelBitmap; 

CBitmap* pOld = memDC.SelectObject(pBitmapToDisplay); 

BITMAP bits; 
pBitmapToDisplay->GetObject(sizeof(BITMAP),&bits); 

PanelRect1.SetRect(0,PanelsRect.top, PanelsRect.right /2 , PanelsRect.Height()/3); 
PanelRect2.SetRect(0,PanelsRect.top+PanelRect1.Height(), PanelsRect.right /2, PanelsRect.Height()/3) + PanelRect1.Height()); 

argDC->StretchBlt(PanelRect1.left ,PanelRect1.top,PanelRect1.Width(),PanelRect1.Height(), 
      &memDC,0,0,bits.bmWidth-1, bits.bmHeight-1, SRCCOPY); 

memDC.SelectObject(pOld); 

,我們將能夠幫助你更多。

+0

非常感謝。我很困惑,情況會是怎樣。與指甲一樣厚的Apoligies。 – bigbaz34 2012-02-08 14:32:21