Caption bar will display the (column,row) of the cell clicked.
Code:
#include <windows.h>
#define MATRIX_WIDTH 5
#define MATRIX_HEIGHT 4
#define CELL_WIDTH 100
#define CELL_HEIGHT 100
LRESULT CALLBACK WndProc( HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam )
{
static char matrix[MATRIX_WIDTH][MATRIX_HEIGHT];
static HBRUSH brush = (HBRUSH)GetStockObject(BLACK_BRUSH);
switch(msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT rc;
HDC hdc;
hdc = BeginPaint( hwnd, &ps );
for( int row = 0; row < MATRIX_HEIGHT; ++row )
{
for( int column = 0; column < MATRIX_WIDTH; ++column )
{
rc.left = column * CELL_WIDTH;
rc.top = row * CELL_HEIGHT;
rc.right = rc.left + CELL_WIDTH;
rc.bottom = rc.top + CELL_HEIGHT;
FrameRect( hdc, &rc, brush );
}
}
EndPaint ( hwnd, &ps );
return 0;
}
case WM_LBUTTONDOWN:
{
int x = LOWORD(lParam);
int y = HIWORD(lParam);
char buffer[100];
wsprintf( buffer, "You clicked in column %d, row %d",
x/CELL_WIDTH, y/CELL_HEIGHT );
SetWindowText( hwnd, buffer );
return 0;
}
case WM_DESTROY:
{
PostQuitMessage ( 0 );
}
default: return DefWindowProc(hwnd,msg,wParam,lParam);
}
}
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR args, int nShow )
{
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "Minimum" );
wc.hInstance = hInst ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
RECT rc = { 0, 0, MATRIX_WIDTH * CELL_WIDTH, MATRIX_HEIGHT * CELL_HEIGHT };
AdjustWindowRect( &rc, WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU, FALSE );
HWND hwnd = CreateWindow(wc.lpszClassName,TEXT("Click on a cell."),
WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU ,
0,0,rc.right-rc.left,rc.bottom-rc.top,0,0,hInst,0);
MSG msg ;
while( GetMessage(&msg,0,0,0) > 0 ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}