因小弟用windowsAPI和纯c写了一个程序,没有涉及到MFC,这个问题困扰我很久
思路也应该是在消息WM_CTLCOLORLISTBOX中改变背景色和字体颜色,但看MSDN很就不得其解,就是不知道怎么操作才能改变ListBox控件的背景色,有会的,能适量写点代码,多谢
7 个解决方案
#1
差不多是这样,在父窗口处理
HBRUSH hbr = CreateSolidBrush(RGB(128, 128, 128));
......
case WM_CTLCOLORLISTBOX:
{
HDC hDC = (HDC)wParam;
SetBkColor(hDC, RGB(128, 128, 128));
SetTextColor(hDC, RGB(255, 0, 0));
return hbr;
}
break;
......
HBRUSH hbr = CreateSolidBrush(RGB(128, 128, 128));
......
case WM_CTLCOLORLISTBOX:
{
HDC hDC = (HDC)wParam;
SetBkColor(hDC, RGB(128, 128, 128));
SetTextColor(hDC, RGB(255, 0, 0));
return hbr;
}
break;
......
#2
http://www.geocities.com/Heartland/Meadows/9818/win32tut/lesson4.html
int WINAPI
WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
...
// Initialise colors
crEditFg = RGB(0,255,0); // Green ON
crEditBg = RGB(0,0,0); // Black
hbrEditBg = CreateSolidBrush(crEditBg);
crScrollFg = RGB(0,0,0); // Black ON
crScrollBg = RGB(255,255,0); // Yellow
hbrScrollBg = CreateSolidBrush(crScrollBg);
crListFg = RGB(0,0,255); // Blue ON
crListBg = RGB(255,255,0); // Yellow
hbrListBg = CreateSolidBrush(crListBg);
...
}
// Callback function for the Main Window class
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
RECT rc;
switch(msg)
{
...
case WM_CTLCOLOREDIT:
// wParam contains display context
// lParam contains handle of edit control
SetBkColor((HDC)wParam, crEditBg); //Make background black
SetTextColor((HDC)wParam, crEditFg); //Make foreground green
return (LRESULT)hbrEditBg;
case WM_CTLCOLORLISTBOX:
SetBkColor((HDC)wParam, crListBg);
SetTextColor((HDC)wParam, crListFg);
return (LRESULT)hbrListBg;
case WM_CTLCOLORSCROLLBAR:
SetBkColor((HDC)wParam, crScrollBg);
SetTextColor((HDC)wParam, crScrollFg);
return (LRESULT)hbrScrollBg;
...
}
}
int WINAPI
WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
...
// Initialise colors
crEditFg = RGB(0,255,0); // Green ON
crEditBg = RGB(0,0,0); // Black
hbrEditBg = CreateSolidBrush(crEditBg);
crScrollFg = RGB(0,0,0); // Black ON
crScrollBg = RGB(255,255,0); // Yellow
hbrScrollBg = CreateSolidBrush(crScrollBg);
crListFg = RGB(0,0,255); // Blue ON
crListBg = RGB(255,255,0); // Yellow
hbrListBg = CreateSolidBrush(crListBg);
...
}
// Callback function for the Main Window class
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
RECT rc;
switch(msg)
{
...
case WM_CTLCOLOREDIT:
// wParam contains display context
// lParam contains handle of edit control
SetBkColor((HDC)wParam, crEditBg); //Make background black
SetTextColor((HDC)wParam, crEditFg); //Make foreground green
return (LRESULT)hbrEditBg;
case WM_CTLCOLORLISTBOX:
SetBkColor((HDC)wParam, crListBg);
SetTextColor((HDC)wParam, crListFg);
return (LRESULT)hbrListBg;
case WM_CTLCOLORSCROLLBAR:
SetBkColor((HDC)wParam, crScrollBg);
SetTextColor((HDC)wParam, crScrollFg);
return (LRESULT)hbrScrollBg;
...
}
}
#3
http://www.vckbase.com/document/viewdoc/?id=400
#4
楼上的都是牛人啊,小弟从来没服过高手,本人自认为高手,今天第一次发帖,服了
#5
又出问题了,goodboyws(深夜不眠者) , masterz(www.fruitfruit.com), laiyiling(陌生人 V2.0) 来救我啊
当我改变SetBkColor(hDC, RGB(128, 128, 128));->SetBkColor(hDC, RGB(255, 0, 0));
会出现字符部分为红色,而字符后面的颜色还是RGB(128, 128, 128));
怎么会出现这种情况啊?
而且这个HDC不释放吗?
当我改变SetBkColor(hDC, RGB(128, 128, 128));->SetBkColor(hDC, RGB(255, 0, 0));
会出现字符部分为红色,而字符后面的颜色还是RGB(128, 128, 128));
怎么会出现这种情况啊?
而且这个HDC不释放吗?
#6
这里没改
HBRUSH hbr = CreateSolidBrush(RGB(255, 0, 0));
HDC不用释放,Windows会替你搞定
HBRUSH hbr = CreateSolidBrush(RGB(255, 0, 0));
HDC不用释放,Windows会替你搞定
#7
SetBkMode(hDC, TRANSPARENT);
#1
差不多是这样,在父窗口处理
HBRUSH hbr = CreateSolidBrush(RGB(128, 128, 128));
......
case WM_CTLCOLORLISTBOX:
{
HDC hDC = (HDC)wParam;
SetBkColor(hDC, RGB(128, 128, 128));
SetTextColor(hDC, RGB(255, 0, 0));
return hbr;
}
break;
......
HBRUSH hbr = CreateSolidBrush(RGB(128, 128, 128));
......
case WM_CTLCOLORLISTBOX:
{
HDC hDC = (HDC)wParam;
SetBkColor(hDC, RGB(128, 128, 128));
SetTextColor(hDC, RGB(255, 0, 0));
return hbr;
}
break;
......
#2
http://www.geocities.com/Heartland/Meadows/9818/win32tut/lesson4.html
int WINAPI
WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
...
// Initialise colors
crEditFg = RGB(0,255,0); // Green ON
crEditBg = RGB(0,0,0); // Black
hbrEditBg = CreateSolidBrush(crEditBg);
crScrollFg = RGB(0,0,0); // Black ON
crScrollBg = RGB(255,255,0); // Yellow
hbrScrollBg = CreateSolidBrush(crScrollBg);
crListFg = RGB(0,0,255); // Blue ON
crListBg = RGB(255,255,0); // Yellow
hbrListBg = CreateSolidBrush(crListBg);
...
}
// Callback function for the Main Window class
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
RECT rc;
switch(msg)
{
...
case WM_CTLCOLOREDIT:
// wParam contains display context
// lParam contains handle of edit control
SetBkColor((HDC)wParam, crEditBg); //Make background black
SetTextColor((HDC)wParam, crEditFg); //Make foreground green
return (LRESULT)hbrEditBg;
case WM_CTLCOLORLISTBOX:
SetBkColor((HDC)wParam, crListBg);
SetTextColor((HDC)wParam, crListFg);
return (LRESULT)hbrListBg;
case WM_CTLCOLORSCROLLBAR:
SetBkColor((HDC)wParam, crScrollBg);
SetTextColor((HDC)wParam, crScrollFg);
return (LRESULT)hbrScrollBg;
...
}
}
int WINAPI
WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
...
// Initialise colors
crEditFg = RGB(0,255,0); // Green ON
crEditBg = RGB(0,0,0); // Black
hbrEditBg = CreateSolidBrush(crEditBg);
crScrollFg = RGB(0,0,0); // Black ON
crScrollBg = RGB(255,255,0); // Yellow
hbrScrollBg = CreateSolidBrush(crScrollBg);
crListFg = RGB(0,0,255); // Blue ON
crListBg = RGB(255,255,0); // Yellow
hbrListBg = CreateSolidBrush(crListBg);
...
}
// Callback function for the Main Window class
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
RECT rc;
switch(msg)
{
...
case WM_CTLCOLOREDIT:
// wParam contains display context
// lParam contains handle of edit control
SetBkColor((HDC)wParam, crEditBg); //Make background black
SetTextColor((HDC)wParam, crEditFg); //Make foreground green
return (LRESULT)hbrEditBg;
case WM_CTLCOLORLISTBOX:
SetBkColor((HDC)wParam, crListBg);
SetTextColor((HDC)wParam, crListFg);
return (LRESULT)hbrListBg;
case WM_CTLCOLORSCROLLBAR:
SetBkColor((HDC)wParam, crScrollBg);
SetTextColor((HDC)wParam, crScrollFg);
return (LRESULT)hbrScrollBg;
...
}
}
#3
http://www.vckbase.com/document/viewdoc/?id=400
#4
楼上的都是牛人啊,小弟从来没服过高手,本人自认为高手,今天第一次发帖,服了
#5
又出问题了,goodboyws(深夜不眠者) , masterz(www.fruitfruit.com), laiyiling(陌生人 V2.0) 来救我啊
当我改变SetBkColor(hDC, RGB(128, 128, 128));->SetBkColor(hDC, RGB(255, 0, 0));
会出现字符部分为红色,而字符后面的颜色还是RGB(128, 128, 128));
怎么会出现这种情况啊?
而且这个HDC不释放吗?
当我改变SetBkColor(hDC, RGB(128, 128, 128));->SetBkColor(hDC, RGB(255, 0, 0));
会出现字符部分为红色,而字符后面的颜色还是RGB(128, 128, 128));
怎么会出现这种情况啊?
而且这个HDC不释放吗?
#6
这里没改
HBRUSH hbr = CreateSolidBrush(RGB(255, 0, 0));
HDC不用释放,Windows会替你搞定
HBRUSH hbr = CreateSolidBrush(RGB(255, 0, 0));
HDC不用释放,Windows会替你搞定
#7
SetBkMode(hDC, TRANSPARENT);