Windows mobile 透明/半透明控件组(皮肤控件)

 

关键词:Windows mobile 透明控件组(皮肤控件),YCtrls,YCode,Y控件,Y代码
本源码可以解决以下疑问:
windows mobile 下如何读写文件,如何读写Ini文件,如何绘图,如何显示图片,如何实现PNG图片,如何进行字符串处理,如何判断文件的存在,如何查找窗口,如何只运行一个程序实例,如何获取文件夹下的所有文件.......
 
 
 
 
                          版权声明
   本控件组版权归作者所有,未经同意,不得用于商业用途。任何人未经同意,不得擅自更改或在原基础上进行再次开发。
 
    当前Windows mobile下的皮肤控件还很少,而且实现透明的更加少。本人曾经在Delphi开发过一套透明控件,于是决定把它移植到windows mobile系统下。在仿真器上和三星I718+上测试过,更换皮肤速度优化得相当快了,在Windows mobile下实属不易啊。
 
    整套控件完全脱离MFC类,有自己的:CList、CString等组件类,所有控件由CWin这个透明窗体类派生,CWin派生CForm和CCtrl类,在CCtrl类上派生各个透明控件,有:CBtt(按钮控件)、CCheckBox、CRadioBox、CPanel、CLabel、CEdit、CWord、CPage、CGrid......,CApp为程序的应用类,担负了皮肤的加载等工作和处理系统消息的功能。
 
    按钮控件有按下、放下、焦点、禁止四种状态,可以实现透明、半透明显示。图片可以是伸展方式或平铺方式显示,伸展方式可以自适应控件的大小。
 
    希望本源码对初学Windows mobile编程的人有所帮助!
 
 
皮肤例子(以后会有更多皮肤):
1、蓝色经典
按此在新窗口浏览图片
2、绿色经典
按此在新窗口浏览图片
用VS调试,请设置外置卡目录到Skin下
源码下载:
部分源码:
#include "YCtrls.h"
/*********************************************************************************************
*        CWin               *
*                        *
*  YCode Y代码 YCtrl Y控件 Windows Mobile 透明控件 2008 YCtrl 1.0       *
*   作者:代码客(卢益贵) QQ:48092788  http://blog.csdn.net/guestcode         *
*                        *
**********************************************************************************************/
CWin::CWin()
{
 m_cidClassID = CID_Y_WIN;
 m_hWnd = NULL;
 m_nLeft = 0;
 m_nTop = 26;
 m_nWidth = 240;
 m_nHeigth = 320 - 26;
 m_wsWinState = WS_Y_NORMAL;
 m_rcClientRect.left = 0;
 m_rcClientRect.top = 0;
 m_rcClientRect.right = m_nWidth;
 m_rcClientRect.bottom = m_nHeigth;
 m_hBitmap = NULL;
 m_hDC = NULL;
 m_pCtrls = new CList;
 m_pParent = NULL;
 m_pOwner = NULL;
 m_bEnabled = TRUE;
 m_bStretch = TRUE;
 m_pszText = NULL;
 m_nTextLen = 0;
 m_hFont = NULL;
 m_clTextColor = 0xFFFF00;
 m_dwMouseState = 0;
 m_hPen = NULL;
}
CWin::~CWin()
{
 CWin *pWin;
 while( GetCtrlCount() )
 {
  pWin = GetCtrl( 0 );
  delete pWin;
 }
 if( m_pParent )
  m_pParent->DeleteCtrl( this );
 if( m_pCtrls )
  delete m_pCtrls;
    if( m_hWnd )
        DestroyWindow( m_hWnd );
 if( m_hBitmap )
   DeleteObject( m_hBitmap );
 if( m_hDC )
  DeleteDC( m_hDC );
 
 if( m_pszText )
  free( (void *)m_pszText );
 if( m_hFont )
  DeleteObject( m_hFont );
 if( m_hPen )
  DeleteObject( m_hPen );
}
void CWin::OnCreated( void )
{
}
void CWin::CreateStyle( pCREATESTYLE pCreateStyle )
{
 pCreateStyle->dwStyle = WS_VISIBLE;
 pCreateStyle->dwExStyle = 0;
}
void CWin::CreateBitmap( void )
{
 if( !m_hWnd )
  return;
 if( m_hBitmap )
   DeleteObject( m_hBitmap );
 if( !m_hDC )
  m_hDC = CreateCompatibleDC( GetDC( m_hWnd ) );
 if( m_hBitmap )
   DeleteObject( m_hBitmap );
 m_hBitmap = CreateCompatibleBitmap( GetDC( m_hWnd ), m_nWidth, m_nHeigth );
 SelectObject( m_hDC, m_hBitmap );
}
BOOL CWin::Create( CWin *pOwner, CWin *pParent, HMENU hMenu, int nLeft, int nTop, int nWidth, int nHeigth,
       LPCWSTR lpszClassName, LPCWSTR lpszCaption )
{
 HWND   hParent;
 m_pParent = pParent;
 m_pOwner = pOwner;
 if( NULL == pParent )
  hParent = NULL;
 else
 {
  hParent = pParent->GetHandle();
  m_pParent->AddCtrl( this );
 }
 CREATESTYLE cs;
 CreateStyle( &cs );
    m_hWnd = CreateWindowEx( cs.dwExStyle, lpszClassName,  lpszCaption,  cs.dwStyle,
        nLeft, nTop, nWidth, nHeigth, hParent,  hMenu, g_pApp->GetInstance(), (PVOID)this );
 
 m_nTextLen = lstrlen( lpszCaption );
 m_pszText = (WCHAR *)malloc( m_nTextLen * 2 + 2 );
 lstrcpy( m_pszText, lpszCaption );
 if( m_hWnd )
 {
  m_nLeft = nLeft;
  m_nTop = nTop;
  m_nWidth = nWidth;
  m_nHeigth = nHeigth;
  GetClientRect( m_hWnd, &m_rcClientRect );
  SetWindowLong( m_hWnd, GWL_USERDATA, (LONG) this );
  if( SetWindowLong( m_hWnd, GWL_USERDATA, (LONG) this ) )
  {
   GetObject( (HFONT) GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &m_lFont );
   m_hFont = CreateFontIndirect( &m_lFont );
   CreateBitmap();
   OnCreated();
   OnDraw();
   return TRUE;
  }
 }
 
 return FALSE;  
}
LRESULT CWin::WindowProc( HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam )
{
 LRESULT r;
 switch( uMsg )
    {
  case WM_PAINT:
   OnPaint( hWnd );
   return 0;
  case WM_LBUTTONDOWN:
   m_dwMouseState = MS_Y_DOWN;
   return DefWindowProc( hWnd, uMsg, wParam, lParam );
  case WM_LBUTTONUP:   
   r = DefWindowProc( hWnd, uMsg, wParam, lParam );
   if( (m_dwMouseState & MS_Y_DOWN) && (((short)LOWORD(lParam)) >= 0) && (((short)LOWORD(lParam))  <= m_nWidth) &&
    (((short)HIWORD(lParam)) >= 0) && (((short)HIWORD(lParam)) <= m_nHeigth) )
    SendMessage( m_hWnd, WM_LBUTTonCLICK, 0, 0);
   m_dwMouseState &= (~(MS_Y_DOWN | MS_Y_MOVE));
   return r;
  case WM_MOUSEMOVE:
   if( (((short)LOWORD(lParam)) < 0) || (((short)LOWORD(lParam)) > m_nWidth) || (((short)HIWORD(lParam)) < 0) || (((short)HIWORD(lParam)) > m_nHeigth) )
   {
    m_dwMouseState &= (~MS_Y_MOVE);
    SendMessage( m_hWnd, WM_MOUSELEAVE, 0, 0);    
   }else
    m_dwMouseState |= MS_Y_MOVE;
   return DefWindowProc( hWnd, uMsg, wParam, lParam );
  case WM_MOVE:
   m_wsWinState = WS_Y_MOVE;
   m_nLeft = (int)LOWORD(lParam);
   m_nTop = (int)HIWORD(lParam);
   r = DefWindowProc( hWnd, uMsg, wParam, lParam );
   if( m_pParent )
    m_pParent->OnPaint();
   ChangeSkin();
   return r;
  case WM_SIZE:
   switch( wParam )
   {
    case SIZE_MINIMIZED:
     m_wsWinState = WS_Y_MIN;
     break;
    case SIZE_MAXIMIZED:
     m_wsWinState = WS_Y_MAX;
     break;
    case SIZE_MAXSHOW:
     m_wsWinState = WS_Y_MAXSHOW;
     break;
    case SIZE_MAXHIDE:
     m_wsWinState = WS_Y_MAXHIDE;
     break;
    default:
     m_wsWinState = WS_Y_NORMAL;
   }
   m_nWidth = (int)LOWORD(lParam);
   m_nHeigth = (int)HIWORD(lParam);
   GetClientRect( hWnd, &m_rcClientRect );
   r = DefWindowProc( hWnd, uMsg, wParam, lParam );   
   if( m_pParent )
    m_pParent->OnPaint();
   CreateBitmap();
   ChangeSkin();
   return r;
  case WM_GETTEXT:
   if( m_nTextLen < (int) wParam )
   {
    memcpy( (void *)lParam, (void *)m_pszText, (int) m_nTextLen * 2 );
    ((WCHAR *)lParam)[(int) m_nTextLen] = 0;
   }else
   {
    memcpy( (void *)lParam, (void *)m_pszText, (int) wParam * 2 );
    ((WCHAR *)lParam)[(int) wParam] = 0;
   }
   return 0;
  case WM_SETTEXT:
   if( m_pszText )
    free( (void *)m_pszText );
   m_nTextLen = lstrlen( (WCHAR *)lParam );
   m_pszText = (WCHAR *) malloc( m_nTextLen + 2 );
   lstrcpy( m_pszText, (WCHAR *)lParam );
   return 0;
  case WM_GETTEXTLENGTH:
   return m_nTextLen;
        default:
   return DefWindowProc( hWnd, uMsg, wParam, lParam );
    } 
}
LRESULT CALLBACK CWin::WinProc( HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam )
{
 CWin *pThis;
    pThis = (CWin*) GetWindowLong( hWnd, GWL_USERDATA );
    if( pThis )
    {
        return pThis->WindowProc( hWnd, uMsg, wParam, lParam );
    }
    return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
BOOL CWin::RegClass( LPCWSTR lpszClassName )
{
 WNDCLASS    wc;
 ZeroMemory( &wc, sizeof( wc ));
 wc.lpfnWndProc = CWin::WinProc;
    wc.hInstance = g_pApp->GetInstance();
    wc.lpszClassName = lpszClassName;
 wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
 if( NULL == RegisterClass( &wc ))
        return FALSE;
 else
  return TRUE;
}
void CWin::OnDraw( void )
{
}
void CWin::OnPaint( HDC hdc )
{
 BitBlt( hdc, 0, 0, m_nWidth, m_nHeigth, m_hDC, 0, 0, SRCCOPY );
}
void CWin::OnPaint( HWND hWnd )
{
 HDC hdc;
 PAINTSTRUCT ps;
 hdc = BeginPaint(hWnd, &ps);
 OnPaint( hdc ); 
 EndPaint(hWnd, &ps);
}
void CWin::OnPaint( void )
{
 HDC hdc;
 hdc = GetDC( m_hWnd );
 OnPaint( hdc ); 
 ReleaseDC( m_hWnd, hdc );
}
void CWin::Invalidate( void )
{
 if( m_hWnd )
 {
  OnDraw();
  OnPaint();
  //InvalidateRect( m_hWnd , &m_rcClientRect, TRUE );
 }
}
void CWin::GetCtrlBKGND( CWin *pCtrl, HDC hdc )
{
 POINT p;
 p.x = 0;
 p.y = 0;
 ClientToScreen( pCtrl->GetHandle(), &p);
 ScreenToClient( m_hWnd, &p);
 BitBlt( hdc, 0, 0, pCtrl->GetWidth(), pCtrl->GetHeigth(), m_hDC, p.x, p.y, SRCCOPY );
}
HWND CWin::GetHandle(void)
{
 return m_hWnd;
}
void CWin::SetParent( CWin *pParent )
{
 if( (m_pParent != pParent) && pParent )
 {
  if( m_pParent )
   m_pParent->DeleteCtrl( this );
  m_pParent = pParent;
  m_pParent->AddCtrl( this );
  ::SetParent( m_hWnd, m_pParent->GetHandle() );
  Invalidate();
 }
}
CWin *CWin::GetParent( void )
{
 return m_pParent;
}
CWin *CWin::GetOwner( void )
{
 return m_pOwner;
}
int CWin::GetLeft( void )
{
 return m_nLeft;
}
int CWin::GetTop( void )
{
 return m_nTop;
}
int CWin::GetWidth( void )
{
 return m_nWidth;
}
int CWin::GetHeigth( void )
{
 return m_nHeigth;
}
void CWin::SetPos( int nLeft, int nTop, int nWidth, int nHeigth )
{
 m_nLeft = nLeft;
 m_nTop = nTop;
 m_nWidth = nWidth;
 m_nHeigth = nHeigth;
 if( m_pParent )
  m_pParent->OnPaint();
 MoveWindow( m_hWnd, nLeft, nTop, nWidth, nHeigth, TRUE );
 Invalidate();
}
void  CWin::AddCtrl( CWin *pCtrl )
{
 m_pCtrls->Add( pCtrl );
}
void  CWin::DeleteCtrl( CWin *pCtrl )
{
 m_pCtrls->Delete( pCtrl );
}
int  CWin::GetCtrlCount( void )
{
 return m_pCtrls->GetCount();
}
CWin *CWin::GetCtrl( int nIndex )
{
 return (CWin *)m_pCtrls->GetItem( nIndex );
}
void CWin::SetVisible( BOOL bVisible )
{
 if( bVisible != GetVisible() )
  ShowWindow( m_hWnd, bVisible );
}
BOOL CWin::GetVisible( void )
{
 return IsWindowVisible( m_hWnd );
}
void CWin::SetEnabled( BOOL bEnabled )
{
 if( bEnabled != GetEnabled() )
 {
  EnableWindow( m_hWnd, bEnabled );
  OnDraw();
  OnPaint();
 }
}
BOOL CWin::GetEnabled( void )
{
 return IsWindowEnabled( m_hWnd );
}
void CWin::SetStretch( BOOL bStretch )
{
 if( m_bStretch != bStretch )
 {
  m_bStretch = bStretch;
  ChangeSkin();
 }
}
BOOL CWin::GetStretch( void )
{
 return m_bStretch;
}
void CWin::ChangeSkin( void )
{
 int i; 
 Invalidate();
 for( i = 0; i < GetCtrlCount(); i++ )
  GetCtrl( i )->ChangeSkin();
}
LOGFONT CWin::GetFont( void )
{
 return m_lFont;
}
void CWin::SetFont( LOGFONT * lFont )
{
 m_lFont = *lFont;
 if( m_hFont )
  DeleteObject(m_hFont);
 m_hFont = CreateFontIndirect( &m_lFont );
 SelectFont( GetDC( m_hWnd ), m_hFont );
 Invalidate();
}
void CWin::SetTextColor( COLORREF clTextColor )
{
 if( m_clTextColor != clTextColor )
 {
  m_clTextColor = clTextColor;
  Invalidate();
 }
}
COLORREF CWin::GetTextColor( void )
{
 return m_clTextColor;
}
LPCWSTR CWin::GetText( void )
{
 return m_pszText;
}
int CWin::GetTextLen( void )
{
 return m_nTextLen;
}
void CWin::SetText( LPCWSTR lpszText )
{
 SendMessage( m_hWnd, WM_SETTEXT, lstrlen( lpszText ), (UINT) lpszText );
}

DWORD CWin::GetClassID( void )
{
 return m_cidClassID;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值