Play with mouse event


When I gatherd information about major example of malware functions, I encounted this article at Wikipedia . I got interest in this behavior, so I coded same function imidiately.
This is joke program coded in c++. I developed this program to study how to handle mouse event in c++.

mouseevt.cpp
#include <windows.h>
#include <string>

#define HEIGHT 60
#define WIDTH 120

#define MARGIN 10

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp);

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch(msg) {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hWnd, msg, wp, lp);
}

int main() {
	WNDCLASS wc ={0};

	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = wc.cbWndExtra=0;
	wc.hInstance = GetModuleHandle(NULL);
	wc.hCursor = wc.hIcon=NULL;
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszClassName = "CatchMeIfYouCan";
	wc.lpszMenuName = NULL;

	if(!RegisterClass(&wc)){
		return false;
	}

	HWND hWnd=CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_NOACTIVATE|WS_EX_WINDOWEDGE ,
			"CatchMeIfYouCan",
			"",
			WS_VISIBLE | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
			0,
			0,
			WIDTH,
			HEIGHT,
			NULL,
			NULL,
			GetModuleHandle(NULL),
			NULL);
	if(hWnd==NULL){
		UnregisterClass("CatchMeIfYouCan", GetModuleHandle(NULL));
		return false;
	}

	RECT rcDesktop = {0};
	RECT rcWnd = {0};

	SystemParametersInfo(SPI_GETWORKAREA,0,&rcDesktop,0);
	GetWindowRect( hWnd, &rcWnd );

	int originalX = ((rcDesktop.right - (rcWnd.right - rcWnd.left)) / 2 );
	int originalY = ((rcDesktop.bottom - (rcWnd.bottom - rcWnd.top)) / 2 );

	SetWindowPos( hWnd, HWND_TOPMOST,
			originalX,
			originalY,
			0, 0,
			(SWP_NOSIZE|SWP_NOACTIVATE) );

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	MSG msg = {0};
	while(1){
		if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)){
			if(!GetMessage(&msg, NULL, 0 ,0 )){
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		} else {
			POINT pt = {0};
			GetCursorPos(&pt);
			GetWindowRect( hWnd, &rcWnd );

			if( rcWnd.left - MARGIN <= pt.x && pt.x <= rcWnd.right + WIDTH + MARGIN &&
				rcWnd.top - MARGIN <= pt.y && pt.y <= rcWnd.bottom + HEIGHT + MARGIN){

				int newX = rcWnd.left;
				int newY = rcWnd.top;
				if((rcWnd.right - pt.x) > (pt.x - rcWnd.left)){
					// left side
					newX += (pt.x - rcWnd.left) + MARGIN;
				} else {
					// right side
					newX -= (rcWnd.right - pt.x) - MARGIN;
				}

				if((rcWnd.bottom - pt.y) > (pt.y - rcWnd.top)){
					// upper side
					newY += (pt.y - rcWnd.top) + MARGIN;
				} else {
					// lower side
					newY -= (rcWnd.bottom - pt.y) - MARGIN;
				}

				if(newX <= rcDesktop.left + MARGIN || rcDesktop.right - MARGIN <= newX ||
				   newY <= rcDesktop.top + MARGIN || rcDesktop.bottom - MARGIN <= newY){
					SetWindowPos( hWnd, HWND_TOPMOST, originalX, originalY, 0, 0, (SWP_NOSIZE|SWP_NOACTIVATE) );
				} else {
					SetWindowPos( hWnd, HWND_TOPMOST, newX, newY, 0, 0, (SWP_NOSIZE|SWP_NOACTIVATE) );
				}
				UpdateWindow(hWnd);
			}
		}
	}
	return 0;
}
Profile
I have technical job experience in enbedded software development and server side infrastructure/application engineering. I'm interested in programming and computer security.
Objective
To write down my technical knowledge in the place where I can access from anywhere. To share my program source code. To train my writing skill.
Link
  • LinkedIn (preparing)

  • Twitter

  • Facebook (preparing)

  • GitHub

  • StackOverFlow (preparing)

Archives