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

📄 inputsndlg.cpp

📁 按热键自动输入注册码
💻 CPP
字号:
 // InPutSnDlg.cpp : implementation file
//

#include "stdafx.h"
#include "InPutSn.h"
#include "InPutSnDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInPutSnDlg dialog

CInPutSnDlg::CInPutSnDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CInPutSnDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CInPutSnDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CInPutSnDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInPutSnDlg)
	DDX_Control(pDX, IDC_HOTKEY1, m_hotkey1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CInPutSnDlg, CDialog)
	//{{AFX_MSG_MAP(CInPutSnDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_HIDE, OnHide)
	ON_COMMAND(ID_SHOWHIDE, OnShowhide)
	ON_COMMAND(ID_EXITAPP, OnExitapp)
	ON_BN_CLICKED(IDC_SETHOTKEY, OnSethotkey)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_HOTKEY , OnHotKey)
	ON_MESSAGE(WM_ICON_NOTIFY  , OnTrayNotify)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInPutSnDlg message handlers

BOOL CInPutSnDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	m_tray.Create(this , WM_ICON_NOTIFY , "Quick Input Sn" , 
		AfxGetApp()->LoadIcon(IDR_MAINFRAME) , IDR_POPMENU1);

	UpdateCfg(g_cfg , FALSE);
	UpdateCfg(g_cfg , TRUE);
	
	if(!RegisterHotKey(GetSafeHwnd(),WM_SHOWHOTKEY,
		HIWORD(atoi(g_cfg.HotKey )),LOWORD(atoi(g_cfg.HotKey ))))
	{
		AfxMessageBox("注册热键错误");
	}

	m_hotkey1.SetHotKey(LOWORD(atoi(g_cfg.HotKey ) ) , HIWORD(atoi(g_cfg.HotKey )));
	
	// TODO: Add extra initialization here

	SetTimer(100, 1000 , NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CInPutSnDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CInPutSnDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CInPutSnDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CInPutSnDlg::OnOK() 
{
	return;
}

LRESULT CInPutSnDlg::OnHotKey(WPARAM wParam,LPARAM lParam)
{
	if(wParam ==WM_SHOWHOTKEY)
	{
		OnInputsn();
		//SetTimer(1 , 10 , NULL);
	}
	return 0;
}

void CInPutSnDlg::SetToClip(char *p)
{
	HGLOBAL clipbuffer;
	char	*buffer;

	OpenClipboard();
	clipbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(p)+1);
	buffer = (char*)GlobalLock(clipbuffer);
	strcpy(buffer, LPCSTR(p));
	GlobalUnlock(clipbuffer);
	EmptyClipboard();
	SetClipboardData(CF_TEXT , buffer);
	CloseClipboard();

}
void CInPutSnDlg::GetFromClip(char *p)
{
	char	*buffer;

	OpenClipboard();
	HANDLE h=::GetClipboardData(CF_TEXT);

	if(h)
	{
		buffer = (char *)GlobalLock(h);
		memcpy(p , (char *)h , 1023);
		GlobalUnlock(h);
	}
	CloseClipboard();
}

void CInPutSnDlg::OnInputsn() 
{
	char	szTemp[1024];
	char	szSubSn[20];
	char	*result,*temp;
	HWND	hwndFocus,hwndTemp;



//	ReleaseFocus() ;
	GetFromClip(szTemp);

	DWORD	dwTID;
	dwTID = ::GetWindowThreadProcessId(::GetForegroundWindow(), NULL);
	if(dwTID == ::GetCurrentThreadId())
		return;
	AttachThreadInput(
		::GetCurrentThreadId() , 
		dwTID , 
		TRUE);
	result = temp = szTemp;
	hwndFocus = hwndTemp = ::GetFocus();
	while((result = strpbrk(result , "- ")) != NULL)
	{
		memset(szSubSn , 0 , sizeof(szSubSn));
		strncpy(szSubSn , temp , result - temp);
		result ++;
		temp = result;


		SetToClip(szSubSn);
		Sleep(40);
		::SendMessage(hwndFocus, WM_PASTE , 0 ,0 );
		hwndFocus = ::GetFocus();
		if(hwndFocus == hwndTemp)
		{
			Sleep(40);
			keybd_event(VK_TAB , 0 , 0 , 0);
			hwndTemp = hwndFocus;
		}

	}
	memset(szSubSn , 0 , sizeof(szSubSn));
	strncpy(szSubSn , temp , (szTemp + strlen(szTemp) - temp));

	SetToClip(szSubSn);
	::SendMessage(::GetFocus(), WM_PASTE , 0 ,0 );
	Sleep(40);
	keybd_event(VK_TAB , 0 , 0 , 0);
	AttachThreadInput(
		::GetCurrentThreadId() , 
		dwTID , 
		FALSE);

	SetToClip(szTemp);

}

void CInPutSnDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	UnregisterHotKey(GetSafeHwnd(),WM_SHOWHOTKEY); 	
	
}


void CInPutSnDlg::OnTimer(UINT nIDEvent) 
{
//	DWORD	dwTID;
//	dwTID = ::GetWindowThreadProcessId(::GetForegroundWindow(), NULL);
//	if(dwTID == ::GetCurrentThreadId())
//		return;
//	AttachThreadInput(
//		::GetCurrentThreadId() , 
//		dwTID , 
//		TRUE);
//	::SendMessage(::GetFocus() , WM_CHAR , 'a' , 0);
//	AttachThreadInput(
//		::GetCurrentThreadId() , 
//		dwTID, 
//		FALSE);
	if(100 == nIDEvent)
	{
		char	szClip[1024];
		memset(szClip , 0 , sizeof(szClip));
		GetFromClip(szClip);
		SetDlgItemText(IDC_EDIT1 , szClip);
	}
	CDialog::OnTimer(nIDEvent);
}

BOOL CInPutSnDlg::PreTranslateMessage(MSG* pMsg) 
{
//	HWND wnd = ::GetForegroundWindow();
//	if(IsWindow(wnd))
//	{
//		if(wnd != this->m_hWnd)
//		{
//			if(gFocus != wnd)
//			{
//				if(IsWindow(gFocus))
//				{
//					AttachThreadInput(
//						GetWindowThreadProcessId(m_hWnd,NULL),
//						GetWindowThreadProcessId(gFocus,NULL),
//						FALSE);
//				}
//
//				gFocus = wnd;
//				AttachThreadInput(
//					GetWindowThreadProcessId(m_hWnd,NULL),
//					GetWindowThreadProcessId(gFocus,NULL),
//					TRUE);
//			}
//		}
//	}
	
	return CDialog::PreTranslateMessage(pMsg);
}

void CInPutSnDlg::ReleaseFocus() 
{
	if(IsWindow(gFocus))
	{
		HWND wnd = ::GetForegroundWindow();
		if(IsWindow(wnd))
		{
			if(wnd == gFocus)
			{
				return;
			}
		}
		::SetForegroundWindow(gFocus);
		::SetFocus(gFocus);
	}
}


void CInPutSnDlg::OnHide() 
{
	ShowWindow(SW_HIDE);
	
}
LRESULT CInPutSnDlg::OnTrayNotify(WPARAM wParam, LPARAM lParam)
{
	return m_tray.OnTrayNotification(wParam , lParam);
}

void CInPutSnDlg::OnShowhide() 
{
	if(IsWindowVisible())
	{
		ShowWindow(SW_HIDE);
	}
	else
	{
		ShowWindow(SW_SHOW);
	}
	
}

void CInPutSnDlg::OnExitapp() 
{
	EndDialog(IDCANCEL);	
}

void CInPutSnDlg::OnSethotkey() 
{
	WORD wVK , wMf;
	m_hotkey1.GetHotKey( wVK , wMf);
	sprintf(g_cfg.HotKey , "%u",MAKELONG(wVK , wMf));
	UpdateCfg(g_cfg,TRUE);
}

⌨️ 快捷键说明

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