Skip to content

Instantly share code, notes, and snippets.

@Gelio
Created March 8, 2017 09:53
Show Gist options
  • Save Gelio/10533af4fcc2bfb0accb9b5c9befcff7 to your computer and use it in GitHub Desktop.
Save Gelio/10533af4fcc2bfb0accb9b5c9befcff7 to your computer and use it in GitHub Desktop.
// lab1-proba.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "lab1-proba.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_LAB1PROBA, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LAB1PROBA));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAB1PROBA));
wcex.hCursor = LoadCursor(nullptr, IDC_HAND);
wcex.hbrBackground = CreateSolidBrush(RGB(0,0,0));
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_LAB1PROBA);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
int screenWidth = GetSystemMetrics(SM_CXSCREEN),
screenHeight = GetSystemMetrics(SM_CYSCREEN);
long windowsStyles = WS_EX_TOPMOST | WS_EX_LAYERED | WS_POPUP;
windows[0] = CreateWindowW(szWindowClass, szTitle, windowsStyles, screenWidth - WINDOW_SIZE, 0, WINDOW_SIZE, WINDOW_SIZE, nullptr, nullptr, hInstance, nullptr);
windows[1] = CreateWindowW(szWindowClass, szTitle, windowsStyles, 0, 0, WINDOW_SIZE, WINDOW_SIZE, nullptr, nullptr, hInstance, nullptr);
windows[2] = CreateWindowW(szWindowClass, szTitle, windowsStyles, 0, screenHeight - WINDOW_SIZE, WINDOW_SIZE, WINDOW_SIZE, nullptr, nullptr, hInstance, nullptr);
windows[3] = CreateWindowW(szWindowClass, szTitle, windowsStyles, screenWidth - WINDOW_SIZE, screenHeight - WINDOW_SIZE, WINDOW_SIZE, WINDOW_SIZE, nullptr, nullptr, hInstance, nullptr);
SetWindowPos(windows[0], HWND_TOPMOST, screenWidth - WINDOW_SIZE, 0, WINDOW_SIZE, WINDOW_SIZE, SWP_SHOWWINDOW);
SetWindowPos(windows[1], HWND_TOPMOST, 0, 0, WINDOW_SIZE, WINDOW_SIZE, SWP_SHOWWINDOW);
SetWindowPos(windows[2], HWND_TOPMOST, 0, screenHeight - WINDOW_SIZE, WINDOW_SIZE, WINDOW_SIZE, SWP_SHOWWINDOW);
SetWindowPos(windows[3], HWND_TOPMOST, screenWidth - WINDOW_SIZE, screenHeight - WINDOW_SIZE, WINDOW_SIZE, WINDOW_SIZE, SWP_SHOWWINDOW);
for (int i = 0; i < 4; i++)
if (!windows[i])
return FALSE;
for (int i = 0; i < 4; i++)
{
SetWindowInitialTransparency(windows[i]);
ShowWindow(windows[i], nCmdShow);
UpdateWindow(windows[i]);
}
for (int i = 1; i < 4; i++)
{
HWND hWnd = windows[i];
long style = GetWindowLong(hWnd, GWL_STYLE);
style |= WS_EX_TOOLWINDOW; // flags don't work - windows remains in taskbar
style &= ~(WS_EX_APPWINDOW);
SetWindowLong(hWnd, GWL_EXSTYLE, style); // set the style
}
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
for (int i = 0; i < 4; i++)
timers[i] = 0;
SetTimer(hWnd, 0, 100, NULL);
HMENU menu = GetSystemMenu(hWnd, FALSE);
RemoveMenu(menu, 1, MF_BYPOSITION);
SetMenu(hWnd, menu);
}
break;
case WM_TIMER:
ChangeWindowOpacity(hWnd);
break;
case WM_KEYDOWN:
return MoveWindowsSymetrically(hWnd, message, wParam, lParam);
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
LRESULT CALLBACK MoveWindowsSymetrically(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int quadrant = GetQuadrant(hWnd);
// Difference with regards to the window in the second quadrant (top-left)
int xDifference = 0,
yDifference = 0;
switch (wParam)
{
case VK_LEFT:
if (quadrant == 2 || quadrant == 3)
xDifference -= MOVE_STEP;
else
xDifference += MOVE_STEP;
break;
case VK_RIGHT:
if (quadrant == 2 || quadrant == 3)
xDifference += MOVE_STEP;
else
xDifference -= MOVE_STEP;
break;
case VK_UP:
if (quadrant == 1 || quadrant == 2)
yDifference -= MOVE_STEP;
else
yDifference += MOVE_STEP;
break;
case VK_DOWN:
if (quadrant == 1 || quadrant == 2)
yDifference += MOVE_STEP;
else
yDifference -= MOVE_STEP;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
MoveWindows(xDifference, yDifference);
return 0;
}
void MoveWindows(int xDifference, int yDifference)
{
RECT topLeftWindowRect;
GetWindowRect(windows[1], &topLeftWindowRect);
topLeftWindowRect.left += xDifference;
topLeftWindowRect.top += yDifference;
int screenWidth = GetSystemMetrics(SM_CXSCREEN),
screenHeight = GetSystemMetrics(SM_CYSCREEN);
int maxXPos = screenWidth / 2 - WINDOW_SIZE,
maxYPos = screenHeight / 2 - WINDOW_SIZE;
if (topLeftWindowRect.left < 0)
topLeftWindowRect.left = 0;
if (topLeftWindowRect.left > maxXPos)
topLeftWindowRect.left = maxXPos;
if (topLeftWindowRect.top < 0)
topLeftWindowRect.top = 0;
if (topLeftWindowRect.top > maxYPos)
topLeftWindowRect.top = maxYPos;
// Second quadrant
MoveWindow(windows[1], topLeftWindowRect.left, topLeftWindowRect.top, WINDOW_SIZE, WINDOW_SIZE, TRUE);
// First quadrant
MoveWindow(windows[0], screenWidth - topLeftWindowRect.left - WINDOW_SIZE, topLeftWindowRect.top, WINDOW_SIZE, WINDOW_SIZE, TRUE);
// Third quadrant
MoveWindow(windows[2], topLeftWindowRect.left, screenHeight - topLeftWindowRect.top - WINDOW_SIZE, WINDOW_SIZE, WINDOW_SIZE, TRUE);
// Fourth quadrant
MoveWindow(windows[3], screenWidth - topLeftWindowRect.left - WINDOW_SIZE, screenHeight - topLeftWindowRect.top - WINDOW_SIZE, WINDOW_SIZE, WINDOW_SIZE, TRUE);
}
void SetWindowInitialTransparency(HWND hWnd)
{
int quadrant = GetQuadrant(hWnd);
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
if (quadrant == 2 || quadrant == 4)
{
SetLayeredWindowAttributes(hWnd, 0, 0.05 * 255, LWA_ALPHA);
}
else
{
SetLayeredWindowAttributes(hWnd, 0, 0.95 * 255, LWA_ALPHA);
}
}
int GetQuadrant(HWND hWnd)
{
int quadrant = 0;
for (int i = 0; i < 4; i++)
{
if (hWnd == windows[i])
{
quadrant = i + 1;
break;
}
}
return quadrant;
}
void ChangeWindowOpacity(HWND hWnd)
{
int quadrant = GetQuadrant(hWnd);
int nextAlpha = 0;
timers[quadrant - 1] += 5;
timers[quadrant - 1] %= 180;
if (quadrant == 1 || quadrant == 3)
{
// zaczynamy od 5%
nextAlpha = 5;
if (timers[quadrant - 1] <= 90)
nextAlpha += timers[quadrant - 1];
else
nextAlpha += 180 - timers[quadrant - 1];
}
else
{
// zaczynamy od 95%
nextAlpha = 95;
if (timers[quadrant - 1] <= 90)
nextAlpha -= timers[quadrant - 1];
else
nextAlpha -= 180 - timers[quadrant - 1];
}
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hWnd, 0, (255 * nextAlpha) / 100, LWA_ALPHA);
//ShowWindow(hWnd, SW_SHOWNA);
UpdateWindow(hWnd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment