📄 basecontrol.cpp
字号:
/*******************************************************************************
* Copyright (C) 1980-2008 Xumingxsh
* All rights reserved.
*
* 文件名称: BaseControl.cpp
* 创建日期: 2008-04-28
* 创 建 人: 徐敏荣
* 说 明: 类BaseControl的实现文件
*-------------------------------------------------------------------------------
* 版本 日 期 修改人 修改说明
*-------------------------------------------------------------------------------
* 1.0.0 2008-04-28 徐敏荣 完成初始版本
*******************************************************************************/
#include "stdafx.h"
#include "BaseControl.h"
#include "LogFile.h"
#include "ItemKey.h"
#include "ImgLib.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
HWND BaseControl::CreateControl(CreateWndStruct &controlStruct)
{
return ::CreateWindowEx(
controlStruct.dwExStyle,
controlStruct.lpszClass,
controlStruct.lpszName,
controlStruct.style,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
controlStruct.hwndParent,
controlStruct.hMenu,
controlStruct.hInstance,
controlStruct.lpCreateParams);
}
// 构造函数
BaseControl::BaseControl()
{
m_ClickEvent = NULL;
}
// 析构函数
BaseControl::~BaseControl()
{
}
// 设置控件的位置
void BaseControl::OnMove()
{
::MoveWindow(this->m_hWnd, this->m_CProperty.m_Rect.left, this->m_CProperty.m_Rect.top,
this->m_CProperty.m_Rect.right - this->m_CProperty.m_Rect.left,
this->m_CProperty.m_Rect.bottom - this->m_CProperty.m_Rect.top, true);
}
// 创建控件的位置
void BaseControl::CreateControl(char* chValue, char* key, HWND hParentWnd)
{
this->m_CProperty.SetValue(chValue, key);
this->m_parentWnd = hParentWnd;
this->m_CProperty.m_WndStruct.hwndParent = this->m_parentWnd;
this->m_CProperty.m_WndStruct.hMenu = (HMENU)this->m_nID;
this->m_hWnd = BaseControl::CreateControl(this->m_CProperty.m_WndStruct);
// 如果不是文本框,且文本不为空,则设置控件文本
if (this->m_CProperty.m_strText != "")
{
::SetWindowText(this->m_hWnd, TEXT(this->m_CProperty.m_strText));
}
}
// 画出控件
void BaseControl::DrawItem(WPARAM &wParam, LPARAM &lParam)
{
LPDRAWITEMSTRUCT pdis;
pdis = (LPDRAWITEMSTRUCT) lParam;
TCHAR szTemp[MAX_PATH];
// 背景图片
if (this->m_CProperty.m_strImgSrc != "")
{
if (this->m_CProperty.m_nImgCount == 0)
{
this->m_CProperty.m_nImgCount = 1;
}
if (this->m_CProperty.m_nImgIndex == 0)
{
this->m_CProperty.m_nImgIndex = 1;
}
// 将取得的图片添加到控件上
this->DrawBitBmp(pdis->hwndItem, this->m_CProperty.m_strImgSrc,
this->m_CProperty.m_nImgCount, this->m_CProperty.m_nImgIndex);
}
else
{
// 设置背景颜色
FillRect(pdis->hDC, &pdis->rcItem, CreateSolidBrush(this->m_CProperty.m_BgColor));
// 如果控件无背景图片,则用户选择了控件后,背景颜色改变
switch(pdis->itemState)
{
case ODS_FOCUS:
{
::FillRect(pdis->hDC, &pdis->rcItem, CreateSolidBrush(this->m_CProperty.m_FocColor));
}
break;
case ODS_DISABLED:
{
::FillRect(pdis->hDC, &pdis->rcItem, CreateSolidBrush(this->m_CProperty.m_EnbColor));
}
break;
}
}
// 设置控件的字体
if (GetWindowText(pdis->hwndItem, szTemp, MAX_PATH) > 0)
{
this->SetTitleText(pdis->hwndItem, szTemp, NULL,
this->m_CProperty.m_TextFormat, this->m_CProperty.m_ForColor);
}
if (!this->m_CProperty.m_bIsShow)
{
::ShowWindow(pdis->hwndItem, SW_HIDE);
}
else
{
::ShowWindow(pdis->hwndItem, SW_SHOW);
}
}
// 给控件设置图片
void BaseControl::DrawBitBmp(HWND hwnd, CString strFilePath, int nCount, int nIndex)
{
if (nIndex < 1)
{
nIndex = 1;
}
HBITMAP hBmp;
CBMPLib::LoadMap(strFilePath, hBmp);
if (hBmp)
{
BITMAP bmp;
::GetObject(hBmp, sizeof(BITMAP), &bmp);
long nWidth;
if (nCount < 1)
{
nCount = 1;
}
nWidth = bmp.bmWidth/nCount;
RECT rect;
rect.left = (nIndex - 1)*nWidth;
rect.top = 0;
rect.right= nWidth;
rect.bottom = bmp.bmHeight;
RECT rc;
rc.left = 0;
rc.right = 0;
rc.top = 0;
rc.bottom = 0;
::GetClientRect(hwnd, &rc);
HDC hDC = ::GetDC(hwnd);
HDC hMemoryDC = ::CreateCompatibleDC(hDC);
HBITMAP hOldBitMap = (HBITMAP)::SelectObject(hMemoryDC, hBmp);
::StretchBlt(hDC, 0, 0, rc.right, rc.bottom, hMemoryDC,
rect.left, rect.top, rect.right, rect.bottom, SRCCOPY);
//开辟了内存,用完一定要记得释放
::SelectObject(hMemoryDC, hOldBitMap);
::ReleaseDC(hwnd, hDC);
}
}
// 设置控件字体
void BaseControl::SetTitleText(HWND hWnd, TCHAR* chLabel,
HFONT hFont, UINT textFormat, COLORREF rgb)
{
if (!*chLabel || strcmp(chLabel, "") == 0)
{
return;
}
//显示文字的区域
RECT rt,rect;
::GetClientRect(hWnd, &rt);
::SetRect(&rect, rt.left, rt.top, rt.right, rt.bottom);
// 设置字体
HFONT hOldFont;
if (this->m_CProperty.m_bIsBold)
{
this->m_CProperty.m_logfont.lfWeight = 700;
}
else
{
this->m_CProperty.m_logfont.lfWeight = 0;
}
hFont = ::CreateFontIndirect(&this->m_CProperty.m_logfont);
HDC dc = ::GetDC(hWnd);
if(hFont != NULL)//使用配置的字体,否则使用默认的字体
{
hOldFont = (HFONT)::SelectObject(dc, hFont);
}
//背景透明效果
::SetBkMode(dc,TRANSPARENT);
//文字的颜色
COLORREF hOldColor;
hOldColor = ::SetTextColor(dc, rgb);
::DrawText( dc, chLabel, -1, &rect, textFormat);
//释放对象
::SetTextColor(dc, hOldColor);
::SelectObject(dc, hOldFont);
::ReleaseDC(hWnd,dc);
}
// 显示或隐藏控件
void BaseControl::ShowWnd(bool isSHOW)
{
if (isSHOW && !this->m_CProperty.m_bIsShow)
{
this->m_CProperty.m_bIsShow = true;
::ShowWindow(m_hWnd, SW_SHOW);
}
if (!isSHOW && this->m_CProperty.m_bIsShow)
{
this->m_CProperty.m_bIsShow = false;
::ShowWindow(m_hWnd, SW_HIDE);
}
}
// 设置控件文本
void BaseControl::SetText(CString strText)
{
this->m_CProperty.m_strText = strText;
::SetWindowText(this->m_hWnd, TEXT(this->m_CProperty.m_strText));
}
void BaseControl::SetText(char* chText)
{
CString strText(chText);
this->SetText(strText);
}
// 设置控件背景图片
void BaseControl::SetImg(int nIndex)
{
if (nIndex <= this->m_CProperty.m_nImgCount)
{
this->m_CProperty.m_nImgIndex = nIndex;
// 将取得的图片添加到控件上
this->DrawBitBmp(this->m_hWnd, this->m_CProperty.m_strImgSrc,
this->m_CProperty.m_nImgCount, this->m_CProperty.m_nImgIndex);
}
}
CString BaseControl::GetText()
{
TCHAR chInput[MAX_PATH] = {0};
::GetWindowText(this->m_hWnd, chInput, MAX_PATH);
return CString(chInput);
}
void BaseControl::SetEnable(bool isAble)
{
this->m_CProperty.m_bIsEnable = !isAble;
::EnableWindow(this->m_hWnd, isAble);
}
bool BaseControl::IsCurContrl(int nID)
{
if (nID == this->m_nID)
{
return true;
}
return false;
}
void BaseControl::OnClick()
{
if (this->m_ClickEvent != NULL)
{
m_ClickEvent();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -