?? btnetscdlg.cpp
字號:
// btnetscDlg.cpp : implementation file
//
#include "stdafx.h"
#include "btnetsc.h"
#include "btnetscDlg.h"
#include ".\btnetscdlg.h"
#include <atlimage.h>
#include "SendDlg.h"
#include "base64.h"
#include <fstream>
#include "ConfigDlg.h"
#include "TimeoutSocket.h"
#include "BitMapWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CString get_temp_file_path()
{
char temp_path[1000];
::GetTempPath(1000,temp_path);
CString temp_file = temp_path;
temp_file += "bugtracker_screen_capture.jpeg";
return temp_file;
}
// 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()
// CbtnetscDlg dialog
CbtnetscDlg::CbtnetscDlg(CWnd* pParent /*=NULL*/)
: CDialog(CbtnetscDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hBitmap = 0;
m_strPassword = "";
}
CbtnetscDlg::~CbtnetscDlg()
{
if (m_hBitmap)
{
::DeleteObject(m_hBitmap);
}
}
void CbtnetscDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_MYPICT, m_ctlPict);
DDX_Control(pDX, IDC_FILE_SAVED, m_ctlFileSaved);
DDX_Control(pDX, IDC_SEND, m_btnSend);
}
BEGIN_MESSAGE_MAP(CbtnetscDlg, CDialog)
//{{AFX_MSG_MAP(CbtnetscDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_SEND, OnBnClickedSend)
ON_BN_CLICKED(IDC_CONFIGURE, OnBnClickedConfigure)
ON_BN_CLICKED(IDC_FOREGROUND, OnBnClickedForeground)
ON_BN_CLICKED(IDC_ALL, OnBnClickedAll)
END_MESSAGE_MAP()
// CbtnetscDlg message handlers
BOOL CbtnetscDlg::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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CbtnetscDlg::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 CbtnetscDlg::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();
//CPaintDC dc(this);
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CbtnetscDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CbtnetscDlg::CaptureWindow(bool bForeground)
{
if (m_hBitmap)
{
::DeleteObject(m_hBitmap);
m_hBitmap = 0;
}
// Save original position
WINDOWPLACEMENT lpwndpl;
GetWindowPlacement(&lpwndpl);
lpwndpl.showCmd = SW_MINIMIZE;
SetWindowPlacement(&lpwndpl);
Sleep(1000);
// Capture entire screen
int nScreenWidth;
int nScreenHeight;
HWND hSourceWnd;
if (bForeground)
{
hSourceWnd = ::GetForegroundWindow();
RECT rectSource;
::GetWindowRect(hSourceWnd,&rectSource);
nScreenWidth = rectSource.right-rectSource.left;
nScreenHeight = rectSource.bottom-rectSource.top;
}
else
{
hSourceWnd = ::GetDesktopWindow();
nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
}
HDC hSourceDC = ::GetWindowDC(hSourceWnd);
HDC hMemDC = ::CreateCompatibleDC(hSourceDC);
m_hBitmap = ::CreateCompatibleBitmap(hSourceDC, nScreenWidth, nScreenHeight);
::SelectObject(hMemDC,m_hBitmap);
::BitBlt(hMemDC,0,0,nScreenWidth,nScreenHeight,hSourceDC,0,0,SRCCOPY);
//SaveBitmap(m_hBitmap);
// Display bitmap
m_ctlPict.SetBitmap(m_hBitmap);
::ReleaseDC(hSourceWnd,hSourceDC);
::DeleteDC(hMemDC);
// Restore this window
lpwndpl.showCmd = SW_RESTORE;
SetWindowPlacement(&lpwndpl);
m_btnSend.EnableWindow();
//OnOK();
}
///////////////////////////////////////////////////////////////////////
void CbtnetscDlg::SaveBitmap(HBITMAP hbitmap)
{
// Save as a JPEG
ATL::CImage img;
img.Attach(hbitmap);
img.Save(get_temp_file_path(), Gdiplus::ImageFormatJPEG);
img.Detach();
m_ctlFileSaved.SetWindowText(get_temp_file_path() + " saved.");
}
///////////////////////////////////////////////////////////////////////
bool CbtnetscDlg::CheckIfSettingsComplete()
{
CbtnetscApp* pApp = (CbtnetscApp*) AfxGetApp();
pApp->GetSavedSettings();
if (this->m_strPassword = "")
{
this->m_strPassword = pApp->m_strPassword;
}
if (pApp->m_strUrl == ""
|| pApp->m_strPort == ""
|| pApp->m_strEmail == ""
|| pApp->m_strUsername == ""
|| this->m_strPassword == "" )
{
return false;
}
else
{
return true;
}
}
///////////////////////////////////////////////////////////////////////
void CbtnetscDlg::OnBnClickedSend()
{
if (!CheckIfSettingsComplete())
{
AfxMessageBox("Please enter your settings, username, and password.");
OnBnClickedConfigure();
if (!CheckIfSettingsComplete())
{
return;
}
}
CSendDlg send_dlg;
if (send_dlg.DoModal() != IDOK)
{
return;
}
SaveBitmap(m_ctlPict.GetBitmap());
CbtnetscApp* pApp = (CbtnetscApp*) AfxGetApp();
// GET THE JPEG FILE INTO MEMORY
std::ifstream jpeg_file;
jpeg_file.open(get_temp_file_path(), std::ios::binary);
if (!jpeg_file.is_open())
{
AfxMessageBox (get_temp_file_path() + " not found.");
return;
}
jpeg_file.seekg (0, std::ios::end);
int jpeg_file_size = jpeg_file.tellg();
unsigned char* jpeg_file_buffer = new unsigned char[jpeg_file_size];
jpeg_file.seekg (0, std::ios::beg);
jpeg_file.read((char*)jpeg_file_buffer,jpeg_file_size);
// sorry about the mixture of STL strings and MFC strings...
CString email = "";
// Format the email embedded into the request
email += "From: $.$FROM$.$\r\n";
email += "To: BugTracker.NET\r\n";
email += "Subject: $.$SUBJECT$.$\r\n";
email += "MIME-Version: 1.0\r\n";
email += "Content-Type: multipart/mixed;\r\n";
email += "\tboundary=\"----=...my_boundary..........\"\r\n\r\n";
email += "This is a multi-part message in MIME format.\r\n\r\n";
email += "------=...my_boundary..........\r\n";
// Body
email += "Content-Type: text/plain;\r\n\tcharset=\"iso-8859-1\"\r\n";
email += "Content-Transfer-Encoding: 7bit\r\n\r\n";
email += "$.$BODY$.$\r\n";
email += "------=...my_boundary..........\r\n";
// Attachment
email += "Content-Type: image/jpeg;\r\n\tname=\"screen_capture.jpeg\"\r\n";
email += "Content-Transfer-Encoding: base64\r\n";
email += "Content-Disposition: attachment;\r\n\tfilename=\"screen_capture.jpeg\"\r\n\r\n";
email += "$.$JPEG$.$\r\n\r\n";
email += "------=...my_boundary..........--\r\n";
//AfxMessageBox(email);
// Replace the variables in the email
std::string base64_jpeg = base64_encode(
jpeg_file_buffer,jpeg_file_size);
email.Replace("$.$FROM$.$",pApp->m_strEmail);
email.Replace("$.$SUBJECT$.$",send_dlg.m_strDesc);
email.Replace("$.$BODY$.$", send_dlg.m_strComment);
email.Replace("$.$JPEG$.$",base64_jpeg.c_str());
CString strUrl = pApp->m_strUrl;
strUrl.Replace("HTTP://","");
strUrl.Replace("http://","");
int pos = strUrl.Find("/");
CString strHostPart = strUrl.Left(pos);
int nUrlLen = strUrl.GetLength();
CString strUrlPart = strUrl.Right(nUrlLen-pos);
// Create the HTTP POST request
std::string msg = "POST ";
msg += strUrlPart;
msg += "\r\n";
msg += "Content-Type: application/x-www-form-urlencoded\r\n";
// Format the content of the HTTP request
CString content = "";
content += "username=";
content += url_encode(std::string(pApp->m_strUsername)).c_str();
content += "&password=";
content += url_encode(std::string(this->m_strPassword)).c_str();
content += "&from=" ;
content += url_encode(std::string(pApp->m_strEmail)).c_str();
content += "&short_desc=" ;
content += url_encode(std::string(send_dlg.m_strDesc)).c_str();
content += "&projectid=" ;
content += url_encode(std::string(pApp->m_strProject)).c_str();
content += "&message=";
content += url_encode(std::string(email)).c_str();
content += "&foo=bar";
// calculate the Content-Length;
msg += "Content-Length: ";
char int_buf[20];
msg += itoa(content.GetLength(), int_buf, 10);
msg += "\r\n\r\n";
msg += content;
msg += "\r\n\r\n";
delete jpeg_file_buffer;
jpeg_file.close();
/*
std::ofstream request_file;
request_file.open("c:\\btnetsc_request_file.txt",std::ios::binary);
request_file << msg.c_str();
request_file.close();
*/
// Send the HTTP request
AfxSocketInit();
CTimeOutSocket sock;
BOOL created = sock.Create();
sock.SetTimeOut(10000);
BOOL connected = sock.Connect(
strHostPart,
atoi(pApp->m_strPort));
if (connected)
{
size_t http_request_len = msg.size();
int sent = sock.Send(msg.c_str(),(int)http_request_len);
//sock.Receive(
char buf[2000];
// Examine the response
int received = sock.Receive(buf,1024);
buf[received] = '\0';
AfxMessageBox(buf);
}
else
{
AfxMessageBox("Unable to connect to " + strHostPart + " port " + pApp->m_strPort);
}
sock.Close();
}
void CbtnetscDlg::OnBnClickedConfigure()
{
CConfigDlg dlg;
dlg.DoModal();
m_strPassword = dlg.m_strPassword;
}
void CbtnetscDlg::OnBnClickedForeground()
{
CaptureWindow(true);
}
void CbtnetscDlg::OnBnClickedAll()
{
CaptureWindow(false);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -