1 void CDemoDlg::OnPaint() 2 { 3 if (IsIconic()) 4 { 5 CPaintDC dc(this); // device context for painting 6 7 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 8 9 // Center icon in client rectangle 10 int cxIcon = GetSystemMetrics(SM_CXICON); 11 int cyIcon = GetSystemMetrics(SM_CYICON); 12 CRect rect; 13 GetClientRect(&rect); 14 int x = (rect.Width() - cxIcon + 1) / 2; 15 int y = (rect.Height() - cyIcon + 1) / 2; 16 17 // Draw the icon 18 dc.DrawIcon(x, y, m_hIcon); 19 } 20 else 21 { 22 //设置整个dialog的背景 (一张位图) 23 CPaintDC dc(this); 24 CRect rect; 25 GetClientRect(&rect); 26 27 CDC dcMem; 28 dcMem.CreateCompatibleDC(&dc); 29 30 CBitmap bmpBackground; 31 bmpBackground.LoadBitmap(IDB_BITMAP1); 32 BITMAP bitmap; 33 bmpBackground.GetBitmap(&bitmap); 34 CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground); 35 dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0, 36 bitmap.bmWidth,bitmap.bmHeight,SRCCOPY); 37 38 39 40 41 42 //静态文字 43 CDC *pDC = this->GetDC(); 44 CString str; 45 str.LoadString(IDS_STRING102); 46 pDC->SetBkMode(TRANSPARENT); 47 pDC->SetBkColor(TRANSPARENT); 48 49 pDC->SetTextColor(RGB(82,82,122)); 50 pDC->TextOut(290,40,str); 51 str.LoadString(IDS_STRING103); 52 pDC->TextOut(400,40,str); 53 54 str.LoadString(IDS_STRING104); 55 pDC->TextOut(480,80,str); 56 str.LoadString(IDS_STRING105); 57 pDC->TextOut(480,100,str); 58 str.LoadString(IDS_STRING106); 59 pDC->TextOut(480,120,str); 60 61 ReleaseDC(pDC); 62 63 64 //编辑框-不知道为啥,这个不起作用 65 CDC *pCDC = GetDlgItem(IDC_PROGRESS2)->GetDC(); 66 pCDC->SetBkMode(TRANSPARENT); 67 pCDC->SetBkColor(TRANSPARENT); 68 pCDC->SetTextColor(RGB(0,255,0)); 69 ReleaseDC(pCDC); 70 71 } 72
一张截图,静态文字背景和窗口背景一样,但是输入框却不一样,不知道哪里写错了
试试把
pCDC->SetBkMode(TRANSPARENT);
pCDC->SetBkColor(TRANSPARENT);
放在
pDC->TextOut(480,80,str);
之前
为什么呢?
首先我有两个CDC指针pDC、pCDC
他们操纵的控件也一样啊,一个是整个对话框,一个是IDC_PROGRESS2