⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 seedit.cpp

📁 一个自定义的SPIN控件
💻 CPP
字号:
/////////////////////////////////////////////////////////////////////////////
// SEEdit.cpp : implementation file
//
// Written by Chris Maunder (chrismaunder@codeguru.com)
// Copyright (c) 1998.
//
// The code contained in this file is based on the original
// CInPlaceEdit from http://www.codeguru.com/listview/edit_subitems.shtml
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. If 
// the source code in  this file is used in any commercial application 
// then acknowledgement must be made to the author of this file 
// (in whatever form you wish).
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage/loss of business that
// this product may cause.
//
// Expect bugs!
// 
// Please use and enjoy. Please let me know of any bugs/mods/improvements 
// that you have found/implemented and I will fix/incorporate them into this
// file. 
//
// ..........................................................................
// CSEEdit is based on Chris Maunder' CInplaceEdit. In order to avoid frequently
// dynamicly creating CInplaceEdit ctrl, I introduced two new member methods
// 1, BOOL CSEEdit::Create( DWORD dwStyle, CWnd* pParentWnd, UINT nID )
//		"Create" Creates and initializes the CSEEdit ctrl. 
// 2, void CSEEdit::ShowAt( DWORD dwStyle, CRect& rect, CString strInit, UINT nFirstChar )
//		"ShowAt" Will make the control be visible in the "CRect& rect" with initial text
//		"strInit". You can change the ctrl's style by passing a valid style in "dwStyle"
// How to use CSEEdit object as inplaced edit ctrl:
// First call the constructor CSEEdit, then call the Create to create the CSEEdit ctrl
// do not including WS_VISIBLE style in "dwStyle". Call "ShowAt " method to show the ctrl
// at "CRect rect". If you create the CSEEdit object on the heap by using "new" function,
// you must call delete to destruct the object.
//										huangchaoyi@263.net
// ..........................................................................
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TCHAR.h"
#include "SEEdit.h"
#include "spinedit.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSEEdit

IMPLEMENT_DYNCREATE(CSEEdit, CEdit)

CSEEdit::CSEEdit()
{
	m_nLastChar     = 0; 
    m_sInitText		= _T("");
}

BOOL CSEEdit::Create( DWORD dwStyle, CWnd* pParentWnd, UINT nID )
{
    DWORD dwEditStyle = WS_CHILD| ES_AUTOHSCROLL | dwStyle;

	CRect rect;
	rect.left = 0;
	rect.top = 0;
	rect.right = 10;
	rect.bottom = 10;
	
    if (!CEdit::Create(dwEditStyle, rect, pParentWnd, nID)) 
	{
		TRACE( "\ncreate Edit failure\n");
		return FALSE;
	}

	SetFont( pParentWnd->GetFont() );
	SetWindowText(m_sInitText);

	return TRUE;
}

CSEEdit::~CSEEdit()
{

}
 
BEGIN_MESSAGE_MAP(CSEEdit, CEdit)
    //{{AFX_MSG_MAP(CSEEdit)
    ON_WM_KILLFOCUS()
    ON_WM_CHAR()
    ON_WM_KEYDOWN()
    ON_WM_KEYUP()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CSEEdit::ShowAt( DWORD dwStyle, CRect rect, CString strInit, UINT nFirstChar )
{
	m_nLastChar     = 0; 
	m_sInitText		= strInit;

	SetWindowText( m_sInitText );
	ModifyStyle( 0, WS_VISIBLE | dwStyle );
	MoveWindow( rect );
	
	SetFocus();

    switch (nFirstChar)
	{
        case VK_LBUTTON:  
        case VK_RETURN:   SetSel(0,-1);return;
        case VK_BACK:     break;
        case VK_SPACE:
        case VK_END:      SetSel(0,-1); return;
        default:          SetSel(0,-1);
    }

    SendMessage(WM_CHAR, nFirstChar);
}

////////////////////////////////////////////////////////////////////////////
// CSEEdit message handlers

// If an arrow key (or associated) is pressed, then exit if
//  a) The Ctrl key was down, or
void CSEEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
    if ((nChar == VK_PRIOR || nChar == VK_NEXT ||
         nChar == VK_DOWN  || nChar == VK_UP   ||
         nChar == VK_RIGHT || nChar == VK_LEFT) && ( GetKeyState(VK_CONTROL) < 0))
    {
        m_nLastChar = nChar;
        GetParent()->SetFocus();
        return;
    }

    if (nChar == VK_ESCAPE) 
    {
        SetWindowText(m_sInitText);    // restore previous text
        m_nLastChar = nChar;
        GetParent()->SetFocus();
        return;
    }

    CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}

// Need to keep a lookout for Tabs, Esc and Returns. These send a 
// "KeyUp" message, but no "KeyDown". That's why I didn't put their
// code in OnKeyDown. (I will never understand windows...)
void CSEEdit::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
    if (nChar == VK_TAB || nChar == VK_RETURN || nChar == VK_ESCAPE)
    {
        m_nLastChar = nChar;
        GetParent()->SetFocus();    // This will destroy this window
        return;
    }

    CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
}

// As soon as this edit loses focus, kill it.
void CSEEdit::OnKillFocus(CWnd* pNewWnd)
{
    CEdit::OnKillFocus(pNewWnd);
	EndEdit( pNewWnd );	
}

void CSEEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	//only allow digital number
	CString holder = "qwertyuiop[]=\\asdfghjkl;'zxcvbnm,/~!@#$%^&*()_|}{POIUYTREWQASDFGHJKL:\"?><MNBVCXZ";
	if ( holder.Find( nChar ) == (-1) )
		CEdit::OnChar(nChar, nRepCnt, nFlags);
}

////////////////////////////////////////////////////////////////////////////
// CSEEdit overrides

BOOL CSEEdit::PreTranslateMessage(MSG* pMsg) 
{
	// Make sure that the keystrokes continue to the appropriate handlers
	if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_KEYUP)
	{
		::TranslateMessage(pMsg);
		::DispatchMessage(pMsg);
		return TRUE;
	}	

	// Catch the Alt key so we don't choke if focus is going to an owner drawn button
	if (pMsg->message == WM_SYSCHAR)
		return TRUE;

    return CWnd::PreTranslateMessage(pMsg);
}

////////////////////////////////////////////////////////////////////////////
// CSEEdit implementation

void CSEEdit::EndEdit( CWnd* pNewWnd )
{
    CString str;
    GetWindowText(str);
 
    // Send Notification to parent
    SE_NOTIFYMSG msginfo;	

    msginfo.hdr.hwndFrom = GetSafeHwnd();
    msginfo.hdr.idFrom   = GetDlgCtrlID();
    msginfo.hdr.code     = SEN_ENDLABELEDIT;
 
    msginfo.szText  = str;			//在父窗口处将转换为浮点数
    msginfo.lParam  = (LPARAM) m_nLastChar; 
	msginfo.pNewFocusWnd = pNewWnd;

	ModifyStyle( WS_VISIBLE, 0 );
 
    CWnd* pOwner = GetOwner();
    if (pOwner)
	{
        pOwner->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&msginfo );
		pOwner->Invalidate( );
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -