开发中用到Git,我用的是TortoiseGit,经常需要输入密码,很烦人,就随手写了个检测密码窗口并自动输入的程序。
(当然,也可以用生成自动验证密钥,不过我没有服务端的权限,没办法产生)
下面把代码贴下来备忘,其中有注释部分没搞明白,有谁知道为啥不行的告诉我一下啊!本人刚不胜感激
// AutoTortoiseLinkPwdDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AutoTortoiseLinkPwd.h"
#include "AutoTortoiseLinkPwdDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define TIMER_FLAG 10
#define TIMER_INVAL 200
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CAutoTortoiseLinkPwdDlg dialog
CAutoTortoiseLinkPwdDlg::CAutoTortoiseLinkPwdDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAutoTortoiseLinkPwdDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAutoTortoiseLinkPwdDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAutoTortoiseLinkPwdDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, &CAutoTortoiseLinkPwdDlg::OnBnClickedOk)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_START, &CAutoTortoiseLinkPwdDlg::OnBnClickedButtonStart)
ON_BN_CLICKED(IDC_BUTTON_STOP, &CAutoTortoiseLinkPwdDlg::OnBnClickedButtonStop)
END_MESSAGE_MAP()
// CAutoTortoiseLinkPwdDlg message handlers
BOOL CAutoTortoiseLinkPwdDlg::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
SetForegroundWindow();
// TODO: Add extra initialization here
SetDlgItemText(IDC_EDIT_PWD, TEXT("820115"));
return TRUE; // return TRUE unless you set the focus to a control
}
void CAutoTortoiseLinkPwdDlg::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 CAutoTortoiseLinkPwdDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CAutoTortoiseLinkPwdDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void SendKey(HWND hwnd, TCHAR c)
{
#define MAKE_LP(vk,t) MAKELPARAM(t,MapVirtualKey(vk, 0))
//从字符找到虚拟键码
SHORT vCode = VkKeyScan(c);
//获取虚拟键码和Shift状态(高位为Shift状态)
UINT vKey = vCode & 0xFF;
bool IsShift = (vCode & 0x0100) == 0x0100 ? true : false;
PostMessage(hwnd, WM_CHAR, c, MAKE_LP(vKey,1));
/*
//如果Shift按下,则发送Shift键按下消息
if(IsShift)
PostMessage(hwnd, WM_KEYDOWN, VK_SHIFT, MAKE_LP(VK_SHIFT,1));
PostMessage(hwnd, WM_KEYDOWN, vKey, MAKE_LP(vKey,1));
//30、31位置1并发送
PostMessage(hwnd, WM_KEYUP, vKey, MAKE_LP(vKey,1) | (1 << 30) | (1 << 31));
//如果Shift按下,则发送Shift键弹起消息
if(IsShift)
PostMessage(hwnd, WM_KEYUP, VK_SHIFT, MAKE_LP(VK_SHIFT,1) | (1 << 30) | (1 << 31));
*/
#undef MAKE_LP
}
static HWND HwndBtnOk = 0;
static HWND HwndEditPwd = 0;
BOOL CALLBACK EnumChildWindowsProc(HWND hwnd, LPARAM lParam)
{
CAutoTortoiseLinkPwdDlg* dlg = (CAutoTortoiseLinkPwdDlg*)lParam;
//获取控件类型
TCHAR cName[512] = {0};
::GetClassName(hwnd, cName, 511);
if(_wcsicmp(cName, TEXT("Edit")) == 0)
{
CString str;
dlg->GetDlgItemText(IDC_EDIT_PWD, str);
for (int i=0; i<str.GetLength(); i++)
{
SendKey(hwnd, str[i]);
Sleep(1);
}
HwndEditPwd = hwnd;
}
//找到OK按钮,并点击
if(_wcsicmp(cName, TEXT("Button")) == 0)
{
TCHAR txtBuf[512] = {0};
::GetWindowTextW(hwnd, txtBuf, 511);
if(_wcsicmp(txtBuf, L"OK") == 0)
{
HwndBtnOk = hwnd;
}
}
return TRUE;
}
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
const TCHAR* WND_NAME = /*L"AutoTortoiseLinkPwd"; */ L"TortoisePlink";
TCHAR txtBuf[512] = {0};
::GetWindowTextW(hwnd, txtBuf, 511);
if(_wcsicmp(txtBuf, WND_NAME) == 0)
{
HwndBtnOk = 0;
HwndEditPwd = 0;
::EnumChildWindows(hwnd, &EnumChildWindowsProc, lParam);
if(HwndBtnOk != 0)
{
PostMessage(HwndBtnOk, WM_LBUTTONDOWN, MK_LBUTTON, 0);
Sleep(10);
PostMessage(HwndBtnOk, WM_LBUTTONUP, MK_LBUTTON, 0);
}
return FALSE;
}
return TRUE;
}
void CAutoTortoiseLinkPwdDlg::OnBnClickedOk()
{
ShowWindow(SW_HIDE);
}
void CAutoTortoiseLinkPwdDlg::OnTimer(UINT_PTR nIDEvent)
{
if(nIDEvent == TIMER_FLAG)
EnumWindows(EnumWindowsProc, (LPARAM)this);
CDialog::OnTimer(nIDEvent);
}
void CAutoTortoiseLinkPwdDlg::OnBnClickedButtonStart()
{
SetTimer(TIMER_FLAG, TIMER_INVAL, NULL);
((CButton*)GetDlgItem(IDC_BUTTON_START))->EnableWindow(FALSE);
((CButton*)GetDlgItem(IDC_BUTTON_STOP))->EnableWindow(TRUE);
}
void CAutoTortoiseLinkPwdDlg::OnBnClickedButtonStop()
{
KillTimer(TIMER_FLAG);
((CButton*)GetDlgItem(IDC_BUTTON_START))->EnableWindow(TRUE);
((CButton*)GetDlgItem(IDC_BUTTON_STOP))->EnableWindow(FALSE);
}