VC++6.0中改變窗口背景顏色和控件背景顏色
1.改變對話框的背景色
在C…App類中的InitInstance()里添加
SetDialogBkColor(RGB(0,192,0),RGB(0,0,0));
2.如果想改變靜態文本或單選按鈕的背景色可以用你說的那個獲得控件ID,然后設置背景色,具體步驟:
(1)響應對話框類的WM_CTLCOLOR消息生成OnCtlColor函數
(2)為對話框類添加成員變量CBrush m_brush;
并在初始化函數中初始化m_brush.CreateSolidBrush(RGB(0,255,0));
(3)在OnCtlColor函數中添加代碼以改變控件的文字顏色和背景色
switch(pWnd->GetDlgCtrlID())
{
case(IDC_INPUT):
pDC->SetTextColor(RGB(255,0,192));
pDC->SetBkMode(TRANSPARENT);
return m_brush;
break;
case(IDC_EDIT1):
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkMode(TRANSPARENT);
return m_brush;
break;
case(IDC_CHOICE):
pDC->SetTextColor(RGB(255,128,0));
pDC->SetBkMode(TRANSPARENT);
return m_brush;
break;
case(IDC_RADIO1):
pDC->SetTextColor(RGB(255,0,20));
pDC->SetBkMode(TRANSPARENT);
return m_brush;
break;
default:
break;
}
3.如果想改變按鈕的背景色,簡直太難了,你要重寫兩個類,還需要在網上下,孫鑫的視頻教程中也簡單介紹了這個,可只是改變按鈕的文字顏色
int SetBkMode(
HDC hdc, // handle to DC
int iBkMode // background mode
);
The SetBkMode function sets the background mix mode of the specified device context. The background mix mode is used with text, hatched brushes, and pen styles that are not solid lines.
SetTextColor
The SetTextColor function sets the text color for the specified device context to the specified color.
COLORREF SetTextColor(
HDC hdc, // handle to DC
COLORREF crColor // text color
);
轉載聲明: 本文轉自 http://wmnmtm.blog.163.com/blog/static/3824571420097223040181/
===============================================================
CDC, 我的感覺
基于對話框的程序:
void CTestDlg::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);
AfxMessageBox("dd"); //寫在這兒不彈出
}
else
{
CDialog::OnPaint();
//AfxMessageBox("dd"); //寫在這兒將不停的彈出,說明在不停的調用
if (NULL==GetDC())
{
AfxMessageBox("a");
}
else
{
AfxMessageBox("b");
}
}
}
新建一個對話框,添加WM_PAINT消息
void CMyDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CDialog::OnPaint() for painting messages
}
可以在里面通過dc畫圖
void CMyDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.MoveTo(0,0);
dc.LineTo(10,10);
dc.Arc(50,50,80,80,100,100,200,200);
// TODO: Add your message handler code here
// Do not call CDialog::OnPaint() for painting messages
}
對于單文檔程序,在View類中,會自動生成OnDraw函數
void CTestAView::OnDraw(CDC* pDC)
{
CTestADoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
可以利用pDC來進行繪圖:
void CTestAView::OnDraw(CDC* pDC)
{
CTestADoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->MoveTo(0,0);
pDC->LineTo(100,100);
}
!!順便說一下,在OnDraw函數中,自動加了一句:CTestADoc* pDoc = GetDocument();
通過pDoc,可以獲得與這個視圖相關聯的文檔的指針。這使你能夠調用文檔的成員函數。
如下 :
void CTestAView::OnDraw(CDC* pDC)
{
CTestADoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->MoveTo(0,0);
pDC->LineTo(100,100);
pDC->Arc(100.,100,200,200,300,300,400,400);
pDC->TextOut(100,100,"ABC");
//設置文檔的名稱
LPCTSTR lpctstr;
lpctstr="我的文檔";
pDoc->SetTitle(lpctstr);
CString str=pDoc->GetTitle();
AfxMessageBox(str);
}
轉載聲明: 本文轉自 http://wmnmtm.blog.163.com/blog/static/3824571420096319566302/
===============================================================
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
