www.gusucode.com > 微软FreeCL开源免费的C++编译器源码源码程序 > 微软FreeCL开源免费的C++编译器源码/FreeCL_Skin.v2.03.Full.Source.ccrun.718306/FreeCL_Skin.v2.03.Full.Source.ccrun.718306/EngineSource/FreeCL_Tutorial3/Main.cpp

    #include "../FreeCL/FreeCL.h"

#ifdef __BORLANDC__
	#pragma comment(lib, "FreeCL_StaticLib_TC.lib")
#endif

using namespace FreeCL;
    
//------------------------------------------------------------------------------------------------------------

/*
本范例演示了ListBox控件的用法,ComboBox的用法与ListBox类似。
如果遇到不明白的语句,请循序渐进的参考前面的范例。如果用户发现
FreeCL的bug,敬请函告,以便作者在新版本中改进。
*/					    

class MainForm : public WinWindow
{	//主窗口
public:
	WinListBox	mListBox1;
	WinListBox	mListBox2;
	WinListBox	mListBox3;
protected:
	void OnCreate();
	bool OnListBoxSkin(WinListBox* lbox, const ListBoxSkinData& skin);
	bool OnListBoxDrawItem(WinListBox* lbox, const ListBoxItemDrawData& data);
};

void MainForm::OnCreate()
{   
	/*
	UI编辑功能:
	调整控件位置:	Ctrl+鼠标右键
	调整控件尺寸:	Shift+鼠标右键
	选中控件:		Ctrl+鼠标右键
	取消选择:		再一次Ctrl+鼠标右键,或者在主窗口中单击鼠标左键
	UI菜单:			在任一一个选中控件上 Ctrl+Alt+Shift+鼠标右键。UI菜单有些功能只能在VC++环境才有用
	*/

	//设置主窗口自己的背景底纹
	String skinFile = String(FreeCL::GetModulePath()) + String("Patchy.bmp");
	mSkinFile = skinFile.c_str();

	//计算客户区的宽度(尽管主窗口的宽度是固定的,但在不同的主题下其客户区的宽度却不一定相同)
	Rect cRect = mClientRect;
	int cWidth = cRect.mRight - cRect.mLeft;
	int cHeight = cRect.mBottom - cRect.mTop;
	//==============================================================================  		 
		ListBoxDesc lbDesc1;
		//lbDesc1.mSkinScrollBar = true;
		mListBox1.CreateListBox(this, 0, 0, cWidth, 80, lbDesc1);
		mListBox1.mLockFlag = TopLockFlag;
		mListBox1.mParentSkin = true;
		//下面在ListBox中添加3个Item
		mListBox1.AddItem("ListBox Item1");
		mListBox1.AddItem("ListBox Item2");
		mListBox1.AddItem("ListBox Item3");
		mListBox1.AddItem("ListBox Item4");
		mListBox1.AddItem("ListBox Item5");
		mListBox1.AddItem("ListBox Item6");
		mListBox1.AddItem("ListBox Item7");
		mListBox1.AddItem("ListBox Item8");
		mListBox1.mScrollWidth = cWidth;
	//==============================================================================
		ListBoxDesc lbDesc2;
		lbDesc2.mEdgeStyle = EdgeStyle_Color; 
		lbDesc2.mBorderColor = 244;
		lbDesc2.mSkinScrollBar = true;
		mListBox2.CreateListBox(this, 0, 82, cWidth, 80, lbDesc2);   
		mListBox2.mLockFlag = TopLockFlag;
		//mListBox2.mOnDrawSkin = &MainForm::OnListBoxSkin;  
		mListBox2.AddItem("FreeCL 2.00 for Skinning ListBox Item1");
		mListBox2.AddItem("FreeCL 2.00 for Skinning ListBox Item2");
		mListBox2.mTextColor = 0xff0000;
		//加入Item的文本太长,可设置一个足够容纳文本的滚动宽度,这样会迫使水平滚动条出现
		mListBox2.mScrollWidth = cWidth;
	//==============================================================================
		ListBoxDesc lbDesc3;
		lbDesc3.mOwnerDraw = true;//告诉FreeCL要触发ListBox的自绘事件
		mListBox3.CreateListBox(this, 0, 164, cWidth, cHeight-164, lbDesc3);  
		mListBox3.mLockFlag = ClientLockFlag;
		mListBox3.mOnDrawItem = &MainForm::OnListBoxDrawItem;
		mListBox3.AddItem("ListBox Item用户自绘");
		mListBox3.AddItem("ListBox Item父窗口背景");
		mListBox3.AddItem("ListBox Item3");
		mListBox3.mParentSkin = true;
	//==============================================================================
		
}

bool MainForm::OnListBoxSkin(WinListBox* lbox, const ListBoxSkinData& skin)
{
	//自绘Edit的背景,在水平方向从黄色过度到白色
	Rect cRect = mClientRect;
	DrawGradientBox(skin.mDC, *(RECT*)&cRect, 0xffff, 0xffffff, true);
	//返回false阻止系统绘制背景
	return false;
}

bool MainForm::OnListBoxDrawItem(WinListBox* lbox, const ListBoxItemDrawData& data)
{
	if(data.mSelected)
		::FillRect(data.mDC, (const RECT*)&data.mRect, HBRUSH(1+COLOR_HIGHLIGHT));
	else
	{
		uint color = 0xff;
		HBRUSH brush = ::CreateSolidBrush(color << (8*(data.mIndex%3)));
		::FillRect(data.mDC, (const RECT*)&data.mRect, brush);
		::DeleteObject(brush);
	}

	RECT iRect = *(RECT*)&data.mRect;
	iRect.left += 2;   
	cstr iText = lbox->GetItemText(data.mIndex);
	int iLen = strlen(iText);
	int iMode = ::SetBkMode(data.mDC, TRANSPARENT);
	::DrawText(data.mDC, iText, iLen, &iRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE); 
	::SetBkMode(data.mDC, iMode);
	
	return false;
}

//------------------------------------------------------------------------------------------------------------

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPTSTR lpCmdLine,int nCmdShow)
{	
	MainForm gForm;

	WindowDesc wDesc;
	wDesc.mWindowType = OverlappedType;
	wDesc.mMaxButton = false;
	gForm.CreateWindow(NULL, 309, 103, 400, 300, wDesc);
	
	//设置主窗口的标题文本
	gForm.mCaption = "ListBox Demo";

	//进入消息循环,只有最顶级的根窗口才有必要调用这个函数
	return gForm.Run();
}