自動輸入Git密碼的小程序(僅做備忘用)

開發中用到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);
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章