// CustomEdit.cpp#include "stdafx.h"#include "customedit.h"#include "../Graphics/BitmapX.h"#include "../Graphics/Display.h"CCustomEdit::CCustomEdit(void){ m_lX = 0; m_lY = 0; m_lWidth = 0; m_lHeight = 16; m_lSize = MAX_INPUT_DATA; m_hFatherWnd = NULL; m_hWnd = NULL; m_hDC = NULL; m_DefProc = NULL; memset( m_strInputBuffer, 0, MAX_INPUT_DATA ); m_pBitmap = NULL; m_pDisplay = NULL;}CCustomEdit::~CCustomEdit(void){ if( m_pDisplay != NULL ) m_pDisplay->RemoveBitmap( m_pBitmap );}// -------------------------------------------------------// Name: Create()// Describe: 创建并初始化// -------------------------------------------------------long CCustomEdit::Create( HWND hMainWnd, CDisplay* pDisplay, long x, long y, long width, long height, bool bIsPass, bool bIsLimit ){ m_pDisplay = pDisplay; m_lX = x; m_lY = y; m_lWidth = width; m_lHeight = height; m_hFatherWnd = hMainWnd; // 创建Edit窗口 DWORD dwStyle = WS_CHILD | ES_LEFT | ES_AUTOHSCROLL; if( bIsPass ) { dwStyle |= ES_PASSWORD; } if( bIsLimit ) { dwStyle &= ~ES_AUTOHSCROLL; } m_hWnd = CreateWindow( "EDIT", NULL, dwStyle, x, y, width, height, hMainWnd, NULL, (HINSTANCE)GetWindowLong( hMainWnd, GWL_HINSTANCE ), NULL ); if( m_hWnd == NULL ) return -1; // 初始化 UpdateWindow( m_hWnd ); ShowWindow( m_hWnd, SW_SHOW); m_hDC = GetDC( m_hWnd ); m_DefProc = (WNDPROC)GetWindowLong( m_hWnd, GWL_WNDPROC ); SetWindowLong( m_hWnd, GWL_WNDPROC, (LONG)CustomEditProc); m_pBitmap = pDisplay->CreateSurface( width, height ); m_pBitmap->SetColorKey( RGB(255, 0, 0) ); return 0;}// -------------------------------------------------------// Name: Show()// Describe: 显示文本编辑框// -------------------------------------------------------long CCustomEdit::Show( ){ static HDC hDC; ((LPDIRECTDRAWSURFACE7)(m_pBitmap->m_pBuffer))->GetDC( &hDC ); BitBlt( hDC, 0, 0, m_lWidth, m_lHeight, m_hDC, 0, 0, SRCCOPY ); ((LPDIRECTDRAWSURFACE7)(m_pBitmap->m_pBuffer))->ReleaseDC( hDC ); m_pDisplay->DrawSurface( m_lX, m_lY, m_pBitmap, m_pDisplay->GetBackSurface() ); return 0;}// -------------------------------------------------------// Name: GetString()// Describe: 获取文本编辑框的文字串// -------------------------------------------------------char* CCustomEdit::GetString( ){ memset( m_strInputBuffer, 0, MAX_INPUT_DATA ); ::GetWindowText( m_hWnd, m_strInputBuffer, MAX_INPUT_DATA ); return m_strInputBuffer;}list<CCustomEdit*> g_listCustomEdit;typedef list<CCustomEdit* >::iterator CustomEditIterator;CCustomEdit* g_pActiveCustomEdit = NULL;// 文本输入框创建函式CCustomEdit* CreateCustomEdit( HWND hMainWnd, CDisplay* pDisplay, long x, long y, long width, long height, bool bIsPass, bool bIsLimit ){ CCustomEdit* pEdit = new CCustomEdit; assert( pEdit ); pEdit->Create( hMainWnd, pDisplay, x, y, width, height, bIsPass, bIsLimit ); g_listCustomEdit.push_back( pEdit ); return pEdit;}// 设置活动编辑框void SetActiveCustomEdit( CCustomEdit* pEdit ){ if( pEdit != NULL ) g_pActiveCustomEdit = pEdit; SetFocus( g_pActiveCustomEdit->m_hWnd );}// 显示活动编辑框void ShowCustomEdit( ){ if( g_listCustomEdit.size() == 0 ) return; for( CustomEditIterator it=g_listCustomEdit.begin(); it!=g_listCustomEdit.end(); it++ ) { CCustomEdit* p = *it; p->Show(); }}// 移除活动编辑框void RemoveCustomEdit( CCustomEdit* pEdit ){ if( g_listCustomEdit.size() == 0 ) { SAFE_DELETE( pEdit ); return; } g_listCustomEdit.remove( pEdit ); SAFE_DELETE( pEdit );}// 施放活动编辑框void ReleaseCustomEdit( ){ if( g_listCustomEdit.size() == 0 ) return; for( CustomEditIterator it=g_listCustomEdit.begin(); it!=g_listCustomEdit.end(); it++ ) { CCustomEdit* p = *it; SAFE_DELETE( p ); }}// 文本输入框消息响应函数LRESULT CALLBACK CustomEditProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ switch (message) { case WM_KEYDOWN: { switch(wParam) { case VK_ESCAPE: break; case VK_RETURN: { // 此处使用了自定义消息 // WM_USER+10 : 响应输入确认消息 SendMessage( g_pActiveCustomEdit->m_hFatherWnd, WM_USER+10, 0, 0); } break; case VK_TAB: { } break; default: break; } } break; case WM_CHAR: { } break; case WM_LBUTTONDOWN: break; case WM_PAINT: break; default: break; } return CallWindowProc( g_pActiveCustomEdit->m_DefProc, hWnd, message, wParam, lParam );}