mgsLib  1.3
Mermaja's Graphic Screen. A simple C library to build Windows graphic applications from console programs.
colorSelect.c

This example shows how to use the Color Picker to and two slider bars to select color and size of a matrix of rectangles on screen. It also adds a button to end the execution of the program.

#include <stdio.h>
#include <stdlib.h>
#include <mgsLib.h>
#include <mrmWidgets.h>
#define SCRW 800
#define SCRH 500
#define SIZE 48
#define GAP 2
#define FREE 50
int main() {
// Handlers for the main graphic elements
int scr, cp, slh, slv, btn;
// Variables to control aspect: color, sizes and positions
int col, width, height, nh, nv;
// Generic and auxiliar variables.
int i, j;
char buffer[100];
// Create the main screen and set the font for its texts
if ((scr = mgsCreateScreen(SCRW, SCRH, "Selecting colors", MGS_CBLACK, NULL)) < 0)
exit(EXIT_FAILURE);
mgsSetFont(scr, 30, MGS_CWHITE, MGS_TRANSPARENT);
// Create the color picker widget
if ((cp = mrmCPInit(0x808080, MGS_CWHITE)) < 0)
exit(EXIT_FAILURE);
// Move it and the main window to a suitable place on screen
mrmCPMove(20, 130);
mgsMoveScreen(scr, 300, 150);
// Create the horizontal and vertical sliders and place on top and left sides respectively
slh = mrmSliderInit(scr, MWG_SLHORIZONTAL, 25, SCRW - 50, 0.0, SIZE, MGS_CGREY, MGS_CRED, 0x000080, 0x202020);
slv = mrmSliderInit(scr, MWG_SLVERTICAL, 25, SCRH - 50, 0.0, SIZE, MGS_CGREY, MGS_CRED, 0x000080, 0x202020);
mrmSliderSetPos(slh, 40, 5);
mrmSliderSetPos(slv, 5, 40);
// Create the exit button and set its aspect
btn = mrmButtonInit(scr, 80, 40, NULL, "Exit", MGS_CGREY, 0x202020, MGS_CBLACK);
mrmButtonSetFont(btn, 26, 0, "Arial", MGS_BOLD);
mrmButtonSetFontColor(btn, 0x2020E0, MGS_CRED, 0x2020E0);
mrmButtonSetPos(btn, SCRW - 100, SCRH - 50);
// Calculate the horizontal and vertical number of rectangles
nh = 1 + (SCRW - SIZE - GAP - 50) / (SIZE + GAP);
nv = 1 + (SCRH - SIZE - GAP - 50 - FREE) / (SIZE + GAP);
// Main animation loop: stay until the user presses the button.
while(mrmButtonGetCounter(btn) == 0) {
// Call widget loop
// Read the height and width of the rectangles from the sliders
width = mrmSliderGetVal(slh);
height = mrmSliderGetVal(slv);
// Get the color from the picker
col = mrmCPRGB();
// And prepare the text
sprintf(buffer, "Width: %d - Height: %d", width, height);
// Clear the screen and start to draw
// A small square at the top left corner of complementary color
mgsRectangle(scr, 5, 5, 30, 30, ~col & 0xFFFFFF);
// The matrix of rectangles of the selected color, whose size depends on the sliders
for(i = 0; i < nh; i++)
for(j = 0; j < nv; j++)
mgsRectangle(scr, i * (SIZE + GAP) + 40, j * (SIZE + GAP) + 40, i * (SIZE + GAP) + 40 + width, j * (SIZE + GAP) + 40 + height, col);
// And show the values read with the text
mgsPutsAligned(scr, MGS_THCENTER | MGS_TVCENTER, 0, SCRH - FREE - 30, SCRW, SCRH, buffer);
// Call to draw all widgets
// And synchronize and update screen to end the animation.
}
// Good bye.
exit(EXIT_SUCCESS);
}