fix files

This commit is contained in:
2020-01-20 07:01:00 +03:00
parent c7d908b4d5
commit 81510d5105
8 changed files with 205 additions and 178 deletions

View File

@@ -1,27 +1,24 @@
//{{NO_DEPENDENCIES}} // resource.h
// Включаемый файл, созданный в Microsoft Visual C++. #define IDS_APP_TITLE 100
// Используется wCenterWindow.rc #define IDS_CLASSNAME 101
#define IDS_HELPTEXT 102
#define IDI_TRAYICON 103
#define IDR_MENU 104
#define ID_POPUPMENU_HELP 105
#define ID_POPUPMENU_EXIT 106
#define IDS_ERR_MAIN 107
#define IDS_ERR_WND 108
#define IDS_ERR_ICON 109
#define IDS_ERR_MENU 110
#define IDS_ERR_POPUP 111
#define IDS_ERR_HOOK 112
#define IDS_RUNNING 113
#define IDC_STATIC -1
#define IDS_APP_TITLE 103 // Next default values for new objects
#define IDR_MAINFRAME 128
#define IDD_WCENTERWINDOW_DIALOG 102
#define IDD_ABOUTBOX 103
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDI_WCENTERWINDOW 107
#define IDI_SMALL 108
#define IDC_WCENTERWINDOW 109
#define IDC_MYICON 2
#ifndef IDC_STATIC
#define IDC_STATIC -1
#endif
// Следующие стандартные значения для новых объектов
//
#ifdef APSTUDIO_INVOKED #ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS #ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NO_MFC 1
#define _APS_NO_MFC 130
#define _APS_NEXT_RESOURCE_VALUE 129 #define _APS_NEXT_RESOURCE_VALUE 129
#define _APS_NEXT_COMMAND_VALUE 32771 #define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_CONTROL_VALUE 1000

View File

@@ -8,6 +8,8 @@
#define WIN32_LEAN_AND_MEAN // Исключите редко используемые компоненты из заголовков Windows #define WIN32_LEAN_AND_MEAN // Исключите редко используемые компоненты из заголовков Windows
// Файлы заголовков Windows // Файлы заголовков Windows
#include <windows.h> #include <windows.h>
#include <strsafe.h>
#include <shellapi.h>
// Файлы заголовков среды выполнения C // Файлы заголовков среды выполнения C
#include <stdlib.h> #include <stdlib.h>
#include <malloc.h> #include <malloc.h>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

View File

@@ -4,177 +4,207 @@
#include "framework.h" #include "framework.h"
#include "wCenterWindow.h" #include "wCenterWindow.h"
#define MAX_LOADSTRING 100 #define MAX_LOADSTRING 80
#define WM_WCW WM_USER + 0x7F00
// Глобальные переменные: // Глобальные переменные:
HINSTANCE hInst; // текущий экземпляр HINSTANCE hInst; // Текущий экземпляр
WCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка WCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка
WCHAR szWindowClass[MAX_LOADSTRING]; // имя класса главного окна WCHAR szClass[MAX_LOADSTRING]; // Имя класса главного окна
WCHAR szHelp[MAX_LOADSTRING]; // Текст описания
HHOOK KeyboardHook;
HICON hIcon;
HMENU hMenu, hPopup;
HWND hWnd;
NOTIFYICONDATA nid = { 0 };
KBDLLHOOKSTRUCT *pkhs;
int dtCenterX, dtCenterY;
bool pressed = FALSE;
// Отправить объявления функций, включенных в этот модуль кода: // Прототипы функций
ATOM MyRegisterClass(HINSTANCE hInstance); ATOM MyRegisterClass(HINSTANCE);
BOOL InitInstance(HINSTANCE, int); VOID ShowError(HINSTANCE, UINT);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK KeyboardHookProc(int, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, // Точка входа
_In_opt_ HINSTANCE hPrevInstance, int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_ LPWSTR lpCmdLine, _In_opt_ HINSTANCE hPrevInstance,
_In_ int nCmdShow) _In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{ {
UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Разместите код здесь. LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDS_CLASSNAME, szClass, MAX_LOADSTRING);
if (FindWindow(szClass, NULL))
{
ShowError(hInstance, IDS_RUNNING);
return FALSE;
}
MyRegisterClass(hInstance);
hWnd = CreateWindowExW(0, szClass, szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
ShowError(hInstance, IDS_ERR_WND);
return FALSE;
}
// Инициализация глобальных строк nid.cbSize = sizeof(NOTIFYICONDATA);
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); nid.hWnd = hWnd;
LoadStringW(hInstance, IDC_WCENTERWINDOW, szWindowClass, MAX_LOADSTRING); nid.uVersion = NOTIFYICON_VERSION;
MyRegisterClass(hInstance); nid.uCallbackMessage = WM_WCW;
nid.hIcon = hIcon;
nid.uID = IDI_TRAYICON;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.dwInfoFlags = NIIF_INFO;
StringCchCopy(nid.szTip, sizeof(nid.szTip), szTitle);
if (!Shell_NotifyIcon(NIM_ADD, &nid))
{
ShowError(hInstance, IDS_ERR_ICON);
SendMessage(hWnd, WM_CLOSE, NULL, NULL);
}
// Выполнить инициализацию приложения: hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU));
if (!InitInstance (hInstance, nCmdShow)) if (!hMenu)
{ {
return FALSE; ShowError(hInstance, IDS_ERR_MENU);
} SendMessage(hWnd, WM_CLOSE, NULL, NULL);
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WCENTERWINDOW)); hPopup = GetSubMenu(hMenu, 0);
if (!hPopup)
{
ShowError(hInstance, IDS_ERR_POPUP);
SendMessage(hWnd, WM_CLOSE, NULL, NULL);
}
MSG msg; KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, hInstance, NULL);
if (!KeyboardHook)
{
ShowError(hInstance, IDS_ERR_HOOK);
SendMessage(hWnd, WM_CLOSE, NULL, NULL);
}
// Цикл основного сообщения: LoadStringW(hInstance, IDS_HELPTEXT, szHelp, MAX_LOADSTRING);
while (GetMessage(&msg, nullptr, 0, 0)) RECT dtrc = { 0 };
{ SystemParametersInfo(SPI_GETWORKAREA, NULL, &dtrc, FALSE);
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) dtCenterX = dtrc.right / 2, dtCenterY = dtrc.bottom / 2;
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam; MSG msg;
BOOL bRet;
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if (bRet == -1)
{
ShowError(hInstance, IDS_ERR_MAIN);
return -1;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
} }
//
// ФУНКЦИЯ: MyRegisterClass()
//
// ЦЕЛЬ: Регистрирует класс окна.
//
ATOM MyRegisterClass(HINSTANCE hInstance) ATOM MyRegisterClass(HINSTANCE hInstance)
{ {
WNDCLASSEXW wcex; WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbSize = sizeof(WNDCLASSEX); wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TRAYICON));
wcex.lpfnWndProc = WndProc; wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.cbClsExtra = 0; wcex.lpszClassName = szClass;
wcex.cbWndExtra = 0; wcex.hIconSm = wcex.hIcon;
wcex.hInstance = hInstance; hIcon = wcex.hIcon;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WCENTERWINDOW)); return RegisterClassEx(&wcex);
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WCENTERWINDOW);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
} }
//
// ФУНКЦИЯ: InitInstance(HINSTANCE, int)
//
// ЦЕЛЬ: Сохраняет маркер экземпляра и создает главное окно
//
// КОММЕНТАРИИ:
//
// В этой функции маркер экземпляра сохраняется в глобальной переменной, а также
// создается и выводится главное окно программы.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Сохранить маркер экземпляра в глобальной переменной
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// ФУНКЦИЯ: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// ЦЕЛЬ: Обрабатывает сообщения в главном окне.
//
// WM_COMMAND - обработать меню приложения
// WM_PAINT - Отрисовка главного окна
// WM_DESTROY - отправить сообщение о выходе и вернуться
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ {
switch (message) switch (message)
{ {
case WM_COMMAND: case WM_WCW:
{ if (lParam == WM_RBUTTONDOWN && wParam == IDI_TRAYICON)
int wmId = LOWORD(wParam); {
// Разобрать выбор в меню: SetForegroundWindow(hWnd);
switch (wmId) POINT pt;
{ GetCursorPos(&pt);
case IDM_ABOUT: int idMenu = TrackPopupMenu(hPopup, TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL);
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); if (idMenu == ID_POPUPMENU_HELP && !pressed)
break; {
case IDM_EXIT: pressed = TRUE;
DestroyWindow(hWnd); if (MessageBox(hWnd, szHelp, szTitle, MB_OK | MB_TOPMOST) == IDOK) pressed = FALSE;
break; }
default: if (idMenu == ID_POPUPMENU_EXIT) SendMessage(hWnd, WM_CLOSE, NULL, NULL);
return DefWindowProc(hWnd, message, wParam, lParam); }
} break;
}
break; case WM_DESTROY:
case WM_PAINT: if (KeyboardHook) UnhookWindowsHookEx(KeyboardHook);
{ if (hMenu) DestroyMenu(hMenu);
PAINTSTRUCT ps; Shell_NotifyIcon(NIM_DELETE, &nid);
HDC hdc = BeginPaint(hWnd, &ps); PostQuitMessage(0);
// TODO: Добавьте сюда любой код прорисовки, использующий HDC... break;
EndPaint(hWnd, &ps);
} default:
break; return DefWindowProc(hWnd, message, wParam, lParam);
case WM_DESTROY: }
PostQuitMessage(0); return 0;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
} }
// Обработчик сообщений для окна "О программе". LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{ {
UNREFERENCED_PARAMETER(lParam); pkhs = (KBDLLHOOKSTRUCT*)lParam;
switch (message) bool bLCTRL, bLWIN;
{ GetAsyncKeyState(VK_LCONTROL) < 0 ? bLCTRL = TRUE : bLCTRL = FALSE;
case WM_INITDIALOG: GetAsyncKeyState(VK_LWIN) < 0 ? bLWIN = TRUE : bLWIN = FALSE;
return (INT_PTR)TRUE;
case WM_COMMAND: if ((bLCTRL && bLWIN && pkhs->vkCode == 0x43) && !pressed)
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
{ pressed = TRUE;
EndDialog(hDlg, LOWORD(wParam)); HWND fgWindow = GetForegroundWindow();
return (INT_PTR)TRUE; if (fgWindow)
} {
break; HWND parentWindow = fgWindow;
} while (TRUE)
return (INT_PTR)FALSE; {
parentWindow = GetParent(fgWindow);
if (parentWindow) fgWindow = parentWindow;
else break;
}
WINDOWPLACEMENT wp = { 0 };
wp.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(fgWindow, &wp);
if (wp.showCmd == SW_SHOWNORMAL)
{
int fgW = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
int fgH = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
int fgX = dtCenterX - (fgW / 2);
int fgY = dtCenterY - (fgH / 2);
wp.rcNormalPosition.left = fgX;
wp.rcNormalPosition.top = fgY;
wp.rcNormalPosition.right = fgX + fgW;
wp.rcNormalPosition.bottom = fgY + fgH;
SendMessage(fgWindow, WM_ENTERSIZEMOVE, NULL, NULL);
SetWindowPlacement(fgWindow, &wp);
SendMessage(fgWindow, WM_EXITSIZEMOVE, NULL, NULL);
}
}
pressed = FALSE;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
VOID ShowError(HINSTANCE hInstance, UINT uID)
{
WCHAR szErrorText[MAX_LOADSTRING]; // Текст ошибки
LoadStringW(hInstance, uID, szErrorText, MAX_LOADSTRING);
MessageBox(hWnd, szErrorText, szTitle, MB_OK | MB_ICONERROR);
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

View File

@@ -123,12 +123,14 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<DebugInformationFormat>None</DebugInformationFormat>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>false</GenerateDebugInformation>
<AssemblyDebug>false</AssemblyDebug>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -152,7 +154,7 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="framework.h" /> <ClInclude Include="framework.h" />
<ClInclude Include="Resource.h" /> <ClInclude Include="resource.h" />
<ClInclude Include="targetver.h" /> <ClInclude Include="targetver.h" />
<ClInclude Include="wCenterWindow.h" /> <ClInclude Include="wCenterWindow.h" />
</ItemGroup> </ItemGroup>
@@ -163,7 +165,6 @@
<ResourceCompile Include="wCenterWindow.rc" /> <ResourceCompile Include="wCenterWindow.rc" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Image Include="small.ico" />
<Image Include="wCenterWindow.ico" /> <Image Include="wCenterWindow.ico" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@@ -21,10 +21,10 @@
<ClInclude Include="targetver.h"> <ClInclude Include="targetver.h">
<Filter>Файлы заголовков</Filter> <Filter>Файлы заголовков</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Resource.h"> <ClInclude Include="wCenterWindow.h">
<Filter>Файлы заголовков</Filter> <Filter>Файлы заголовков</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="wCenterWindow.h"> <ClInclude Include="resource.h">
<Filter>Файлы заголовков</Filter> <Filter>Файлы заголовков</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
@@ -39,9 +39,6 @@
</ResourceCompile> </ResourceCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Image Include="small.ico">
<Filter>Файлы ресурсов</Filter>
</Image>
<Image Include="wCenterWindow.ico"> <Image Include="wCenterWindow.ico">
<Filter>Файлы ресурсов</Filter> <Filter>Файлы ресурсов</Filter>
</Image> </Image>