Skip to content

Instantly share code, notes, and snippets.

@frostiq
Last active December 30, 2015 11:59
Show Gist options
  • Save frostiq/7826204 to your computer and use it in GitHub Desktop.
Save frostiq/7826204 to your computer and use it in GitHub Desktop.
#include <Windows.h>
#include <string>
#include <sstream>
#include <fstream>
#include "../../KWnd.h"
int CopyDirTo(const std::wstring& source_folder, const std::wstring& target_folder);
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int KWnd::class_count=0;
UINT time;
#define TIMER 1
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if(strcmp(lpCmdLine, "") != 0)
{
std::stringstream s;
s << lpCmdLine;
s >> time;
}
else time = 5;
time *= 1000;
KWnd window(L"Win1", hInstance, 0, WndProc, NULL, 0, 0, 0, 0,
CS_HREDRAW | CS_VREDRAW, WS_POPUP, NULL);
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
SetTimer(hWnd, TIMER, time, NULL);
break;
case WM_TIMER:{
std::ofstream log("D:/out/log.txt", std::ofstream::out | std::ostream::trunc);
log<<CopyDirTo(L"D:\\2\\", L"D:\\out\\");
log.close();
SendMessage(hWnd, WM_DESTROY, 0, 0);
break;}
case WM_DESTROY:
KillTimer(hWnd, TIMER);
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
int CopyDirTo(const std::wstring& source_folder, const std::wstring& target_folder)
{
std::wstring new_sf = source_folder + L"\\*";
WCHAR sf[MAX_PATH+1];
WCHAR tf[MAX_PATH+1];
wcscpy_s(sf, MAX_PATH, new_sf.c_str());
wcscpy_s(tf, MAX_PATH, target_folder.c_str());
sf[lstrlenW(sf)+1] = 0;
tf[lstrlenW(tf)+1] = 0;
SHFILEOPSTRUCTW s = {0};
s.wFunc = FO_COPY;
s.pTo = tf;
s.pFrom = sf;
s.fFlags = FOF_NO_UI;
return SHFileOperationW(&s);
}
#ifndef _KWND.H_
#define _KWND.H_
#include <windows.h>
#include <sstream>
#include <vector>
class KWnd
{
public:
KWnd(LPCTSTR windowName, HINSTANCE hInst, int cmdShow,
LRESULT (WINAPI *pWndProc)(HWND,UINT,WPARAM,LPARAM),
LPCTSTR menuName = NULL,
int x = CW_USEDEFAULT, int y = 0,
int width = CW_USEDEFAULT, int heigth = 0,
UINT classStyle = CS_HREDRAW | CS_VREDRAW,
DWORD windowStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE,
HWND hParent = NULL);
HWND GetHWnd(int i=0) {return hWnd[i];}
static int class_count;
void CreateNewWin (LPCTSTR windowName, HINSTANCE hInst, int cmdShow, DWORD windowStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE,
int x = CW_USEDEFAULT, int y = 0, int width = 300, int heigth = 300, HWND hParent = NULL);
~KWnd();
protected:
std::vector<HWND> hWnd;
WNDCLASSEX wc;
void ConstPtrAssign(LPCTSTR& dest, LPCTSTR source);
};
KWnd::KWnd(LPCTSTR windowName, HINSTANCE hInst, int cmdShow,
LRESULT (WINAPI *pWndProc)(HWND,UINT,WPARAM,LPARAM),
LPCTSTR menuName, int x, int y, int width, int height,
UINT classStyle, DWORD windowStyle, HWND hParent)
{
wchar_t szClassName[22] = L"KWndClass";
wchar_t buff[12];
std::basic_stringstream<wchar_t> ss;
ss<<++class_count;
ss>>buff;
wcscat_s(szClassName,buff);
wc.cbSize = sizeof(wc);
wc.style = classStyle;
wc.lpfnWndProc = pWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
//ConstPtrAssign(wc.lpszMenuName,menuName); оказывается wc.lpszMenuName - не строка
wc.lpszMenuName=menuName;
ConstPtrAssign(wc.lpszClassName,szClassName);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
wchar_t msg[100] = L"Cannot register class: ";
wcscat_s(msg,szClassName);
MessageBox(NULL, msg, L"Error", MB_OK);
}
else
CreateNewWin(windowName, hInst, cmdShow, windowStyle);
}
void KWnd::CreateNewWin(LPCTSTR windowName, HINSTANCE hInst, int cmdShow, DWORD windowStyle, int x, int y, int width, int height, HWND hParent)
{
HWND h=CreateWindow(wc.lpszClassName, windowName, windowStyle, x, y,
width, height, hParent, (HMENU)NULL, hInst, NULL);
if (!h)
{
wchar_t msg[100] = L"Cannot create window: ";
wcscat_s(msg,windowName);
MessageBox(NULL, msg, L"Error", MB_OK);
}
else
{
hWnd.push_back(h);
ShowWindow(hWnd.back(), cmdShow);
}
}
void KWnd::ConstPtrAssign(LPCTSTR& dest, LPCTSTR source)
{
if (source)
{
wchar_t* buff = new wchar_t[wcslen(source)];
wcscpy(buff,source);
dest=buff;
}
else dest = NULL;
}
KWnd::~KWnd()
{
//delete[] wc.lpszClassName; типа удаляется системой
}
class KBeginPaint
{
public:
KBeginPaint(HWND _hwnd): hwnd(_hwnd), hdc(BeginPaint(_hwnd,&ps)) {}
operator HDC& () { return hdc; }
~KBeginPaint() { EndPaint(hwnd,&ps); }
private:
HDC hdc;
HWND hwnd;
PAINTSTRUCT ps;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment