📄 maindlg.cpp
字号:
// MainDlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "aboutdlg.h"
#include "MainDlg.h"
#include <gdiplus.h>
using namespace Gdiplus;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
const size_t FILE_BUFFER_SIZE = 16 * 1024;
BYTE FileBuffer[FILE_BUFFER_SIZE];
BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
{
return CWindow::IsDialogMessage(pMsg);
}
BOOL CMainDlg::OnIdle()
{
return FALSE;
}
LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// center the dialog on the screen
CenterWindow();
// set icons
HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
SetIcon(hIcon, TRUE);
HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
SetIcon(hIconSmall, FALSE);
// register object for message filtering and idle updates
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);
UIAddChildWindowContainer(m_hWnd);
//UI Related
m_bThreadRunning = false;
m_IpAddressCtrl.Attach(GetDlgItem(IDC_IPADDRESS1));
memset(m_szFilePath,0,sizeof(m_szFilePath));
memset(&m_msgHead,0,sizeof(m_msgHead));
DoDataExchange(TRUE);
//Initialize the WinSock Stuff...
WORD VersionRequested = MAKEWORD(2,2);
WSADATA wsaData;
WSAStartup(VersionRequested, &wsaData); // Start Winsock Service
if ( wsaData.wVersion != VersionRequested )
{
MessageBox(TEXT("Wrong version or WinSock not loaded\n"),MB_OK);
fflush(0);
}
//int temp;
//int intlen=sizeof(int);
//int hr;
//if( getsockopt(m_Socket,SOL_SOCKET,SO_SNDBUF,(char*)&temp,&intlen)!=0)
//{
// hr=WSAGetLastError();
// return hr;
//}
// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
return TRUE;
}
LRESULT CMainDlg::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CAboutDlg dlg;
dlg.DoModal();
return 0;
}
LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
DoDataExchange(TRUE);
m_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ( m_Socket == INVALID_SOCKET )
{
MessageBox(TEXT("Error: failed to create socket\n"),MB_OK);
fflush(0);
return false;
}
m_ClientSockAddr.sin_family = AF_INET;
m_ClientSockAddr.sin_addr.s_addr = htonl( INADDR_ANY );
m_ClientSockAddr.sin_port = htons(30000);
if ( bind( m_Socket, (struct sockaddr *)&m_ClientSockAddr, sizeof(m_ClientSockAddr)) != 0 )
{
MessageBox(TEXT("Error: failed to bind\n"),MB_OK);
closesocket( m_Socket );
return false;
}
HANDLE hFile;
hFile = CreateFile(m_szFilePath, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(TEXT("Could not open file !"),TEXT("Error"), MB_ICONEXCLAMATION);
return 0;
}
DWORD dwFileSize;
dwFileSize = GetFileSize (hFile, NULL) ;
if (dwFileSize == 0xFFFFFFFF)
{
// Obtain the error code.
return GetLastError() ;
} // End of error handler
m_msgHead.nImageFileSize=dwFileSize;
// Connect to the server and send it.
DWORD dwIPAddress;
m_IpAddressCtrl.GetAddress(&dwIPAddress);
m_ServerSockAddr.sin_addr.s_addr=htonl(dwIPAddress);
m_ServerSockAddr.sin_family=AF_INET;
m_ServerSockAddr.sin_port=htons(6970);
if ( connect( m_Socket, (SOCKADDR*) &m_ServerSockAddr, sizeof(m_ServerSockAddr) ) == SOCKET_ERROR)
{
MessageBox( TEXT("Failed to connect.\n"),TEXT("Error"),MB_ICONEXCLAMATION);
closesocket(m_Socket);
return -1;
}
DWORD nBytestoSend=sizeof(MESSAGEHEAD);
int nBytesSent;
nBytesSent = send( m_Socket, (const char *)&m_msgHead, nBytestoSend, 0 );
if(nBytesSent==SOCKET_ERROR)
{
MessageBox(TEXT("Could not Send Data !"),TEXT("Error"), MB_ICONEXCLAMATION);
return WSAGetLastError();
}
BOOL bResult;
while(1)
{
bResult=ReadFile(hFile,FileBuffer,FILE_BUFFER_SIZE,&nBytestoSend,NULL);
if(bResult && (nBytestoSend == 0))
break;
else
send(m_Socket,(const char *)FileBuffer,nBytestoSend,0);
}
CloseHandle(hFile);
shutdown(m_Socket,SD_SEND);
closesocket(m_Socket);
}
LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CloseDialog(wID);
return 0;
}
void CMainDlg::CloseDialog(int nVal)
{
GdiplusShutdown(gdiplusToken);
WSACleanup();
DestroyWindow();
::PostQuitMessage(nVal);
}
LRESULT CMainDlg::OnBrowse(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
DoDataExchange(TRUE);
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,TEXT(".jpg"),NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("Image Files (*.jpg)\0*.jpg\0")_T("All Files (*.*)\0*.*\0"),m_hWnd);
//TCHAR szFilePath[MAX_PATH*sizeof(TCHAR)];
if(dlg.DoModal()==IDOK)
{
strcpy(m_szFilePath,dlg.m_szFileName);
strcpy(m_msgHead.chImageFileName,dlg.m_szFileTitle);
//m_strFilePath=dlg.m_szFileName;
DoDataExchange(false);
WCHAR wszFilePath[MAX_PATH*sizeof(WCHAR)];
MultiByteToWideChar(
CP_ACP, // code page
MB_PRECOMPOSED, // character-type options
m_szFilePath, // string to map
strlen(m_szFilePath)+1, // number of bytes in string
wszFilePath, // wide-character buffer
sizeof(wszFilePath) // size of buffer
);
Image thePic(wszFilePath);
HWND hWnd=GetDlgItem(IDC_STATIC_PIC);
RECT rect;
::GetWindowRect(hWnd,&rect);
Graphics graphic(hWnd);
graphic.DrawImage(&thePic,0,0,rect.right-rect.left,rect.bottom-rect.top);
}
else
{
return -1;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -