|
|
|

:
case WM_COMMAND:
{
RECT rc;
GetWindowRect(hWnd, &rc);
OffsetRect(&rc, 20, 0);
MoveWindow(hWnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE);
}
// Set WS_EX_LAYERED on this window SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED); // Make this window 50% alpha SetLayeredWindowAttributes(hWnd, 0, (255 * 50) / 100, LWA_ALPHA); // Show this window ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);
int size = 150;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
i * 150, j * 150, 150, 150, NULL, NULL, hInstance, NULL);
}
}
case WM_SIZE:
{
// get the size of the client area
int clientWidth = LOWORD(lParam);
int clientHeight = HIWORD(lParam);
// get the size of the window
RECT rc;
GetWindowRect(hWnd, &rc);
// modify the caption of the window
TCHAR s[256];
_stprintf_s(s, 256, _T("Window's size: %d x %d Client area's size: %d x %d"),
rc.right - rc.left, rc.bottom - rc.top, clientWidth, clientHeight);
SetWindowText(hWnd, s);
}
break;
wchar_t s[256];
swprintf_s(s, 256, L"Window's size: %d x %d Client area's size: %d x %d",
rc.right - rc.left, rc.bottom - rc.top, clientWidth, clientHeight);
SetWindowText(hWnd, s);
case WM_GETMINMAXINFO:
{
MINMAXINFO *minMaxInfo = (MINMAXINFO*)lParam;
minMaxInfo->ptMaxSize.x = minMaxInfo->ptMaxTrackSize.x = 500;
minMaxInfo->ptMaxSize.y = minMaxInfo->ptMaxTrackSize.y = 200;
}
break;
case WM_SIZING:
{
RECT *rc = (RECT*)lParam;
if (wParam == WMSZ_BOTTOM || wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_BOTTOMRIGHT ||
wParam == WMSZ_TOP || wParam == WMSZ_TOPLEFT || wParam == WMSZ_TOPRIGHT)
{
rc->right = rc->left + rc->bottom - rc->top;
} else {
rc->bottom = rc->top + rc->right - rc->left;
}
}
break;
case WM_CREATE:
SetTimer(hWnd, 7, 250, NULL);
break;
case WM_TIMER:
{
RECT rc;
// get the center of the work area of the system
SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
int centerX = (rc.left + rc.right + 1) / 2;
int centerY = (rc.top + rc.bottom + 1) / 2;
// get current size of the window
GetWindowRect(hWnd, &rc);
int currentSize = max(rc.right - rc.left, rc.bottom - rc.top);
// modify size of the window
currentSize += stepSize;
if (currentSize >= maxSize) {
stepSize = -abs(stepSize);
} else if (currentSize <= minSize) {
stepSize = abs(stepSize);
}
MoveWindow(hWnd, centerX - currentSize / 2, centerY - currentSize / 2,
currentSize, currentSize, TRUE);
}
break;
const int minSize = 200; const int maxSize = 400; static int stepSize = 10;(be sure to understand the importance of the static keyword)
void GetTextInfoForMouseMsg(WPARAM wParam, LPARAM lParam, TCHAR *msgName,
TCHAR *buf, int bufSize)
{
short x = (short)LOWORD(lParam);
short y = (short)HIWORD(lParam);
_stprintf_s(buf, bufSize, _T("%s x: %d, y: %d, vk:"), msgName, x, y);
if ((wParam & MK_LBUTTON) != 0) {
_tcscat_s(buf, bufSize, _T(" LEFT"));
}
if ((wParam & MK_MBUTTON) != 0) {
_tcscat_s(buf, bufSize, _T(" MIDDLE"));
}
if ((wParam & MK_RBUTTON) != 0) {
_tcscat_s(buf, bufSize, _T(" RIGHT"));
}
}
case WM_LBUTTONDOWN:
GetTextInfoForMouseMsg(wParam, lParam, _T("LBUTTONDOWN"), buf, bufSize);
SetWindowText(hWnd, buf);
break;
case WM_LBUTTONUP:
GetTextInfoForMouseMsg(wParam, lParam, _T("LBUTTONUP"), buf, bufSize);
SetWindowText(hWnd, buf);
break;
case WM_RBUTTONDOWN:
GetTextInfoForMouseMsg(wParam, lParam, _T("RBUTTONDOWN"), buf, bufSize);
SetWindowText(hWnd, buf);
break;
case WM_RBUTTONUP:
GetTextInfoForMouseMsg(wParam, lParam, _T("RBUTTONUP"), buf, bufSize);
SetWindowText(hWnd, buf);
break;
case WM_LBUTTONDBLCLK:
GetTextInfoForMouseMsg(wParam, lParam, _T("LBUTTONDBLCLK"), buf, bufSize);
SetWindowText(hWnd, buf);
break;
case WM_MBUTTONDBLCLK:
GetTextInfoForMouseMsg(wParam, lParam, _T("MBUTTONDBLCLK"), buf, bufSize);
SetWindowText(hWnd, buf);
break;
case WM_RBUTTONDBLCLK:
GetTextInfoForMouseMsg(wParam, lParam, _T("RBUTTONDBLCLK"), buf, bufSize);
SetWindowText(hWnd, buf);
break;
const int bufSize = 256; TCHAR buf[bufSize];Run the application and test changing the caption of the window when left, middle, or right mouse button is pressed or released
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;When the CS_DBLCLKS style is applied, the following sequence of messages is sent when the user use the double click:
void GetTextInfoForMouseMsg(HWND hWnd, WPARAM wParam, LPARAM lParam, TCHAR *msgName,
TCHAR *buf, int bufSize)
{
short x = (short)LOWORD(lParam);
short y = (short)HIWORD(lParam);
POINT pt = {x, y};
ClientToScreen(hWnd, &pt);
_stprintf_s(buf, bufSize, _T("%s x: %d, y: %d, (sx: %d, sy: %d) vk:"),
msgName, x, y, pt.x, pt.y);
case WM_LBUTTONDOWN:
GetTextInfoForMouseMsg(hWnd, wParam, lParam, _T("LBUTTONDOWN"), buf, bufSize);
SetWindowText(hWnd, buf);
SetCapture(hWnd);
break;
case WM_LBUTTONUP:
ReleaseCapture();
GetTextInfoForMouseMsg(hWnd, wParam, lParam, _T("LBUTTONUP"), buf, bufSize);
SetWindowText(hWnd, buf);
break;
Note, that in this case negative values for mouse coordinations can be sent, so it is very important to use signed integer type to use this value: x = (short)LOWORD(lParam); (without casting to short type big positive values would be used)
case WM_CREATE:
cursor = LoadCursor(NULL, IDC_HAND);
break;
case WM_SETCURSOR:
SetCursor(cursor);
return TRUE;
Add also a declaration of the cursor variable (if it is a local variable, the static keyword must be used)
static HCURSOR cursor = NULL;
case WM_NCHITTEST:
return HTCAPTION;
void GetTextInfoForKeyMsg(WPARAM wParam, TCHAR *msgName, TCHAR *buf, int bufSize)
{
static int counter = 0;
counter++;
_stprintf_s(buf, bufSize, _T("%s key: %d (counter: %d)"), msgName, wParam, counter);
}
case WM_KEYDOWN:
GetTextInfoForKeyMsg(wParam, _T("KEYDOWN"), buf, bufSize);
SetWindowText(hWnd, buf);
break;
case WM_KEYUP:
GetTextInfoForKeyMsg(wParam, _T("KEYUP"), buf, bufSize);
SetWindowText(hWnd, buf);
break;
case WM_CHAR:
_stprintf_s(buf, bufSize, _T("WM_CHAR: %c"), (TCHAR)wParam);
SetWindowText(hWnd, buf);
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
TCHAR s[] = _T("Hello world!");
TextOut(hdc, 0, 0, s, (int)_tcslen(s));
EndPaint(hWnd, &ps);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
TCHAR s[] = _T("Hello world!");
RECT rc;
GetClientRect(hWnd, &rc);
DrawText(hdc, s, (int)_tcslen(s), &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hWnd, &ps);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
HPEN pen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
HPEN oldPen = (HPEN)SelectObject(hdc, pen);
MoveToEx(hdc, 0, 0, NULL);
LineTo(hdc, 100, 100);
SelectObject(hdc, oldPen);
DeleteObject(pen);
EndPaint(hWnd, &ps);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
HPEN pen = CreatePen(PS_DOT, 1, RGB(255, 0, 0));
HPEN oldPen = (HPEN)SelectObject(hdc, pen);
HBRUSH brush = CreateSolidBrush(RGB(0, 128, 0));
HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, brush);
Rectangle(hdc, 20, 20, 120, 120);
SelectObject(hdc, oldPen);
DeleteObject(pen);
SelectObject(hdc, oldBrush);
DeleteObject(brush);
EndPaint(hWnd, &ps);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
TCHAR s[] = _T("Hello world!");
HFONT font = CreateFont(
-MulDiv(24, GetDeviceCaps(hdc, LOGPIXELSY), 72), // Height
0, // Width
0, // Escapement
0, // Orientation
FW_BOLD, // Weight
false, // Italic
FALSE, // Underline
0, // StrikeOut
EASTEUROPE_CHARSET, // CharSet
OUT_DEFAULT_PRECIS, // OutPrecision
CLIP_DEFAULT_PRECIS, // ClipPrecision
DEFAULT_QUALITY, // Quality
DEFAULT_PITCH | FF_SWISS, // PitchAndFamily
_T("Verdana")); // Facename
HFONT oldFont = (HFONT)SelectObject(hdc, font);
RECT rc;
GetClientRect(hWnd, &rc);
DrawText(hdc, s, (int)_tcslen(s), &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
SelectObject(hdc, oldFont);
DeleteObject(font);
EndPaint(hWnd, &ps);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
TCHAR s[] = _T("Hello world!");
HBITMAP bitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP oldBitmap = (HBITMAP)SelectObject(memDC, bitmap);
BitBlt(hdc, 0, 0, 48, 48, memDC, 0, 0, SRCCOPY);
StretchBlt(hdc, 200, 100, -200, 100, memDC, 0, 0, 48, 48, SRCCOPY);
SelectObject(memDC, oldBitmap);
DeleteObject(bitmap);
DeleteDC(memDC);
EndPaint(hWnd, &ps);
}
break;
case WM_LBUTTONDOWN:
{
hdc = GetDC(hWnd);
HBRUSH brush = CreateSolidBrush(RGB(128, 128, 0));
HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, brush);
short x = (short)LOWORD(lParam);
short y = (short)HIWORD(lParam);
const int rad = 5;
Ellipse(hdc, x - rad, y - rad, x + rad, y + rad);
SelectObject(hdc, oldBrush);
DeleteObject(brush);
ReleaseDC(hWnd, hdc);
}
break;
#include <list> using namespace std;
static list<POINT> pointsList;
case WM_LBUTTONDOWN:
{
POINT pt;
pt.x = (short)LOWORD(lParam);
pt.y = (short)HIWORD(lParam);
pointsList.push_back(pt);
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
HBRUSH brush = CreateSolidBrush(RGB(128, 128, 0));
HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, brush);
list<POINT>::const_iterator iter = pointsList.begin();
while (iter != pointsList.end()) {
POINT pt = *iter;
const int rad = 5;
Ellipse(hdc, pt.x - rad, pt.y - rad, pt.x + rad, pt.y + rad);
iter++;
}
SelectObject(hdc, oldBrush);
DeleteObject(brush);
EndPaint(hWnd, &ps);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
RECT rc;
GetClientRect(hWnd, &rc);
HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, (HBRUSH)GetStockObject(GRAY_BRUSH));
Rectangle(hdc, 0, 0, rc.right, rc.bottom);
SelectObject(hdc, (HBRUSH)GetStockObject(BLACK_BRUSH));
const int margin = 50;
Rectangle(hdc, margin, margin, rc.right - margin, rc.bottom - margin);
SelectObject(hdc, oldBrush);
EndPaint(hWnd, &ps);
}
break;
case WM_ERASEBKGND:
return 1;
static HDC offDC = NULL; static HBITMAP offOldBitmap = NULL; static HBITMAP offBitmap = NULL;
case WM_CREATE:
hdc = GetDC(hWnd);
offDC = CreateCompatibleDC(hdc);
ReleaseDC(hWnd, hdc);
break;
case WM_SIZE:
{
int clientWidth = LOWORD(lParam);
int clientHeight = HIWORD(lParam);
hdc = GetDC(hWnd);
if (offOldBitmap != NULL) {
SelectObject(offDC, offOldBitmap);
}
if (offBitmap !=- NULL) {
DeleteObject(offBitmap);
}
offBitmap = CreateCompatibleBitmap(hdc, clientWidth, clientHeight);
offOldBitmap = (HBITMAP)SelectObject(offDC, offBitmap);
ReleaseDC(hWnd, hdc);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
RECT rc;
GetClientRect(hWnd, &rc);
HBRUSH oldBrush = (HBRUSH)SelectObject(offDC, (HBRUSH)GetStockObject(GRAY_BRUSH));
Rectangle(offDC, 0, 0, rc.right, rc.bottom);
SelectObject(offDC, (HBRUSH)GetStockObject(BLACK_BRUSH));
const int margin = 50;
Rectangle(offDC, margin, margin, rc.right - margin, rc.bottom - margin);
SelectObject(offDC, oldBrush);
BitBlt(hdc, 0, 0, rc.right, rc.bottom, offDC, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
}
break;
case WM_ERASEBKGND:
return 1;
case WM_DESTROY:
if (offOldBitmap != NULL) {
SelectObject(offDC, offOldBitmap);
}
if (offDC != NULL) {
DeleteDC(offDC);
}
if (offBitmap != NULL) {
DeleteObject(offBitmap);
}
PostQuitMessage(0);
break;