ATL/WTL 和SkinLib创建漂亮的对话框

ATL 有两个对话框类,CDialogImpl 和 CAxDialogImpl。CAxDialogImpl 用于掌控 ActiveX 控件的对话框。我们在本文中不包括 ActiveX 控件的内容,因而示例代码使用的是 CDialogImpl。 创建一个新的对话框类,要做三件事: 1. 创建对话框资源 
2. 写一个派生于 CDialogImpl 的新类 

3. 创建一个名为 IDD 的 public 成员变量并将其设置为对话框的资源 ID 然后你就可以像在框架窗口中那样添加消息处理器了。WTL 并没有改变此过程,但确实添加了可用于对话框的附加特性。

控件封装类 
WTL 有许多的控件封装类,对于它们,你应该感到熟悉,因为 WTL 类通常与其在 MFC 中的对应类使用相同的(或者极其相似的)名字。通常方法的名字也是一致的,因此当你使用 WTL 的封装类时你可以使用 MFC 的文档。不过当你需要跳转到某个类的定义时,F12 键就派不上用场了。

下面是内建控件的封装类: 

用户控件:CStatic、CButton、CListBox、CComboBox、CEdit、CScrollBar、CDragListBox 

通用控件:CImageList、CListViewCtrl(MFC 中为 CListCtrl)、CTreeViewCtrl(MFC 中为CTreeCtrl)、CHeaderCtrl、CToolBarCtrl、CStatusBarCtrl、CTabCtrl、CToolTipCtrl、CTrackBarCtrl(MFC 中为 CSliderCtrl)、CUpDownCtrl(MFC 中为CSpinButtonCtrl)、CProgressBarCtrl、CHotKeyCtrl、CAnimateCtrl、CRichEditCtrl、CReBarCtrl、CComboBoxEx、CDateTimePickerCtrl、CMonthCalendarCtrl、CIPAddressCtrl 

MFC 中没有的通用控件封装类:CPagerCtrl、CFlatScrollBar、CLinkCtrl(可点击的超链接,仅在 XP 中可用) 
还有一些 WTL 特有的类:CBitmapButton、CCheckListViewCtrl(具有复选框的列表视图)、CTreeViewCtrlEx 和 CTreeItem (两个类一起使用,CTreeItem 封装了HTREEITEM)、CHyperLink(可点击的超链接,在所有操作系统上均可用)。 


class CBuddyInfoDlg : public CDialogImpl<CBuddyInfoDlg>
{
public:
	CBuddyInfoDlg(void);
	~CBuddyInfoDlg(void);

	enum { IDD = IDD_BUDDYINFODLG };

	BEGIN_MSG_MAP_EX(CBuddyInfoDlg)
		MSG_WM_INITDIALOG(OnInitDialog)
		MSG_WM_CLOSE(OnClose)
		MSG_WM_DESTROY(OnDestroy)
	END_MSG_MAP()

private:
	BOOL OnInitDialog(CWindow wndFocus, LPARAM lInitParam);
	void OnClose();
	void OnDestroy();

	BOOL Init();		// 初始化
	void UnInit();		// 反初始化

public:
	CQQClient * m_lpQQClient;
	HWND m_hMainDlg;
	UINT m_nQQUin;
	BOOL m_bIsGMember;
	UINT m_nGroupCode;

private:
	CSkinDialog m_SkinDlg;

	CSkinStatic m_staNumber, m_staNickName, m_staMarkName;
	CSkinStatic m_staSign;
	CSkinStatic m_staGender, m_staShengXiao, m_staConstel, m_staBlood;
	CSkinStatic m_staBirthday, m_staProvince, m_staPhone, m_staEmail, m_staCollege;
	CSkinStatic m_staCountry, m_staCity, m_staMobile, m_staOccupation, m_staHomepage;
	CSkinStatic m_staPersonal;

	CSkinEdit m_edtNumber, m_edtNickName, m_edtMarkName;
	CSkinEdit m_edtSign;
	CSkinEdit m_edtGender, m_edtShengXiao, m_edtConstel, m_edtBlood;
	CSkinEdit m_edtBirthday, m_edtProvince, m_edtPhone, m_edtEmail, m_edtCollege;
	CSkinEdit m_edtCountry, m_edtCity, m_edtMobile, m_edtOccupation, m_edtHomepage;
	CSkinEdit m_edtPersonal;

	CSkinButton m_btnUpdate, m_btnClose;

	HICON m_hDlgIcon, m_hDlgSmallIcon;
};

在OnInitDialog中创建对话框。

BOOL CBuddyInfoDlg::OnInitDialog(CWindow wndFocus, LPARAM lInitParam)
{
	SetWindowPos(NULL, 0, 0, 580, 480, SWP_NOMOVE);

	CenterWindow();

	// set icons
	m_hDlgIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON));
	SetIcon(m_hDlgIcon, TRUE);
	m_hDlgSmallIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
	SetIcon(m_hDlgSmallIcon, FALSE);

	Init();		// 初始化
}

在cpp文件中对控件进行美化

BOOL CBuddyInfoDlg::Init()
{
	m_SkinDlg.SetBgPic(_T("LoginPanel_window_windowBkg.png"), CRect(40, 86, 152, 155));
	m_SkinDlg.SetMinSysBtnPic(_T("SysBtn\\btn_mini_normal.png"), 
		_T("SysBtn\\btn_mini_highlight.png"), _T("SysBtn\\btn_mini_down.png"));
	m_SkinDlg.SetCloseSysBtnPic(_T("SysBtn\\btn_close_normal.png"), 
		_T("SysBtn\\btn_close_highlight.png"), _T("SysBtn\\btn_close_down.png"));
	m_SkinDlg.SubclassWindow(m_hWnd);


	HDC hDlgBgDC = m_SkinDlg.GetBgDC();


	m_staNumber.SetTransparent(TRUE, hDlgBgDC);
	m_staNumber.SubclassWindow(GetDlgItem(ID_STATIC_NUMBER));
	m_staNickName.SetTransparent(TRUE, hDlgBgDC);
	m_staNickName.SubclassWindow(GetDlgItem(ID_STATIC_NICKNAME));
	m_staMarkName.SetTransparent(TRUE, hDlgBgDC);
	m_staMarkName.SubclassWindow(GetDlgItem(ID_STATIC_MARKNAME));
	m_staSign.SetTransparent(TRUE, hDlgBgDC);
	m_staSign.SubclassWindow(GetDlgItem(ID_STATIC_SIGN));
	m_staGender.SetTransparent(TRUE, hDlgBgDC);
	m_staGender.SubclassWindow(GetDlgItem(ID_STATIC_GENDER));
	m_staShengXiao.SetTransparent(TRUE, hDlgBgDC);
	m_staShengXiao.SubclassWindow(GetDlgItem(ID_STATIC_SHENGXIAO));
	m_staConstel.SetTransparent(TRUE, hDlgBgDC);
	m_staConstel.SubclassWindow(GetDlgItem(ID_STATIC_CONSTEL));
	m_staBlood.SetTransparent(TRUE, hDlgBgDC);
	m_staBlood.SubclassWindow(GetDlgItem(ID_STATIC_BLOOD));
	m_staBirthday.SetTransparent(TRUE, hDlgBgDC);
	m_staBirthday.SubclassWindow(GetDlgItem(ID_STATIC_BIRTHDAY));
	m_staProvince.SetTransparent(TRUE, hDlgBgDC);
	m_staProvince.SubclassWindow(GetDlgItem(ID_STATIC_PROVINCE));
	m_staPhone.SetTransparent(TRUE, hDlgBgDC);
	m_staPhone.SubclassWindow(GetDlgItem(ID_STATIC_PHONE));
	m_staEmail.SetTransparent(TRUE, hDlgBgDC);
	m_staEmail.SubclassWindow(GetDlgItem(ID_STATIC_EMAIL));
	m_staCollege.SetTransparent(TRUE, hDlgBgDC);
	m_staCollege.SubclassWindow(GetDlgItem(ID_STATIC_COLLEGE));
	m_staCountry.SetTransparent(TRUE, hDlgBgDC);
	m_staCountry.SubclassWindow(GetDlgItem(ID_STATIC_COUNTRY));
	m_staCity.SetTransparent(TRUE, hDlgBgDC);
	m_staCity.SubclassWindow(GetDlgItem(ID_STATIC_CITY));
	m_staMobile.SetTransparent(TRUE, hDlgBgDC);
	m_staMobile.SubclassWindow(GetDlgItem(ID_STATIC_MOBILE));
	m_staOccupation.SetTransparent(TRUE, hDlgBgDC);
	m_staOccupation.SubclassWindow(GetDlgItem(ID_STATIC_OCCUPATION));
	m_staHomepage.SetTransparent(TRUE, hDlgBgDC);
	m_staHomepage.SubclassWindow(GetDlgItem(ID_STATIC_HOMEPAGE));
	m_staPersonal.SetTransparent(TRUE, hDlgBgDC);
	m_staPersonal.SubclassWindow(GetDlgItem(ID_STATIC_PERSONAL));


	m_edtNumber.SetTransparent(TRUE, hDlgBgDC);
	m_edtNumber.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtNumber.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtNumber.SubclassWindow(GetDlgItem(ID_EDIT_NUMBER));
	m_edtNickName.SetTransparent(TRUE, hDlgBgDC);
	m_edtNickName.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtNickName.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtNickName.SubclassWindow(GetDlgItem(ID_EDIT_NICKNAME));
	m_edtMarkName.SetTransparent(TRUE, hDlgBgDC);
	m_edtMarkName.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtMarkName.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtMarkName.SubclassWindow(GetDlgItem(ID_EDIT_MARKNAME));
	m_edtSign.SetTransparent(TRUE, hDlgBgDC);
	m_edtSign.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtSign.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtSign.SetMultiLine(TRUE);
	m_edtSign.SubclassWindow(GetDlgItem(ID_EDIT_SIGN));
	m_edtGender.SetTransparent(TRUE, hDlgBgDC);
	m_edtGender.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtGender.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtGender.SubclassWindow(GetDlgItem(ID_EDIT_GENDER));
	m_edtShengXiao.SetTransparent(TRUE, hDlgBgDC);
	m_edtShengXiao.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtShengXiao.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtShengXiao.SubclassWindow(GetDlgItem(ID_EDIT_SHENGXIAO));
	m_edtConstel.SetTransparent(TRUE, hDlgBgDC);
	m_edtConstel.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtConstel.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtConstel.SubclassWindow(GetDlgItem(ID_EDIT_CONSTEL));
	m_edtBlood.SetTransparent(TRUE, hDlgBgDC);
	m_edtBlood.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtBlood.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtBlood.SubclassWindow(GetDlgItem(ID_EDIT_BLOOD));
	m_edtBirthday.SetTransparent(TRUE, hDlgBgDC);
	m_edtBirthday.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtBirthday.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtBirthday.SubclassWindow(GetDlgItem(ID_EDIT_BIRTHDAY));
	m_edtProvince.SetTransparent(TRUE, hDlgBgDC);
	m_edtProvince.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtProvince.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtProvince.SubclassWindow(GetDlgItem(ID_EDIT_PROVINCE));
	m_edtPhone.SetTransparent(TRUE, hDlgBgDC);
	m_edtPhone.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtPhone.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtPhone.SubclassWindow(GetDlgItem(ID_EDIT_PHONE));
	m_edtEmail.SetTransparent(TRUE, hDlgBgDC);
	m_edtEmail.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtEmail.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtEmail.SubclassWindow(GetDlgItem(ID_EDIT_EMAIL));
	m_edtCollege.SetTransparent(TRUE, hDlgBgDC);
	m_edtCollege.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtCollege.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtCollege.SubclassWindow(GetDlgItem(ID_EDIT_COLLEGE));
	m_edtCountry.SetTransparent(TRUE, hDlgBgDC);
	m_edtCountry.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtCountry.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtCountry.SubclassWindow(GetDlgItem(ID_EDIT_COUNTRY));
	m_edtCity.SetTransparent(TRUE, hDlgBgDC);
	m_edtCity.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtCity.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtCity.SubclassWindow(GetDlgItem(ID_EDIT_CITY));
	m_edtMobile.SetTransparent(TRUE, hDlgBgDC);
	m_edtMobile.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtMobile.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtMobile.SubclassWindow(GetDlgItem(ID_EDIT_MOBILE));
	m_edtOccupation.SetTransparent(TRUE, hDlgBgDC);
	m_edtOccupation.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtOccupation.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtOccupation.SubclassWindow(GetDlgItem(ID_EDIT_OCCUPATION));
	m_edtHomepage.SetTransparent(TRUE, hDlgBgDC);
	m_edtHomepage.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtHomepage.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtHomepage.SubclassWindow(GetDlgItem(ID_EDIT_HOMEPAGE));
	m_edtPersonal.SetTransparent(TRUE, hDlgBgDC);
	m_edtPersonal.SetBgNormalPic(_T("frameBorderEffect_normalDraw.png"), CRect(2,2,2,2));
	m_edtPersonal.SetBgHotPic(_T("frameBorderEffect_mouseDownDraw.png"), CRect(2,2,2,2));
	m_edtPersonal.SetMultiLine(TRUE);
	m_edtPersonal.SubclassWindow(GetDlgItem(ID_EDIT_PERSONAL));


	m_btnUpdate.SetButtonType(SKIN_PUSH_BUTTON);
	m_btnUpdate.SetTransparent(TRUE, hDlgBgDC);
	m_btnUpdate.SetBgPic(_T("Button\\btn_normal.png"), _T("Button\\btn_highlight.png"),
		_T("Button\\btn_down.png"), _T("Button\\btn_focus.png"));
	m_btnUpdate.SubclassWindow(GetDlgItem(ID_BTN_UPDATE));


	m_btnClose.SetButtonType(SKIN_PUSH_BUTTON);
	m_btnClose.SetTransparent(TRUE, hDlgBgDC);
	m_btnClose.SetBgPic(_T("Button\\btn_normal.png"), _T("Button\\btn_highlight.png"),
		_T("Button\\btn_down.png"), _T("Button\\btn_focus.png"));
	m_btnClose.SubclassWindow(GetDlgItem(ID_BTN_CLOSE));


	return TRUE;
}
调用void CBuddyInfoDlg::UnInit()进行反初始化。

最终的效果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值