VC++6.0中改变窗口背景颜色和控件背景颜色



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

);




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);

}