Example #1
0
BOOL TImageWin::CutImage(BOOL use_clip, BOOL with_save)
{
	RECT rc = { areaPts[0].x, areaPts[0].y, areaPts[1].x, areaPts[1].y };
	RegularRect(&rc);

	int	cx = rc.right  - rc.left + 1; // include right line pixel
	int	cy = rc.bottom - rc.top  + 1; // include bottom line pixel

	HWND	hDesktop = ::GetDesktopWindow();
	HDC		hDesktopDc = ::GetDC(hDesktop);

	HDC		hAreaDc  = ::CreateCompatibleDC(hDesktopDc);
	HBITMAP	hAreaBmp = ::CreateCompatibleBitmap(hDesktopDc, cx, cy);

	HBITMAP	hOldBmp = (HBITMAP)::SelectObject(hSelfDc, hSelfBmp);
	HBITMAP	hAreaOldBmp = (HBITMAP)::SelectObject(hAreaDc, hAreaBmp);

	DrawMarker(hSelfDc);
	::BitBlt(hAreaDc, 0, 0, cx, cy, hSelfDc, rc.left, rc.top, SRCCOPY);

	::SelectObject(hAreaDc, hAreaOldBmp);
	::SelectObject(hSelfDc, hOldBmp);

	if (use_clip && ::OpenClipboard(hWnd)) {
		::EmptyClipboard();
		::SetClipboardData(CF_BITMAP, hAreaBmp);
		::CloseClipboard();
	}

	parentWnd->InsertBitmapByHandle(hAreaBmp);

	if (with_save) {
		char	fname[MAX_PATH_U8] = "";
		OpenFileDlg	dlg(this->parent, OpenFileDlg::SAVE, NULL, OFN_OVERWRITEPROMPT);

		if (dlg.Exec(fname, sizeof(fname), NULL, "PNG file(*.png)\0*.png\0\0",
						cfg->lastSaveDir, "png")) {
			DWORD	size = 0;
			VBuf	*buf = BmpHandleToPngByte(hAreaBmp);
			if (buf) {
				HANDLE	hFile = CreateFileU8(fname, GENERIC_WRITE,
											 FILE_SHARE_READ|FILE_SHARE_WRITE,
											 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
				if (hFile != INVALID_HANDLE_VALUE) {
					WriteFile(hFile, buf->Buf(), buf->Size(), &size, 0);
					CloseHandle(hFile);
				}
				delete buf;
			}
		}
	}

	::DeleteObject(hAreaBmp);
	::DeleteDC(hAreaDc);
	::ReleaseDC(hDesktop, hDesktopDc);

	return	TRUE;
}
Example #2
0
BOOL SaveImageFile(Cfg *cfg, const char *fname, VBuf *buf)
{
    char	path[MAX_PATH_U8];
    DWORD	size;
    HANDLE	hFile;

    if (!MakeImageFolder(cfg, path)) return FALSE;
    CreateDirectoryU8(path, 0);
    strcat(path, "\\");
    strcat(path, fname);

    if ((hFile = CreateFileU8(path, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
                              CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)) == INVALID_HANDLE_VALUE) return FALSE;

    WriteFile(hFile, buf->Buf(), buf->Size(), &size, 0);
    CloseHandle(hFile);

    return	TRUE;
}
Example #3
0
BOOL TRecvDlg::OpenRecvFile(void)
{
	char	path[MAX_BUF];

	if (MakePath(path, fileObj->isDir ? fileObj->path : fileObj->saveDir, fileObj->curFileInfo.Fname()) >= MAX_PATH_U8)
		return	MessageBoxU8(path, GetLoadStrU8(IDS_PATHTOOLONG)), FALSE;
	if (IsSafePath(path, fileObj->curFileInfo.Fname()) == FALSE)
		return	MessageBoxU8(path, GetLoadStrU8(IDS_NOTSAFEPATH)), FALSE;

	if ((fileObj->hFile = CreateFileU8(path, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)) == INVALID_HANDLE_VALUE)
		return	fileObj->isDir ? FALSE : (MessageBoxU8(GetLoadStrU8(IDS_CREATEFAIL), path), FALSE);

	if (fileObj->curFileInfo.Attr() & IPMSG_FILE_RONLYOPT)
		SetFileAttributesU8(path, FILE_ATTRIBUTE_READONLY);

//::SetFilePointer(fileObj->hFile, OFFSET, 0, FILE_BEGIN);
//::SetEndOfFile(fileObj->hFile);

	return	TRUE;
}
Example #4
0
BOOL GetFileInfomationU8(const char *path, WIN32_FIND_DATA_U8 *fdata)
{
	HANDLE	fh;

	if ((fh = FindFirstFileU8(path, fdata)) != INVALID_HANDLE_VALUE)
	{
		::FindClose(fh);
		return	TRUE;
	}

	if ((fh = CreateFileU8(path, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING,
		FILE_FLAG_BACKUP_SEMANTICS, 0)) != INVALID_HANDLE_VALUE)
	{
		BY_HANDLE_FILE_INFORMATION	info;
		BOOL	info_ret = ::GetFileInformationByHandle(fh, &info);
		::CloseHandle(fh);
		if (info_ret) {
			memcpy(fdata, &info, (char *)&info.dwVolumeSerialNumber - (char *)&info);
			return	TRUE;
		}
	}

	return	(fdata->dwFileAttributes = GetFileAttributesU8(path)) == 0xffffffff ? FALSE : TRUE;
}
Example #5
0
BOOL TRecvDlg::RecvDirFile(void)
{
#define BIG_ALLOC	50
#define PEEK_SIZE	8

	if (fileObj->status == FS_DIRFILESTART || fileObj->status == FS_TRANSINFO)
	{
		int		size;
		if (fileObj->infoLen == 0)
		{
			if ((size = ::recv(fileObj->conInfo->sd, fileObj->info + (int)fileObj->offset, PEEK_SIZE - (int)fileObj->offset, 0)) <= 0)
				return	FALSE;
			if ((fileObj->offset += size) < PEEK_SIZE)
				return	TRUE;
			fileObj->info[fileObj->offset] = 0;
			if ((fileObj->infoLen = strtoul(fileObj->info, 0, 16)) >= sizeof(fileObj->info) -1 || fileObj->infoLen <= 0)
				return	FALSE;	// too big or small
		}
		if (fileObj->offset < fileObj->infoLen)
		{
			if ((size = ::recv(fileObj->conInfo->sd, fileObj->info + (int)fileObj->offset, fileObj->infoLen - (int)fileObj->offset, 0)) <= 0)
				return	FALSE;
			fileObj->offset += size;
		}
		if (fileObj->offset == fileObj->infoLen)
		{
			fileObj->info[fileObj->infoLen] = 0;
			if (DecodeDirEntry(fileObj->info, &fileObj->curFileInfo, fileObj->u8fname) == FALSE)
				return	FALSE;	// Illegal entry
			fileObj->offset = fileObj->infoLen = 0;	// 初期化

			if (GET_MODE(fileObj->curFileInfo.Attr()) == IPMSG_FILE_DIR)
			{
				char	buf[MAX_BUF];
				const char *fname = fileObj->dirCnt == 0 ? fileObj->fileInfo->Fname() : fileObj->curFileInfo.Fname();

				if (MakePath(buf, fileObj->path, fname) >= MAX_PATH_U8)
					return	MessageBoxU8(buf, GetLoadStrU8(IDS_PATHTOOLONG)), FALSE;
				if (IsSafePath(buf, fname) == FALSE)
					return	FALSE;

				if (CreateDirectoryU8(buf, NULL) == FALSE)
					return	FALSE;
				strncpyz(fileObj->path, buf, MAX_PATH_U8);
				fileObj->dirCnt++;
			}
			else if (GET_MODE(fileObj->curFileInfo.Attr()) == IPMSG_FILE_RETPARENT)
			{
				if (fileObj->curFileInfo.Mtime())	// directory の time stamp をあわせる(NT系のみ)
				{
					FILETIME	ft;
					HANDLE		hFile;
					UnixTime2FileTime(fileObj->curFileInfo.Mtime(), &ft);
					if ((hFile = CreateFileU8(fileObj->path, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)) != INVALID_HANDLE_VALUE)
					{
						::SetFileTime(hFile, NULL, NULL, &ft);
						::CloseHandle(hFile);
					}
				}
				if (fileObj->curFileInfo.Attr() & IPMSG_FILE_RONLYOPT)
				SetFileAttributesU8(fileObj->path, FILE_ATTRIBUTE_READONLY);
				if (--fileObj->dirCnt <= 0)
				{
					fileObj->status = FS_COMPLETE;
					return	TRUE;
				}
				if (PathToDir(fileObj->path, fileObj->path) == FALSE)
					return	FALSE;
			}
			else {
				if (fileObj->dirCnt == 0)
					return	FALSE;
				
				if (fileObj->curFileInfo.Size() == 0)	// 0byte file
				{
					if (OpenRecvFile())		// 0byteの場合は作成失敗を無視
						CloseRecvFile(TRUE);
				}
				fileObj->status = fileObj->curFileInfo.Size() ? FS_TRANSFILE : FS_TRANSINFO;
			}
			return	TRUE;
		}
	}

	if (fileObj->status == FS_TRANSFILE)
	{
		if (RecvFile() != TRUE)
		{
			CloseRecvFile();
			return	FALSE;
		}
		if (fileObj->status == FS_ENDFILE)
		{
			CloseRecvFile(TRUE);
			fileObj->status = FS_TRANSINFO;
		}
	}

	return	TRUE;
}