WinAPI 32 wrapper in C
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
This is a piece of code using this wrapper to create a simple window and handle some of it's events in Windows environment.
#include "nasui.h"
#include "nasui_ctrls.h"
void ButClick(NASUI_Window *win, NASUI_Button *butn);
void BoxPress(NASUI_Window *win, NASUI_TextBox *tbox);
void ListSel(NASUI_Window *win, NASUI_ListBox *list);
void ScrollScrollScroll(NASUI_Window *win, NASUI_ScrollBar *bar);
void WinCreate(NASUI_Window *win);
int WinClose(NASUI_Window *win);
void WinDestroy(NASUI_Window *win);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow)
NASUI_Window *mywin = NASUI_CreateWindow(hInst, hPrevInst, cmdargs, nCmdShow,
"NASUI", 0, 0, 640, 480);
NASUI_SetOnCreateHandler(mywin, WinCreate);
NASUI_SetOnCloseHandler(mywin, WinClose);
NASUI_SetOnDestroyHandler(mywin, WinDestroy);
NASUI_Run(mywin);
void ListSel(NASUI_Window *win, NASUI_ListBox *list)
char *tofr = NASUI_GetListBoxSelectedItem(list);
printf("selected %sn", tofr);
free(tofr);
void ScrollScrollScroll(NASUI_Window *win, NASUI_ScrollBar *bar)
printf("scroll scroll scrolln");
void WinCreate(NASUI_Window *win)
printf("onCreate()n");
NASUI_Button *butex = NASUI_CreateButton(5, 5, 100, 25, "a button");
NASUI_AddControl(win, (NASUI_Control*)butex);
NASUI_TextBox *tbex = NASUI_CreateTextBox(5, 30, 100, 25, "enter something");
NASUI_AddControl(win, (NASUI_Control*)tbex);
NASUI_ListBox *lst = NASUI_CreateListBox(5, 55, 200, 200);
NASUI_SetOnListBoxSelectHandler(lst, ListSel);
NASUI_AddControl(win, (NASUI_Control*)lst);
NASUI_AddListBoxItem(lst, "THIS");
NASUI_AddListBoxItem(lst, "USEFUL");
NASUI_AddListBoxItem(lst, "ANSI");
NASUI_AddListBoxItem(lst, "àýõúþûÃÂúøüø úþôøÃÂþòúðüø");
NASUI_ScrollBar *scbar = NASUI_CreateScrollBar(205, 55, 25, 200, NASUI_SCROLLBAR_VERTICAL, 100, 5);
NASUI_SetOnScrollBarScrollHandler(scbar, ScrollScrollScroll);
NASUI_AddControl(win, (NASUI_Control*)scbar);
int WinClose(NASUI_Window *win)
printf("onClose()n"); return 1;
void WinDestroy(NASUI_Window *win)
printf("onDestroy()n");
Header files:
nasui.h
#ifndef NASUI_H
#define NASUI_H
#include <windows.h>
typedef struct NASUI_Window NASUI_Window;
typedef struct NASUI_WindowEvents NASUI_WindowEvents;
// WindowEvents possible function pointers
typedef void (*NASUI_onCreate)(NASUI_Window *win);
/*
* Called on window creation. The window aren't even shown now, either no controls are attached.
*/
typedef int (*NASUI_onClose)(NASUI_Window *win);
/*
* Called on user attempt to close the window. Can be cancelled by returning zero.
*/
typedef void (*NASUI_onDestroy)(NASUI_Window *win);
/*
* Called on the window destruction. Can't be cancelled.
*/
NASUI_Window * NASUI_CreateWindow(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow,
const char *title, int x, int y, int width, int height);
/*
* First four arguments are fetched from WinMain() and should not be modified.
* This function prepares Window structure to create a win32 window
*/
void NASUI_SetOnCreateHandler(NASUI_Window *win, NASUI_onCreate handler);
void NASUI_SetOnCloseHandler(NASUI_Window *win, NASUI_onClose handler);
void NASUI_SetOnDestroyHandler(NASUI_Window *win, NASUI_onDestroy handler);
void NASUI_Run(NASUI_Window *win);
/*
* This function creates actual window and enters an infinitive loop for fetching and
* handling Windows OS messages sent to the window.
* CAUTION: use only when all preparations are done or moved to another thread!!!
*/
#endif
nasui_ctrls.h
#ifndef NASUI_CTRLS_H
#define NASUI_CTRLS_H
#include "nasui.h"
typedef struct NASUI_Control NASUI_Control;
void NASUI_AddControl(NASUI_Window *win, NASUI_Control *ctrl);
/*
* Generic polymorhpic structure.
* Contains generic information about control's coordinates, size, handler and type.
* It's children are declared below.
*/
//////////////////
// Button 1
typedef struct NASUI_Button NASUI_Button;
NASUI_Button * NASUI_CreateButton(int x, int y, int width, int height, const char *text);
typedef void (*NASUI_onButtonClick)(NASUI_Window *win, NASUI_Button *button);
void NASUI_SetOnButtonClickHandler(NASUI_Button *button, NASUI_onButtonClick handler);
/*
* Called on button being clicked. Disabled buttons are ignored.
*/
//////////////////
// TextBox 2
typedef struct NASUI_TextBox NASUI_TextBox;
NASUI_TextBox * NASUI_CreateTextBox(int x, int y, int width, int height, const char *initial_text);
typedef void (*NASUI_onTextBoxKeyPress)(NASUI_Window *win, NASUI_TextBox *textbox);
void NASUI_SetOnTextBoxKeyPressHandler(NASUI_TextBox *textbox, NASUI_onTextBoxKeyPress handler);
//////////////////
// ListBox 3
typedef struct NASUI_ListBox NASUI_ListBox;
NASUI_ListBox * NASUI_CreateListBox(int x, int y, int width, int height);
void NASUI_AddListBoxItem(NASUI_ListBox *list, const char *text);
int NASUI_GetListBoxSelectedIndex(NASUI_ListBox *list);
char * NASUI_GetListBoxSelectedItem(NASUI_ListBox *list);
// Don't forget to wash ha... free memory
typedef void (*NASUI_onListBoxSelect)(NASUI_Window *win, NASUI_ListBox *list);
void NASUI_SetOnListBoxSelectHandler(NASUI_ListBox *list, NASUI_onListBoxSelect handler);
//////////////////
// ScrollBar 4
typedef struct NASUI_ScrollBar NASUI_ScrollBar;
NASUI_ScrollBar * NASUI_CreateScrollBar(int x, int y, int width, int height, int orientation, int max, int step);
#define NASUI_SCROLLBAR_VERTICAL 0
#define NASUI_SCROLLBAR_HORIZONTAL 1
typedef void (*NASUI_onScrollBarScroll)(NASUI_Window *win, NASUI_ScrollBar *bar);
void NASUI_SetOnScrollBarScrollHandler(NASUI_ScrollBar *bar, NASUI_onScrollBarScroll handler);
#endif
Actual implementations:
nasui.c
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include "nasui.h"
#include "nasui_ctrls.h"
#include "nasui_incaps.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Hidden callback functions
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wpar, LPARAM lpar)
// LOWORD(wpar) == control ID
//printf("MESSAGE %dn",msg);
//printf("WLOW:%dnWHI:%dnLLOW:%dnLHI:%dn", LOWORD(wpar), HIWORD(wpar), LOWORD(lpar), HIWORD(wpar));
// 309 - key down
// 273 - key up
// 308 - list selection
// HIWORD(wpar)1024 - textbox key down
// HIWORD(wpar)768 - textbox key up
switch(msg)
case WM_CREATE:
CREATESTRUCT *crts = (CREATESTRUCT*)lpar;
NASUI_Window *winptr = (NASUI_Window*)crts->lpCreateParams;
winptr->hwnd = hwnd;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG)winptr); // storing pointer to Window structure
if(winptr->events.on_create)
winptr->events.on_create(winptr);
break;
case WM_CLOSE:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
int close = 1;
if(winptr->events.on_close)
close = winptr->events.on_close(winptr);
if(close)
DestroyWindow(hwnd);
break;
case WM_DESTROY:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
if(winptr->events.on_destroy)
winptr->events.on_destroy(winptr);
PostQuitMessage(0);
break;
case 273:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
//printf("%d ::: ", HIWORD(wpar));
if(winptr->controls[LOWORD(wpar)]->type == 1
&& HIWORD(wpar) == 0)
NASUI_Button *tctrl = (NASUI_Button*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_buttonclick)
tctrl->on_buttonclick(winptr, tctrl);
else if(winptr->controls[LOWORD(wpar)]->type == 2
&& HIWORD(wpar) == 768)
NASUI_TextBox *tctrl = (NASUI_TextBox*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_textboxkeypress)
tctrl->on_textboxkeypress(winptr, tctrl);
else if(winptr->controls[LOWORD(wpar)]->type == 3
&& HIWORD(wpar) == 1)
NASUI_ListBox *tctrl = (NASUI_ListBox*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_select)
tctrl->on_select(winptr, tctrl);
break;
// 217
// lptr hi previous
// wptr hi next
case WM_VSCROLL:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
//printf("%x:::%xn",LOWORD(wpar), SB_THUMBPOSITION);
if(winptr->controls[LOWORD(wpar)]->type == 4)
NASUI_ScrollBar *tctrl = (NASUI_ScrollBar*)winptr->controls[LOWORD(wpar)];
SCROLLINFO snfo; memset(&snfo, 0, sizeof(LPSCROLLINFO));
snfo.cbSize = sizeof(SCROLLINFO); snfo.fMask = SIF_ALL;
GetScrollInfo(tctrl->hwnd, SB_CTL, &snfo);
printf("pos: %dn",snfo.nPos);
switch(LOWORD(wpar))
case SB_LINEDOWN: snfo.nPos -= tctrl->step; break;
case SB_LINEUP: snfo.nPos += tctrl->step; break;
case 5: snfo.nTrackPos = HIWORD(wpar); break;
default:break;
printf("pos2: %dn",snfo.nPos);
snfo.fMask = SIF_POS;
SetScrollInfo(tctrl->hwnd, SB_CTL, &snfo, TRUE);
break;
default:
return DefWindowProc(hwnd, msg, wpar, lpar);
return 0;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Library public functions
NASUI_Window * NASUI_CreateWindow(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow,
const char *title, int x, int y, int width, int height)
NASUI_Window *win = malloc(sizeof(NASUI_Window));
memset(win, 0, sizeof(NASUI_Window));
win->win_class.cbSize = sizeof(WNDCLASSEX);
win->win_class.style = 0;
win->win_class.lpfnWndProc = WndProc;
win->win_class.cbClsExtra = 0;
win->win_class.cbWndExtra = 0;
win->win_class.hInstance = hInst;
win->win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
win->win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
win->win_class.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
win->win_class.lpszMenuName = NULL;
win->win_class.lpszClassName = title;
win->win_class.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
win->hInst = hInst;
win->nCmdShow = nCmdShow;
win->title = title;
win->x = x; win->y = y;
win->w = width; win->h = height;
win->lastid = 1;
win->controls = malloc(sizeof(NASUI_Control*));
win->controls[0] = NULL;
if(!RegisterClassEx(&win->win_class))
free(win);
return NULL;
return win;
void NASUI_SetOnCreateHandler(NASUI_Window *win, NASUI_onCreate handler)win->events.on_create = handler;
void NASUI_SetOnCloseHandler(NASUI_Window *win, NASUI_onClose handler)win->events.on_close = handler;
void NASUI_SetOnDestroyHandler(NASUI_Window *win, NASUI_onDestroy handler)win->events.on_destroy = handler;
void NASUI_Run(NASUI_Window *win)
WS_MINIMIZEBOX
nasui_ctrls.c
#include "stdio.h"
#include <windows.h>
#include "nasui_ctrls.h"
#include "nasui_incaps.h"
/////////////////////////////////
//
// Button
NASUI_Button * NASUI_CreateButton(int x, int y, int width, int height, const char *text)
NASUI_Button *butn = malloc(sizeof(NASUI_Button));
butn->type = 1;
butn->hwnd = 0;
butn->x = x; butn->y = y;
butn->w = width; butn->h = height;
butn->text = text;
butn->on_buttonclick = NULL;
return butn;
void NASUI_SetOnButtonClickHandler(NASUI_Button *button, NASUI_onButtonClick handler) button->on_buttonclick = handler;
/////////////////////////////////
//
// TextBox
NASUI_TextBox * NASUI_CreateTextBox(int x, int y, int width, int height, const char *initial_text)
NASUI_TextBox *box = malloc(sizeof(NASUI_TextBox));
box->type = 2;
box->hwnd = 0;
box->x = x; box->y = y;
box->w = width; box->h = height;
box->itext = initial_text;
box->on_textboxkeypress = NULL;
return box;
void NASUI_SetOnTextBoxKeyPressHandler(NASUI_TextBox *textbox, NASUI_onTextBoxKeyPress handler) textbox->on_textboxkeypress = handler;
/////////////////////////////////
//
// ListBox
NASUI_ListBox * NASUI_CreateListBox(int x, int y, int width, int height)
NASUI_ListBox *list = malloc(sizeof(NASUI_ListBox));
list->type = 3;
list->hwnd = 0;
list->x = x; list->y = y;
list->w = width; list->h = height;
list->on_select = NULL;
return list;
void NASUI_AddListBoxItem(NASUI_ListBox *list, const char *text)
SendMessage(list->hwnd, LB_ADDSTRING, 0, (LPARAM)text);
int NASUI_GetListBoxSelectedIndex(NASUI_ListBox *list)
return SendMessage(list->hwnd, LB_GETCURSEL, 0, 0);
char * NASUI_GetListBoxSelectedItem(NASUI_ListBox *list)
int indx = SendMessage(list->hwnd, LB_GETCURSEL, 0, 0);
int ln = SendMessage(list->hwnd, LB_GETTEXTLEN, (WPARAM) indx, 0);
char *ret = malloc((ln+1)*sizeof(char));
SendMessage(list->hwnd, LB_GETTEXT, indx, (WPARAM)ret);
return ret;
void NASUI_SetOnListBoxSelectHandler(NASUI_ListBox *list, NASUI_onListBoxSelect handler) list->on_select = handler;
/////////////////////////////////
//
// ScrollBar
NASUI_ScrollBar * NASUI_CreateScrollBar(int x, int y, int width, int height, int orientation, int max, int step)
NASUI_ScrollBar *bar = malloc(sizeof(NASUI_ScrollBar));
bar->type = 4;
bar->hwnd = 0;
bar->x = x; bar->y = y;
bar->w = width; bar->h = height;
bar->orient = orientation; bar->max = max; bar->step = step;
bar->on_scroll = NULL;
return bar;
void NASUI_SetOnScrollBarScrollHandler(NASUI_ScrollBar *bar, NASUI_onScrollBarScroll handler) bar->on_scroll = handler;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Direct interaction with WIN32 API
void NASUI_AddControl(NASUI_Window *win, NASUI_Control *ctrl)
switch(ctrl->type) // creating HWND right now
case 1:
WS_CHILD,
tctrl->x, tctrl->y,
tctrl->w, tctrl->h,
win->hwnd,
(HMENU)win->lastid,
GetModuleHandle(NULL),
NULL);
break;
case 2:
NASUI_TextBox *tctrl = (NASUI_TextBox*)ctrl;
tctrl->hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"EDIT",
tctrl->itext,
WS_VISIBLE
case 3:
WS_CHILD
case 4:
NASUI_ScrollBar *tctrl = (NASUI_ScrollBar*)ctrl;
tctrl->hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"SCROLLBAR",
"",
WS_VISIBLE
ctrl->ID = win->lastid;
++win->lastid;
win->controls = realloc(win->controls, sizeof(NASUI_Control*) * win->lastid);
win->controls[win->lastid-1] = ctrl;
nasui_incaps.h
#ifndef NASUI_INCAPS_H
#define NASUI_INCAPS_H
/*
* CAUTION: this things mean to be incapsulated
* and they strongly depend on other NASUI components.
* This file was created only to simplify compilation
* and increase code re-usage.
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// nasui.c
struct NASUI_WindowEvents // private
NASUI_onCreate on_create;
NASUI_onClose on_close;
NASUI_onDestroy on_destroy;
;
struct NASUI_Window // private
// NASUI part
NASUI_WindowEvents events;
const char *title;
int x, y;
int w, h;
// WINAPI part
HINSTANCE hInst;
int nCmdShow;
WNDCLASSEX win_class;
HWND hwnd;
int lastid; // last avaible ID, can be assigned to a control
NASUI_Control **controls; // table of controls, each entry's ID is it's index in array
;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// nasui_ctrls.c
struct NASUI_Control
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
;
struct NASUI_Button
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
const char *text;
NASUI_onButtonClick on_buttonclick;
;
struct NASUI_TextBox
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
const char *itext;
NASUI_onTextBoxKeyPress on_textboxkeypress;
;
struct NASUI_ListBox
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
NASUI_onListBoxSelect on_select;
;
struct NASUI_ScrollBar
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
int orient; int max; int step;
NASUI_onScrollBarScroll on_scroll;
;
#endif
c windows winapi
add a comment |Â
up vote
2
down vote
favorite
This is a piece of code using this wrapper to create a simple window and handle some of it's events in Windows environment.
#include "nasui.h"
#include "nasui_ctrls.h"
void ButClick(NASUI_Window *win, NASUI_Button *butn);
void BoxPress(NASUI_Window *win, NASUI_TextBox *tbox);
void ListSel(NASUI_Window *win, NASUI_ListBox *list);
void ScrollScrollScroll(NASUI_Window *win, NASUI_ScrollBar *bar);
void WinCreate(NASUI_Window *win);
int WinClose(NASUI_Window *win);
void WinDestroy(NASUI_Window *win);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow)
NASUI_Window *mywin = NASUI_CreateWindow(hInst, hPrevInst, cmdargs, nCmdShow,
"NASUI", 0, 0, 640, 480);
NASUI_SetOnCreateHandler(mywin, WinCreate);
NASUI_SetOnCloseHandler(mywin, WinClose);
NASUI_SetOnDestroyHandler(mywin, WinDestroy);
NASUI_Run(mywin);
void ListSel(NASUI_Window *win, NASUI_ListBox *list)
char *tofr = NASUI_GetListBoxSelectedItem(list);
printf("selected %sn", tofr);
free(tofr);
void ScrollScrollScroll(NASUI_Window *win, NASUI_ScrollBar *bar)
printf("scroll scroll scrolln");
void WinCreate(NASUI_Window *win)
printf("onCreate()n");
NASUI_Button *butex = NASUI_CreateButton(5, 5, 100, 25, "a button");
NASUI_AddControl(win, (NASUI_Control*)butex);
NASUI_TextBox *tbex = NASUI_CreateTextBox(5, 30, 100, 25, "enter something");
NASUI_AddControl(win, (NASUI_Control*)tbex);
NASUI_ListBox *lst = NASUI_CreateListBox(5, 55, 200, 200);
NASUI_SetOnListBoxSelectHandler(lst, ListSel);
NASUI_AddControl(win, (NASUI_Control*)lst);
NASUI_AddListBoxItem(lst, "THIS");
NASUI_AddListBoxItem(lst, "USEFUL");
NASUI_AddListBoxItem(lst, "ANSI");
NASUI_AddListBoxItem(lst, "àýõúþûÃÂúøüø úþôøÃÂþòúðüø");
NASUI_ScrollBar *scbar = NASUI_CreateScrollBar(205, 55, 25, 200, NASUI_SCROLLBAR_VERTICAL, 100, 5);
NASUI_SetOnScrollBarScrollHandler(scbar, ScrollScrollScroll);
NASUI_AddControl(win, (NASUI_Control*)scbar);
int WinClose(NASUI_Window *win)
printf("onClose()n"); return 1;
void WinDestroy(NASUI_Window *win)
printf("onDestroy()n");
Header files:
nasui.h
#ifndef NASUI_H
#define NASUI_H
#include <windows.h>
typedef struct NASUI_Window NASUI_Window;
typedef struct NASUI_WindowEvents NASUI_WindowEvents;
// WindowEvents possible function pointers
typedef void (*NASUI_onCreate)(NASUI_Window *win);
/*
* Called on window creation. The window aren't even shown now, either no controls are attached.
*/
typedef int (*NASUI_onClose)(NASUI_Window *win);
/*
* Called on user attempt to close the window. Can be cancelled by returning zero.
*/
typedef void (*NASUI_onDestroy)(NASUI_Window *win);
/*
* Called on the window destruction. Can't be cancelled.
*/
NASUI_Window * NASUI_CreateWindow(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow,
const char *title, int x, int y, int width, int height);
/*
* First four arguments are fetched from WinMain() and should not be modified.
* This function prepares Window structure to create a win32 window
*/
void NASUI_SetOnCreateHandler(NASUI_Window *win, NASUI_onCreate handler);
void NASUI_SetOnCloseHandler(NASUI_Window *win, NASUI_onClose handler);
void NASUI_SetOnDestroyHandler(NASUI_Window *win, NASUI_onDestroy handler);
void NASUI_Run(NASUI_Window *win);
/*
* This function creates actual window and enters an infinitive loop for fetching and
* handling Windows OS messages sent to the window.
* CAUTION: use only when all preparations are done or moved to another thread!!!
*/
#endif
nasui_ctrls.h
#ifndef NASUI_CTRLS_H
#define NASUI_CTRLS_H
#include "nasui.h"
typedef struct NASUI_Control NASUI_Control;
void NASUI_AddControl(NASUI_Window *win, NASUI_Control *ctrl);
/*
* Generic polymorhpic structure.
* Contains generic information about control's coordinates, size, handler and type.
* It's children are declared below.
*/
//////////////////
// Button 1
typedef struct NASUI_Button NASUI_Button;
NASUI_Button * NASUI_CreateButton(int x, int y, int width, int height, const char *text);
typedef void (*NASUI_onButtonClick)(NASUI_Window *win, NASUI_Button *button);
void NASUI_SetOnButtonClickHandler(NASUI_Button *button, NASUI_onButtonClick handler);
/*
* Called on button being clicked. Disabled buttons are ignored.
*/
//////////////////
// TextBox 2
typedef struct NASUI_TextBox NASUI_TextBox;
NASUI_TextBox * NASUI_CreateTextBox(int x, int y, int width, int height, const char *initial_text);
typedef void (*NASUI_onTextBoxKeyPress)(NASUI_Window *win, NASUI_TextBox *textbox);
void NASUI_SetOnTextBoxKeyPressHandler(NASUI_TextBox *textbox, NASUI_onTextBoxKeyPress handler);
//////////////////
// ListBox 3
typedef struct NASUI_ListBox NASUI_ListBox;
NASUI_ListBox * NASUI_CreateListBox(int x, int y, int width, int height);
void NASUI_AddListBoxItem(NASUI_ListBox *list, const char *text);
int NASUI_GetListBoxSelectedIndex(NASUI_ListBox *list);
char * NASUI_GetListBoxSelectedItem(NASUI_ListBox *list);
// Don't forget to wash ha... free memory
typedef void (*NASUI_onListBoxSelect)(NASUI_Window *win, NASUI_ListBox *list);
void NASUI_SetOnListBoxSelectHandler(NASUI_ListBox *list, NASUI_onListBoxSelect handler);
//////////////////
// ScrollBar 4
typedef struct NASUI_ScrollBar NASUI_ScrollBar;
NASUI_ScrollBar * NASUI_CreateScrollBar(int x, int y, int width, int height, int orientation, int max, int step);
#define NASUI_SCROLLBAR_VERTICAL 0
#define NASUI_SCROLLBAR_HORIZONTAL 1
typedef void (*NASUI_onScrollBarScroll)(NASUI_Window *win, NASUI_ScrollBar *bar);
void NASUI_SetOnScrollBarScrollHandler(NASUI_ScrollBar *bar, NASUI_onScrollBarScroll handler);
#endif
Actual implementations:
nasui.c
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include "nasui.h"
#include "nasui_ctrls.h"
#include "nasui_incaps.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Hidden callback functions
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wpar, LPARAM lpar)
// LOWORD(wpar) == control ID
//printf("MESSAGE %dn",msg);
//printf("WLOW:%dnWHI:%dnLLOW:%dnLHI:%dn", LOWORD(wpar), HIWORD(wpar), LOWORD(lpar), HIWORD(wpar));
// 309 - key down
// 273 - key up
// 308 - list selection
// HIWORD(wpar)1024 - textbox key down
// HIWORD(wpar)768 - textbox key up
switch(msg)
case WM_CREATE:
CREATESTRUCT *crts = (CREATESTRUCT*)lpar;
NASUI_Window *winptr = (NASUI_Window*)crts->lpCreateParams;
winptr->hwnd = hwnd;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG)winptr); // storing pointer to Window structure
if(winptr->events.on_create)
winptr->events.on_create(winptr);
break;
case WM_CLOSE:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
int close = 1;
if(winptr->events.on_close)
close = winptr->events.on_close(winptr);
if(close)
DestroyWindow(hwnd);
break;
case WM_DESTROY:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
if(winptr->events.on_destroy)
winptr->events.on_destroy(winptr);
PostQuitMessage(0);
break;
case 273:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
//printf("%d ::: ", HIWORD(wpar));
if(winptr->controls[LOWORD(wpar)]->type == 1
&& HIWORD(wpar) == 0)
NASUI_Button *tctrl = (NASUI_Button*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_buttonclick)
tctrl->on_buttonclick(winptr, tctrl);
else if(winptr->controls[LOWORD(wpar)]->type == 2
&& HIWORD(wpar) == 768)
NASUI_TextBox *tctrl = (NASUI_TextBox*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_textboxkeypress)
tctrl->on_textboxkeypress(winptr, tctrl);
else if(winptr->controls[LOWORD(wpar)]->type == 3
&& HIWORD(wpar) == 1)
NASUI_ListBox *tctrl = (NASUI_ListBox*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_select)
tctrl->on_select(winptr, tctrl);
break;
// 217
// lptr hi previous
// wptr hi next
case WM_VSCROLL:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
//printf("%x:::%xn",LOWORD(wpar), SB_THUMBPOSITION);
if(winptr->controls[LOWORD(wpar)]->type == 4)
NASUI_ScrollBar *tctrl = (NASUI_ScrollBar*)winptr->controls[LOWORD(wpar)];
SCROLLINFO snfo; memset(&snfo, 0, sizeof(LPSCROLLINFO));
snfo.cbSize = sizeof(SCROLLINFO); snfo.fMask = SIF_ALL;
GetScrollInfo(tctrl->hwnd, SB_CTL, &snfo);
printf("pos: %dn",snfo.nPos);
switch(LOWORD(wpar))
case SB_LINEDOWN: snfo.nPos -= tctrl->step; break;
case SB_LINEUP: snfo.nPos += tctrl->step; break;
case 5: snfo.nTrackPos = HIWORD(wpar); break;
default:break;
printf("pos2: %dn",snfo.nPos);
snfo.fMask = SIF_POS;
SetScrollInfo(tctrl->hwnd, SB_CTL, &snfo, TRUE);
break;
default:
return DefWindowProc(hwnd, msg, wpar, lpar);
return 0;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Library public functions
NASUI_Window * NASUI_CreateWindow(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow,
const char *title, int x, int y, int width, int height)
NASUI_Window *win = malloc(sizeof(NASUI_Window));
memset(win, 0, sizeof(NASUI_Window));
win->win_class.cbSize = sizeof(WNDCLASSEX);
win->win_class.style = 0;
win->win_class.lpfnWndProc = WndProc;
win->win_class.cbClsExtra = 0;
win->win_class.cbWndExtra = 0;
win->win_class.hInstance = hInst;
win->win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
win->win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
win->win_class.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
win->win_class.lpszMenuName = NULL;
win->win_class.lpszClassName = title;
win->win_class.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
win->hInst = hInst;
win->nCmdShow = nCmdShow;
win->title = title;
win->x = x; win->y = y;
win->w = width; win->h = height;
win->lastid = 1;
win->controls = malloc(sizeof(NASUI_Control*));
win->controls[0] = NULL;
if(!RegisterClassEx(&win->win_class))
free(win);
return NULL;
return win;
void NASUI_SetOnCreateHandler(NASUI_Window *win, NASUI_onCreate handler)win->events.on_create = handler;
void NASUI_SetOnCloseHandler(NASUI_Window *win, NASUI_onClose handler)win->events.on_close = handler;
void NASUI_SetOnDestroyHandler(NASUI_Window *win, NASUI_onDestroy handler)win->events.on_destroy = handler;
void NASUI_Run(NASUI_Window *win)
WS_MINIMIZEBOX
nasui_ctrls.c
#include "stdio.h"
#include <windows.h>
#include "nasui_ctrls.h"
#include "nasui_incaps.h"
/////////////////////////////////
//
// Button
NASUI_Button * NASUI_CreateButton(int x, int y, int width, int height, const char *text)
NASUI_Button *butn = malloc(sizeof(NASUI_Button));
butn->type = 1;
butn->hwnd = 0;
butn->x = x; butn->y = y;
butn->w = width; butn->h = height;
butn->text = text;
butn->on_buttonclick = NULL;
return butn;
void NASUI_SetOnButtonClickHandler(NASUI_Button *button, NASUI_onButtonClick handler) button->on_buttonclick = handler;
/////////////////////////////////
//
// TextBox
NASUI_TextBox * NASUI_CreateTextBox(int x, int y, int width, int height, const char *initial_text)
NASUI_TextBox *box = malloc(sizeof(NASUI_TextBox));
box->type = 2;
box->hwnd = 0;
box->x = x; box->y = y;
box->w = width; box->h = height;
box->itext = initial_text;
box->on_textboxkeypress = NULL;
return box;
void NASUI_SetOnTextBoxKeyPressHandler(NASUI_TextBox *textbox, NASUI_onTextBoxKeyPress handler) textbox->on_textboxkeypress = handler;
/////////////////////////////////
//
// ListBox
NASUI_ListBox * NASUI_CreateListBox(int x, int y, int width, int height)
NASUI_ListBox *list = malloc(sizeof(NASUI_ListBox));
list->type = 3;
list->hwnd = 0;
list->x = x; list->y = y;
list->w = width; list->h = height;
list->on_select = NULL;
return list;
void NASUI_AddListBoxItem(NASUI_ListBox *list, const char *text)
SendMessage(list->hwnd, LB_ADDSTRING, 0, (LPARAM)text);
int NASUI_GetListBoxSelectedIndex(NASUI_ListBox *list)
return SendMessage(list->hwnd, LB_GETCURSEL, 0, 0);
char * NASUI_GetListBoxSelectedItem(NASUI_ListBox *list)
int indx = SendMessage(list->hwnd, LB_GETCURSEL, 0, 0);
int ln = SendMessage(list->hwnd, LB_GETTEXTLEN, (WPARAM) indx, 0);
char *ret = malloc((ln+1)*sizeof(char));
SendMessage(list->hwnd, LB_GETTEXT, indx, (WPARAM)ret);
return ret;
void NASUI_SetOnListBoxSelectHandler(NASUI_ListBox *list, NASUI_onListBoxSelect handler) list->on_select = handler;
/////////////////////////////////
//
// ScrollBar
NASUI_ScrollBar * NASUI_CreateScrollBar(int x, int y, int width, int height, int orientation, int max, int step)
NASUI_ScrollBar *bar = malloc(sizeof(NASUI_ScrollBar));
bar->type = 4;
bar->hwnd = 0;
bar->x = x; bar->y = y;
bar->w = width; bar->h = height;
bar->orient = orientation; bar->max = max; bar->step = step;
bar->on_scroll = NULL;
return bar;
void NASUI_SetOnScrollBarScrollHandler(NASUI_ScrollBar *bar, NASUI_onScrollBarScroll handler) bar->on_scroll = handler;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Direct interaction with WIN32 API
void NASUI_AddControl(NASUI_Window *win, NASUI_Control *ctrl)
switch(ctrl->type) // creating HWND right now
case 1:
WS_CHILD,
tctrl->x, tctrl->y,
tctrl->w, tctrl->h,
win->hwnd,
(HMENU)win->lastid,
GetModuleHandle(NULL),
NULL);
break;
case 2:
NASUI_TextBox *tctrl = (NASUI_TextBox*)ctrl;
tctrl->hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"EDIT",
tctrl->itext,
WS_VISIBLE
case 3:
WS_CHILD
case 4:
NASUI_ScrollBar *tctrl = (NASUI_ScrollBar*)ctrl;
tctrl->hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"SCROLLBAR",
"",
WS_VISIBLE
ctrl->ID = win->lastid;
++win->lastid;
win->controls = realloc(win->controls, sizeof(NASUI_Control*) * win->lastid);
win->controls[win->lastid-1] = ctrl;
nasui_incaps.h
#ifndef NASUI_INCAPS_H
#define NASUI_INCAPS_H
/*
* CAUTION: this things mean to be incapsulated
* and they strongly depend on other NASUI components.
* This file was created only to simplify compilation
* and increase code re-usage.
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// nasui.c
struct NASUI_WindowEvents // private
NASUI_onCreate on_create;
NASUI_onClose on_close;
NASUI_onDestroy on_destroy;
;
struct NASUI_Window // private
// NASUI part
NASUI_WindowEvents events;
const char *title;
int x, y;
int w, h;
// WINAPI part
HINSTANCE hInst;
int nCmdShow;
WNDCLASSEX win_class;
HWND hwnd;
int lastid; // last avaible ID, can be assigned to a control
NASUI_Control **controls; // table of controls, each entry's ID is it's index in array
;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// nasui_ctrls.c
struct NASUI_Control
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
;
struct NASUI_Button
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
const char *text;
NASUI_onButtonClick on_buttonclick;
;
struct NASUI_TextBox
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
const char *itext;
NASUI_onTextBoxKeyPress on_textboxkeypress;
;
struct NASUI_ListBox
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
NASUI_onListBoxSelect on_select;
;
struct NASUI_ScrollBar
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
int orient; int max; int step;
NASUI_onScrollBarScroll on_scroll;
;
#endif
c windows winapi
1
Can you elaborate on what problem you're trying to solve here? Why would one use this wrapper instead of just directly using Win32? Also, could you post the definitions ofNASUI_Window
,NASUI_Button
,NASUI_TextBox
, etc.
â user1118321
Mar 20 at 2:15
Win32 API is very complicated and uncomfortable to use, it requires writing tons of code. Even simple window creation can take dozens of lines of code.
â SOCIOPATH
Mar 20 at 6:36
That's official "Hello world" guide: msdn.microsoft.com/en-us/library/bb384843.aspx
â SOCIOPATH
Mar 20 at 7:00
Where are the actual implementations of your wrapper functions?
â Martin R
Mar 20 at 8:41
is it enough now?
â SOCIOPATH
Mar 20 at 9:30
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This is a piece of code using this wrapper to create a simple window and handle some of it's events in Windows environment.
#include "nasui.h"
#include "nasui_ctrls.h"
void ButClick(NASUI_Window *win, NASUI_Button *butn);
void BoxPress(NASUI_Window *win, NASUI_TextBox *tbox);
void ListSel(NASUI_Window *win, NASUI_ListBox *list);
void ScrollScrollScroll(NASUI_Window *win, NASUI_ScrollBar *bar);
void WinCreate(NASUI_Window *win);
int WinClose(NASUI_Window *win);
void WinDestroy(NASUI_Window *win);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow)
NASUI_Window *mywin = NASUI_CreateWindow(hInst, hPrevInst, cmdargs, nCmdShow,
"NASUI", 0, 0, 640, 480);
NASUI_SetOnCreateHandler(mywin, WinCreate);
NASUI_SetOnCloseHandler(mywin, WinClose);
NASUI_SetOnDestroyHandler(mywin, WinDestroy);
NASUI_Run(mywin);
void ListSel(NASUI_Window *win, NASUI_ListBox *list)
char *tofr = NASUI_GetListBoxSelectedItem(list);
printf("selected %sn", tofr);
free(tofr);
void ScrollScrollScroll(NASUI_Window *win, NASUI_ScrollBar *bar)
printf("scroll scroll scrolln");
void WinCreate(NASUI_Window *win)
printf("onCreate()n");
NASUI_Button *butex = NASUI_CreateButton(5, 5, 100, 25, "a button");
NASUI_AddControl(win, (NASUI_Control*)butex);
NASUI_TextBox *tbex = NASUI_CreateTextBox(5, 30, 100, 25, "enter something");
NASUI_AddControl(win, (NASUI_Control*)tbex);
NASUI_ListBox *lst = NASUI_CreateListBox(5, 55, 200, 200);
NASUI_SetOnListBoxSelectHandler(lst, ListSel);
NASUI_AddControl(win, (NASUI_Control*)lst);
NASUI_AddListBoxItem(lst, "THIS");
NASUI_AddListBoxItem(lst, "USEFUL");
NASUI_AddListBoxItem(lst, "ANSI");
NASUI_AddListBoxItem(lst, "àýõúþûÃÂúøüø úþôøÃÂþòúðüø");
NASUI_ScrollBar *scbar = NASUI_CreateScrollBar(205, 55, 25, 200, NASUI_SCROLLBAR_VERTICAL, 100, 5);
NASUI_SetOnScrollBarScrollHandler(scbar, ScrollScrollScroll);
NASUI_AddControl(win, (NASUI_Control*)scbar);
int WinClose(NASUI_Window *win)
printf("onClose()n"); return 1;
void WinDestroy(NASUI_Window *win)
printf("onDestroy()n");
Header files:
nasui.h
#ifndef NASUI_H
#define NASUI_H
#include <windows.h>
typedef struct NASUI_Window NASUI_Window;
typedef struct NASUI_WindowEvents NASUI_WindowEvents;
// WindowEvents possible function pointers
typedef void (*NASUI_onCreate)(NASUI_Window *win);
/*
* Called on window creation. The window aren't even shown now, either no controls are attached.
*/
typedef int (*NASUI_onClose)(NASUI_Window *win);
/*
* Called on user attempt to close the window. Can be cancelled by returning zero.
*/
typedef void (*NASUI_onDestroy)(NASUI_Window *win);
/*
* Called on the window destruction. Can't be cancelled.
*/
NASUI_Window * NASUI_CreateWindow(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow,
const char *title, int x, int y, int width, int height);
/*
* First four arguments are fetched from WinMain() and should not be modified.
* This function prepares Window structure to create a win32 window
*/
void NASUI_SetOnCreateHandler(NASUI_Window *win, NASUI_onCreate handler);
void NASUI_SetOnCloseHandler(NASUI_Window *win, NASUI_onClose handler);
void NASUI_SetOnDestroyHandler(NASUI_Window *win, NASUI_onDestroy handler);
void NASUI_Run(NASUI_Window *win);
/*
* This function creates actual window and enters an infinitive loop for fetching and
* handling Windows OS messages sent to the window.
* CAUTION: use only when all preparations are done or moved to another thread!!!
*/
#endif
nasui_ctrls.h
#ifndef NASUI_CTRLS_H
#define NASUI_CTRLS_H
#include "nasui.h"
typedef struct NASUI_Control NASUI_Control;
void NASUI_AddControl(NASUI_Window *win, NASUI_Control *ctrl);
/*
* Generic polymorhpic structure.
* Contains generic information about control's coordinates, size, handler and type.
* It's children are declared below.
*/
//////////////////
// Button 1
typedef struct NASUI_Button NASUI_Button;
NASUI_Button * NASUI_CreateButton(int x, int y, int width, int height, const char *text);
typedef void (*NASUI_onButtonClick)(NASUI_Window *win, NASUI_Button *button);
void NASUI_SetOnButtonClickHandler(NASUI_Button *button, NASUI_onButtonClick handler);
/*
* Called on button being clicked. Disabled buttons are ignored.
*/
//////////////////
// TextBox 2
typedef struct NASUI_TextBox NASUI_TextBox;
NASUI_TextBox * NASUI_CreateTextBox(int x, int y, int width, int height, const char *initial_text);
typedef void (*NASUI_onTextBoxKeyPress)(NASUI_Window *win, NASUI_TextBox *textbox);
void NASUI_SetOnTextBoxKeyPressHandler(NASUI_TextBox *textbox, NASUI_onTextBoxKeyPress handler);
//////////////////
// ListBox 3
typedef struct NASUI_ListBox NASUI_ListBox;
NASUI_ListBox * NASUI_CreateListBox(int x, int y, int width, int height);
void NASUI_AddListBoxItem(NASUI_ListBox *list, const char *text);
int NASUI_GetListBoxSelectedIndex(NASUI_ListBox *list);
char * NASUI_GetListBoxSelectedItem(NASUI_ListBox *list);
// Don't forget to wash ha... free memory
typedef void (*NASUI_onListBoxSelect)(NASUI_Window *win, NASUI_ListBox *list);
void NASUI_SetOnListBoxSelectHandler(NASUI_ListBox *list, NASUI_onListBoxSelect handler);
//////////////////
// ScrollBar 4
typedef struct NASUI_ScrollBar NASUI_ScrollBar;
NASUI_ScrollBar * NASUI_CreateScrollBar(int x, int y, int width, int height, int orientation, int max, int step);
#define NASUI_SCROLLBAR_VERTICAL 0
#define NASUI_SCROLLBAR_HORIZONTAL 1
typedef void (*NASUI_onScrollBarScroll)(NASUI_Window *win, NASUI_ScrollBar *bar);
void NASUI_SetOnScrollBarScrollHandler(NASUI_ScrollBar *bar, NASUI_onScrollBarScroll handler);
#endif
Actual implementations:
nasui.c
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include "nasui.h"
#include "nasui_ctrls.h"
#include "nasui_incaps.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Hidden callback functions
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wpar, LPARAM lpar)
// LOWORD(wpar) == control ID
//printf("MESSAGE %dn",msg);
//printf("WLOW:%dnWHI:%dnLLOW:%dnLHI:%dn", LOWORD(wpar), HIWORD(wpar), LOWORD(lpar), HIWORD(wpar));
// 309 - key down
// 273 - key up
// 308 - list selection
// HIWORD(wpar)1024 - textbox key down
// HIWORD(wpar)768 - textbox key up
switch(msg)
case WM_CREATE:
CREATESTRUCT *crts = (CREATESTRUCT*)lpar;
NASUI_Window *winptr = (NASUI_Window*)crts->lpCreateParams;
winptr->hwnd = hwnd;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG)winptr); // storing pointer to Window structure
if(winptr->events.on_create)
winptr->events.on_create(winptr);
break;
case WM_CLOSE:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
int close = 1;
if(winptr->events.on_close)
close = winptr->events.on_close(winptr);
if(close)
DestroyWindow(hwnd);
break;
case WM_DESTROY:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
if(winptr->events.on_destroy)
winptr->events.on_destroy(winptr);
PostQuitMessage(0);
break;
case 273:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
//printf("%d ::: ", HIWORD(wpar));
if(winptr->controls[LOWORD(wpar)]->type == 1
&& HIWORD(wpar) == 0)
NASUI_Button *tctrl = (NASUI_Button*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_buttonclick)
tctrl->on_buttonclick(winptr, tctrl);
else if(winptr->controls[LOWORD(wpar)]->type == 2
&& HIWORD(wpar) == 768)
NASUI_TextBox *tctrl = (NASUI_TextBox*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_textboxkeypress)
tctrl->on_textboxkeypress(winptr, tctrl);
else if(winptr->controls[LOWORD(wpar)]->type == 3
&& HIWORD(wpar) == 1)
NASUI_ListBox *tctrl = (NASUI_ListBox*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_select)
tctrl->on_select(winptr, tctrl);
break;
// 217
// lptr hi previous
// wptr hi next
case WM_VSCROLL:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
//printf("%x:::%xn",LOWORD(wpar), SB_THUMBPOSITION);
if(winptr->controls[LOWORD(wpar)]->type == 4)
NASUI_ScrollBar *tctrl = (NASUI_ScrollBar*)winptr->controls[LOWORD(wpar)];
SCROLLINFO snfo; memset(&snfo, 0, sizeof(LPSCROLLINFO));
snfo.cbSize = sizeof(SCROLLINFO); snfo.fMask = SIF_ALL;
GetScrollInfo(tctrl->hwnd, SB_CTL, &snfo);
printf("pos: %dn",snfo.nPos);
switch(LOWORD(wpar))
case SB_LINEDOWN: snfo.nPos -= tctrl->step; break;
case SB_LINEUP: snfo.nPos += tctrl->step; break;
case 5: snfo.nTrackPos = HIWORD(wpar); break;
default:break;
printf("pos2: %dn",snfo.nPos);
snfo.fMask = SIF_POS;
SetScrollInfo(tctrl->hwnd, SB_CTL, &snfo, TRUE);
break;
default:
return DefWindowProc(hwnd, msg, wpar, lpar);
return 0;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Library public functions
NASUI_Window * NASUI_CreateWindow(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow,
const char *title, int x, int y, int width, int height)
NASUI_Window *win = malloc(sizeof(NASUI_Window));
memset(win, 0, sizeof(NASUI_Window));
win->win_class.cbSize = sizeof(WNDCLASSEX);
win->win_class.style = 0;
win->win_class.lpfnWndProc = WndProc;
win->win_class.cbClsExtra = 0;
win->win_class.cbWndExtra = 0;
win->win_class.hInstance = hInst;
win->win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
win->win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
win->win_class.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
win->win_class.lpszMenuName = NULL;
win->win_class.lpszClassName = title;
win->win_class.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
win->hInst = hInst;
win->nCmdShow = nCmdShow;
win->title = title;
win->x = x; win->y = y;
win->w = width; win->h = height;
win->lastid = 1;
win->controls = malloc(sizeof(NASUI_Control*));
win->controls[0] = NULL;
if(!RegisterClassEx(&win->win_class))
free(win);
return NULL;
return win;
void NASUI_SetOnCreateHandler(NASUI_Window *win, NASUI_onCreate handler)win->events.on_create = handler;
void NASUI_SetOnCloseHandler(NASUI_Window *win, NASUI_onClose handler)win->events.on_close = handler;
void NASUI_SetOnDestroyHandler(NASUI_Window *win, NASUI_onDestroy handler)win->events.on_destroy = handler;
void NASUI_Run(NASUI_Window *win)
WS_MINIMIZEBOX
nasui_ctrls.c
#include "stdio.h"
#include <windows.h>
#include "nasui_ctrls.h"
#include "nasui_incaps.h"
/////////////////////////////////
//
// Button
NASUI_Button * NASUI_CreateButton(int x, int y, int width, int height, const char *text)
NASUI_Button *butn = malloc(sizeof(NASUI_Button));
butn->type = 1;
butn->hwnd = 0;
butn->x = x; butn->y = y;
butn->w = width; butn->h = height;
butn->text = text;
butn->on_buttonclick = NULL;
return butn;
void NASUI_SetOnButtonClickHandler(NASUI_Button *button, NASUI_onButtonClick handler) button->on_buttonclick = handler;
/////////////////////////////////
//
// TextBox
NASUI_TextBox * NASUI_CreateTextBox(int x, int y, int width, int height, const char *initial_text)
NASUI_TextBox *box = malloc(sizeof(NASUI_TextBox));
box->type = 2;
box->hwnd = 0;
box->x = x; box->y = y;
box->w = width; box->h = height;
box->itext = initial_text;
box->on_textboxkeypress = NULL;
return box;
void NASUI_SetOnTextBoxKeyPressHandler(NASUI_TextBox *textbox, NASUI_onTextBoxKeyPress handler) textbox->on_textboxkeypress = handler;
/////////////////////////////////
//
// ListBox
NASUI_ListBox * NASUI_CreateListBox(int x, int y, int width, int height)
NASUI_ListBox *list = malloc(sizeof(NASUI_ListBox));
list->type = 3;
list->hwnd = 0;
list->x = x; list->y = y;
list->w = width; list->h = height;
list->on_select = NULL;
return list;
void NASUI_AddListBoxItem(NASUI_ListBox *list, const char *text)
SendMessage(list->hwnd, LB_ADDSTRING, 0, (LPARAM)text);
int NASUI_GetListBoxSelectedIndex(NASUI_ListBox *list)
return SendMessage(list->hwnd, LB_GETCURSEL, 0, 0);
char * NASUI_GetListBoxSelectedItem(NASUI_ListBox *list)
int indx = SendMessage(list->hwnd, LB_GETCURSEL, 0, 0);
int ln = SendMessage(list->hwnd, LB_GETTEXTLEN, (WPARAM) indx, 0);
char *ret = malloc((ln+1)*sizeof(char));
SendMessage(list->hwnd, LB_GETTEXT, indx, (WPARAM)ret);
return ret;
void NASUI_SetOnListBoxSelectHandler(NASUI_ListBox *list, NASUI_onListBoxSelect handler) list->on_select = handler;
/////////////////////////////////
//
// ScrollBar
NASUI_ScrollBar * NASUI_CreateScrollBar(int x, int y, int width, int height, int orientation, int max, int step)
NASUI_ScrollBar *bar = malloc(sizeof(NASUI_ScrollBar));
bar->type = 4;
bar->hwnd = 0;
bar->x = x; bar->y = y;
bar->w = width; bar->h = height;
bar->orient = orientation; bar->max = max; bar->step = step;
bar->on_scroll = NULL;
return bar;
void NASUI_SetOnScrollBarScrollHandler(NASUI_ScrollBar *bar, NASUI_onScrollBarScroll handler) bar->on_scroll = handler;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Direct interaction with WIN32 API
void NASUI_AddControl(NASUI_Window *win, NASUI_Control *ctrl)
switch(ctrl->type) // creating HWND right now
case 1:
WS_CHILD,
tctrl->x, tctrl->y,
tctrl->w, tctrl->h,
win->hwnd,
(HMENU)win->lastid,
GetModuleHandle(NULL),
NULL);
break;
case 2:
NASUI_TextBox *tctrl = (NASUI_TextBox*)ctrl;
tctrl->hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"EDIT",
tctrl->itext,
WS_VISIBLE
case 3:
WS_CHILD
case 4:
NASUI_ScrollBar *tctrl = (NASUI_ScrollBar*)ctrl;
tctrl->hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"SCROLLBAR",
"",
WS_VISIBLE
ctrl->ID = win->lastid;
++win->lastid;
win->controls = realloc(win->controls, sizeof(NASUI_Control*) * win->lastid);
win->controls[win->lastid-1] = ctrl;
nasui_incaps.h
#ifndef NASUI_INCAPS_H
#define NASUI_INCAPS_H
/*
* CAUTION: this things mean to be incapsulated
* and they strongly depend on other NASUI components.
* This file was created only to simplify compilation
* and increase code re-usage.
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// nasui.c
struct NASUI_WindowEvents // private
NASUI_onCreate on_create;
NASUI_onClose on_close;
NASUI_onDestroy on_destroy;
;
struct NASUI_Window // private
// NASUI part
NASUI_WindowEvents events;
const char *title;
int x, y;
int w, h;
// WINAPI part
HINSTANCE hInst;
int nCmdShow;
WNDCLASSEX win_class;
HWND hwnd;
int lastid; // last avaible ID, can be assigned to a control
NASUI_Control **controls; // table of controls, each entry's ID is it's index in array
;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// nasui_ctrls.c
struct NASUI_Control
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
;
struct NASUI_Button
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
const char *text;
NASUI_onButtonClick on_buttonclick;
;
struct NASUI_TextBox
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
const char *itext;
NASUI_onTextBoxKeyPress on_textboxkeypress;
;
struct NASUI_ListBox
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
NASUI_onListBoxSelect on_select;
;
struct NASUI_ScrollBar
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
int orient; int max; int step;
NASUI_onScrollBarScroll on_scroll;
;
#endif
c windows winapi
This is a piece of code using this wrapper to create a simple window and handle some of it's events in Windows environment.
#include "nasui.h"
#include "nasui_ctrls.h"
void ButClick(NASUI_Window *win, NASUI_Button *butn);
void BoxPress(NASUI_Window *win, NASUI_TextBox *tbox);
void ListSel(NASUI_Window *win, NASUI_ListBox *list);
void ScrollScrollScroll(NASUI_Window *win, NASUI_ScrollBar *bar);
void WinCreate(NASUI_Window *win);
int WinClose(NASUI_Window *win);
void WinDestroy(NASUI_Window *win);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow)
NASUI_Window *mywin = NASUI_CreateWindow(hInst, hPrevInst, cmdargs, nCmdShow,
"NASUI", 0, 0, 640, 480);
NASUI_SetOnCreateHandler(mywin, WinCreate);
NASUI_SetOnCloseHandler(mywin, WinClose);
NASUI_SetOnDestroyHandler(mywin, WinDestroy);
NASUI_Run(mywin);
void ListSel(NASUI_Window *win, NASUI_ListBox *list)
char *tofr = NASUI_GetListBoxSelectedItem(list);
printf("selected %sn", tofr);
free(tofr);
void ScrollScrollScroll(NASUI_Window *win, NASUI_ScrollBar *bar)
printf("scroll scroll scrolln");
void WinCreate(NASUI_Window *win)
printf("onCreate()n");
NASUI_Button *butex = NASUI_CreateButton(5, 5, 100, 25, "a button");
NASUI_AddControl(win, (NASUI_Control*)butex);
NASUI_TextBox *tbex = NASUI_CreateTextBox(5, 30, 100, 25, "enter something");
NASUI_AddControl(win, (NASUI_Control*)tbex);
NASUI_ListBox *lst = NASUI_CreateListBox(5, 55, 200, 200);
NASUI_SetOnListBoxSelectHandler(lst, ListSel);
NASUI_AddControl(win, (NASUI_Control*)lst);
NASUI_AddListBoxItem(lst, "THIS");
NASUI_AddListBoxItem(lst, "USEFUL");
NASUI_AddListBoxItem(lst, "ANSI");
NASUI_AddListBoxItem(lst, "àýõúþûÃÂúøüø úþôøÃÂþòúðüø");
NASUI_ScrollBar *scbar = NASUI_CreateScrollBar(205, 55, 25, 200, NASUI_SCROLLBAR_VERTICAL, 100, 5);
NASUI_SetOnScrollBarScrollHandler(scbar, ScrollScrollScroll);
NASUI_AddControl(win, (NASUI_Control*)scbar);
int WinClose(NASUI_Window *win)
printf("onClose()n"); return 1;
void WinDestroy(NASUI_Window *win)
printf("onDestroy()n");
Header files:
nasui.h
#ifndef NASUI_H
#define NASUI_H
#include <windows.h>
typedef struct NASUI_Window NASUI_Window;
typedef struct NASUI_WindowEvents NASUI_WindowEvents;
// WindowEvents possible function pointers
typedef void (*NASUI_onCreate)(NASUI_Window *win);
/*
* Called on window creation. The window aren't even shown now, either no controls are attached.
*/
typedef int (*NASUI_onClose)(NASUI_Window *win);
/*
* Called on user attempt to close the window. Can be cancelled by returning zero.
*/
typedef void (*NASUI_onDestroy)(NASUI_Window *win);
/*
* Called on the window destruction. Can't be cancelled.
*/
NASUI_Window * NASUI_CreateWindow(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow,
const char *title, int x, int y, int width, int height);
/*
* First four arguments are fetched from WinMain() and should not be modified.
* This function prepares Window structure to create a win32 window
*/
void NASUI_SetOnCreateHandler(NASUI_Window *win, NASUI_onCreate handler);
void NASUI_SetOnCloseHandler(NASUI_Window *win, NASUI_onClose handler);
void NASUI_SetOnDestroyHandler(NASUI_Window *win, NASUI_onDestroy handler);
void NASUI_Run(NASUI_Window *win);
/*
* This function creates actual window and enters an infinitive loop for fetching and
* handling Windows OS messages sent to the window.
* CAUTION: use only when all preparations are done or moved to another thread!!!
*/
#endif
nasui_ctrls.h
#ifndef NASUI_CTRLS_H
#define NASUI_CTRLS_H
#include "nasui.h"
typedef struct NASUI_Control NASUI_Control;
void NASUI_AddControl(NASUI_Window *win, NASUI_Control *ctrl);
/*
* Generic polymorhpic structure.
* Contains generic information about control's coordinates, size, handler and type.
* It's children are declared below.
*/
//////////////////
// Button 1
typedef struct NASUI_Button NASUI_Button;
NASUI_Button * NASUI_CreateButton(int x, int y, int width, int height, const char *text);
typedef void (*NASUI_onButtonClick)(NASUI_Window *win, NASUI_Button *button);
void NASUI_SetOnButtonClickHandler(NASUI_Button *button, NASUI_onButtonClick handler);
/*
* Called on button being clicked. Disabled buttons are ignored.
*/
//////////////////
// TextBox 2
typedef struct NASUI_TextBox NASUI_TextBox;
NASUI_TextBox * NASUI_CreateTextBox(int x, int y, int width, int height, const char *initial_text);
typedef void (*NASUI_onTextBoxKeyPress)(NASUI_Window *win, NASUI_TextBox *textbox);
void NASUI_SetOnTextBoxKeyPressHandler(NASUI_TextBox *textbox, NASUI_onTextBoxKeyPress handler);
//////////////////
// ListBox 3
typedef struct NASUI_ListBox NASUI_ListBox;
NASUI_ListBox * NASUI_CreateListBox(int x, int y, int width, int height);
void NASUI_AddListBoxItem(NASUI_ListBox *list, const char *text);
int NASUI_GetListBoxSelectedIndex(NASUI_ListBox *list);
char * NASUI_GetListBoxSelectedItem(NASUI_ListBox *list);
// Don't forget to wash ha... free memory
typedef void (*NASUI_onListBoxSelect)(NASUI_Window *win, NASUI_ListBox *list);
void NASUI_SetOnListBoxSelectHandler(NASUI_ListBox *list, NASUI_onListBoxSelect handler);
//////////////////
// ScrollBar 4
typedef struct NASUI_ScrollBar NASUI_ScrollBar;
NASUI_ScrollBar * NASUI_CreateScrollBar(int x, int y, int width, int height, int orientation, int max, int step);
#define NASUI_SCROLLBAR_VERTICAL 0
#define NASUI_SCROLLBAR_HORIZONTAL 1
typedef void (*NASUI_onScrollBarScroll)(NASUI_Window *win, NASUI_ScrollBar *bar);
void NASUI_SetOnScrollBarScrollHandler(NASUI_ScrollBar *bar, NASUI_onScrollBarScroll handler);
#endif
Actual implementations:
nasui.c
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include "nasui.h"
#include "nasui_ctrls.h"
#include "nasui_incaps.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Hidden callback functions
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wpar, LPARAM lpar)
// LOWORD(wpar) == control ID
//printf("MESSAGE %dn",msg);
//printf("WLOW:%dnWHI:%dnLLOW:%dnLHI:%dn", LOWORD(wpar), HIWORD(wpar), LOWORD(lpar), HIWORD(wpar));
// 309 - key down
// 273 - key up
// 308 - list selection
// HIWORD(wpar)1024 - textbox key down
// HIWORD(wpar)768 - textbox key up
switch(msg)
case WM_CREATE:
CREATESTRUCT *crts = (CREATESTRUCT*)lpar;
NASUI_Window *winptr = (NASUI_Window*)crts->lpCreateParams;
winptr->hwnd = hwnd;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG)winptr); // storing pointer to Window structure
if(winptr->events.on_create)
winptr->events.on_create(winptr);
break;
case WM_CLOSE:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
int close = 1;
if(winptr->events.on_close)
close = winptr->events.on_close(winptr);
if(close)
DestroyWindow(hwnd);
break;
case WM_DESTROY:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
if(winptr->events.on_destroy)
winptr->events.on_destroy(winptr);
PostQuitMessage(0);
break;
case 273:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
//printf("%d ::: ", HIWORD(wpar));
if(winptr->controls[LOWORD(wpar)]->type == 1
&& HIWORD(wpar) == 0)
NASUI_Button *tctrl = (NASUI_Button*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_buttonclick)
tctrl->on_buttonclick(winptr, tctrl);
else if(winptr->controls[LOWORD(wpar)]->type == 2
&& HIWORD(wpar) == 768)
NASUI_TextBox *tctrl = (NASUI_TextBox*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_textboxkeypress)
tctrl->on_textboxkeypress(winptr, tctrl);
else if(winptr->controls[LOWORD(wpar)]->type == 3
&& HIWORD(wpar) == 1)
NASUI_ListBox *tctrl = (NASUI_ListBox*)winptr->controls[LOWORD(wpar)];
if(tctrl->on_select)
tctrl->on_select(winptr, tctrl);
break;
// 217
// lptr hi previous
// wptr hi next
case WM_VSCROLL:
NASUI_Window *winptr = ((NASUI_Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA));
//printf("%x:::%xn",LOWORD(wpar), SB_THUMBPOSITION);
if(winptr->controls[LOWORD(wpar)]->type == 4)
NASUI_ScrollBar *tctrl = (NASUI_ScrollBar*)winptr->controls[LOWORD(wpar)];
SCROLLINFO snfo; memset(&snfo, 0, sizeof(LPSCROLLINFO));
snfo.cbSize = sizeof(SCROLLINFO); snfo.fMask = SIF_ALL;
GetScrollInfo(tctrl->hwnd, SB_CTL, &snfo);
printf("pos: %dn",snfo.nPos);
switch(LOWORD(wpar))
case SB_LINEDOWN: snfo.nPos -= tctrl->step; break;
case SB_LINEUP: snfo.nPos += tctrl->step; break;
case 5: snfo.nTrackPos = HIWORD(wpar); break;
default:break;
printf("pos2: %dn",snfo.nPos);
snfo.fMask = SIF_POS;
SetScrollInfo(tctrl->hwnd, SB_CTL, &snfo, TRUE);
break;
default:
return DefWindowProc(hwnd, msg, wpar, lpar);
return 0;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Library public functions
NASUI_Window * NASUI_CreateWindow(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdargs, int nCmdShow,
const char *title, int x, int y, int width, int height)
NASUI_Window *win = malloc(sizeof(NASUI_Window));
memset(win, 0, sizeof(NASUI_Window));
win->win_class.cbSize = sizeof(WNDCLASSEX);
win->win_class.style = 0;
win->win_class.lpfnWndProc = WndProc;
win->win_class.cbClsExtra = 0;
win->win_class.cbWndExtra = 0;
win->win_class.hInstance = hInst;
win->win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
win->win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
win->win_class.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
win->win_class.lpszMenuName = NULL;
win->win_class.lpszClassName = title;
win->win_class.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
win->hInst = hInst;
win->nCmdShow = nCmdShow;
win->title = title;
win->x = x; win->y = y;
win->w = width; win->h = height;
win->lastid = 1;
win->controls = malloc(sizeof(NASUI_Control*));
win->controls[0] = NULL;
if(!RegisterClassEx(&win->win_class))
free(win);
return NULL;
return win;
void NASUI_SetOnCreateHandler(NASUI_Window *win, NASUI_onCreate handler)win->events.on_create = handler;
void NASUI_SetOnCloseHandler(NASUI_Window *win, NASUI_onClose handler)win->events.on_close = handler;
void NASUI_SetOnDestroyHandler(NASUI_Window *win, NASUI_onDestroy handler)win->events.on_destroy = handler;
void NASUI_Run(NASUI_Window *win)
WS_MINIMIZEBOX
nasui_ctrls.c
#include "stdio.h"
#include <windows.h>
#include "nasui_ctrls.h"
#include "nasui_incaps.h"
/////////////////////////////////
//
// Button
NASUI_Button * NASUI_CreateButton(int x, int y, int width, int height, const char *text)
NASUI_Button *butn = malloc(sizeof(NASUI_Button));
butn->type = 1;
butn->hwnd = 0;
butn->x = x; butn->y = y;
butn->w = width; butn->h = height;
butn->text = text;
butn->on_buttonclick = NULL;
return butn;
void NASUI_SetOnButtonClickHandler(NASUI_Button *button, NASUI_onButtonClick handler) button->on_buttonclick = handler;
/////////////////////////////////
//
// TextBox
NASUI_TextBox * NASUI_CreateTextBox(int x, int y, int width, int height, const char *initial_text)
NASUI_TextBox *box = malloc(sizeof(NASUI_TextBox));
box->type = 2;
box->hwnd = 0;
box->x = x; box->y = y;
box->w = width; box->h = height;
box->itext = initial_text;
box->on_textboxkeypress = NULL;
return box;
void NASUI_SetOnTextBoxKeyPressHandler(NASUI_TextBox *textbox, NASUI_onTextBoxKeyPress handler) textbox->on_textboxkeypress = handler;
/////////////////////////////////
//
// ListBox
NASUI_ListBox * NASUI_CreateListBox(int x, int y, int width, int height)
NASUI_ListBox *list = malloc(sizeof(NASUI_ListBox));
list->type = 3;
list->hwnd = 0;
list->x = x; list->y = y;
list->w = width; list->h = height;
list->on_select = NULL;
return list;
void NASUI_AddListBoxItem(NASUI_ListBox *list, const char *text)
SendMessage(list->hwnd, LB_ADDSTRING, 0, (LPARAM)text);
int NASUI_GetListBoxSelectedIndex(NASUI_ListBox *list)
return SendMessage(list->hwnd, LB_GETCURSEL, 0, 0);
char * NASUI_GetListBoxSelectedItem(NASUI_ListBox *list)
int indx = SendMessage(list->hwnd, LB_GETCURSEL, 0, 0);
int ln = SendMessage(list->hwnd, LB_GETTEXTLEN, (WPARAM) indx, 0);
char *ret = malloc((ln+1)*sizeof(char));
SendMessage(list->hwnd, LB_GETTEXT, indx, (WPARAM)ret);
return ret;
void NASUI_SetOnListBoxSelectHandler(NASUI_ListBox *list, NASUI_onListBoxSelect handler) list->on_select = handler;
/////////////////////////////////
//
// ScrollBar
NASUI_ScrollBar * NASUI_CreateScrollBar(int x, int y, int width, int height, int orientation, int max, int step)
NASUI_ScrollBar *bar = malloc(sizeof(NASUI_ScrollBar));
bar->type = 4;
bar->hwnd = 0;
bar->x = x; bar->y = y;
bar->w = width; bar->h = height;
bar->orient = orientation; bar->max = max; bar->step = step;
bar->on_scroll = NULL;
return bar;
void NASUI_SetOnScrollBarScrollHandler(NASUI_ScrollBar *bar, NASUI_onScrollBarScroll handler) bar->on_scroll = handler;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Direct interaction with WIN32 API
void NASUI_AddControl(NASUI_Window *win, NASUI_Control *ctrl)
switch(ctrl->type) // creating HWND right now
case 1:
WS_CHILD,
tctrl->x, tctrl->y,
tctrl->w, tctrl->h,
win->hwnd,
(HMENU)win->lastid,
GetModuleHandle(NULL),
NULL);
break;
case 2:
NASUI_TextBox *tctrl = (NASUI_TextBox*)ctrl;
tctrl->hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"EDIT",
tctrl->itext,
WS_VISIBLE
case 3:
WS_CHILD
case 4:
NASUI_ScrollBar *tctrl = (NASUI_ScrollBar*)ctrl;
tctrl->hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"SCROLLBAR",
"",
WS_VISIBLE
ctrl->ID = win->lastid;
++win->lastid;
win->controls = realloc(win->controls, sizeof(NASUI_Control*) * win->lastid);
win->controls[win->lastid-1] = ctrl;
nasui_incaps.h
#ifndef NASUI_INCAPS_H
#define NASUI_INCAPS_H
/*
* CAUTION: this things mean to be incapsulated
* and they strongly depend on other NASUI components.
* This file was created only to simplify compilation
* and increase code re-usage.
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// nasui.c
struct NASUI_WindowEvents // private
NASUI_onCreate on_create;
NASUI_onClose on_close;
NASUI_onDestroy on_destroy;
;
struct NASUI_Window // private
// NASUI part
NASUI_WindowEvents events;
const char *title;
int x, y;
int w, h;
// WINAPI part
HINSTANCE hInst;
int nCmdShow;
WNDCLASSEX win_class;
HWND hwnd;
int lastid; // last avaible ID, can be assigned to a control
NASUI_Control **controls; // table of controls, each entry's ID is it's index in array
;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// nasui_ctrls.c
struct NASUI_Control
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
;
struct NASUI_Button
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
const char *text;
NASUI_onButtonClick on_buttonclick;
;
struct NASUI_TextBox
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
const char *itext;
NASUI_onTextBoxKeyPress on_textboxkeypress;
;
struct NASUI_ListBox
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
NASUI_onListBoxSelect on_select;
;
struct NASUI_ScrollBar
unsigned char type;
HWND hwnd;
int ID;
int x, y;
int w, h;
int orient; int max; int step;
NASUI_onScrollBarScroll on_scroll;
;
#endif
c windows winapi
edited Mar 20 at 9:28
asked Mar 19 at 23:01
SOCIOPATH
112
112
1
Can you elaborate on what problem you're trying to solve here? Why would one use this wrapper instead of just directly using Win32? Also, could you post the definitions ofNASUI_Window
,NASUI_Button
,NASUI_TextBox
, etc.
â user1118321
Mar 20 at 2:15
Win32 API is very complicated and uncomfortable to use, it requires writing tons of code. Even simple window creation can take dozens of lines of code.
â SOCIOPATH
Mar 20 at 6:36
That's official "Hello world" guide: msdn.microsoft.com/en-us/library/bb384843.aspx
â SOCIOPATH
Mar 20 at 7:00
Where are the actual implementations of your wrapper functions?
â Martin R
Mar 20 at 8:41
is it enough now?
â SOCIOPATH
Mar 20 at 9:30
add a comment |Â
1
Can you elaborate on what problem you're trying to solve here? Why would one use this wrapper instead of just directly using Win32? Also, could you post the definitions ofNASUI_Window
,NASUI_Button
,NASUI_TextBox
, etc.
â user1118321
Mar 20 at 2:15
Win32 API is very complicated and uncomfortable to use, it requires writing tons of code. Even simple window creation can take dozens of lines of code.
â SOCIOPATH
Mar 20 at 6:36
That's official "Hello world" guide: msdn.microsoft.com/en-us/library/bb384843.aspx
â SOCIOPATH
Mar 20 at 7:00
Where are the actual implementations of your wrapper functions?
â Martin R
Mar 20 at 8:41
is it enough now?
â SOCIOPATH
Mar 20 at 9:30
1
1
Can you elaborate on what problem you're trying to solve here? Why would one use this wrapper instead of just directly using Win32? Also, could you post the definitions of
NASUI_Window
, NASUI_Button
, NASUI_TextBox
, etc.â user1118321
Mar 20 at 2:15
Can you elaborate on what problem you're trying to solve here? Why would one use this wrapper instead of just directly using Win32? Also, could you post the definitions of
NASUI_Window
, NASUI_Button
, NASUI_TextBox
, etc.â user1118321
Mar 20 at 2:15
Win32 API is very complicated and uncomfortable to use, it requires writing tons of code. Even simple window creation can take dozens of lines of code.
â SOCIOPATH
Mar 20 at 6:36
Win32 API is very complicated and uncomfortable to use, it requires writing tons of code. Even simple window creation can take dozens of lines of code.
â SOCIOPATH
Mar 20 at 6:36
That's official "Hello world" guide: msdn.microsoft.com/en-us/library/bb384843.aspx
â SOCIOPATH
Mar 20 at 7:00
That's official "Hello world" guide: msdn.microsoft.com/en-us/library/bb384843.aspx
â SOCIOPATH
Mar 20 at 7:00
Where are the actual implementations of your wrapper functions?
â Martin R
Mar 20 at 8:41
Where are the actual implementations of your wrapper functions?
â Martin R
Mar 20 at 8:41
is it enough now?
â SOCIOPATH
Mar 20 at 9:30
is it enough now?
â SOCIOPATH
Mar 20 at 9:30
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189981%2fwinapi-32-wrapper-in-c%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Can you elaborate on what problem you're trying to solve here? Why would one use this wrapper instead of just directly using Win32? Also, could you post the definitions of
NASUI_Window
,NASUI_Button
,NASUI_TextBox
, etc.â user1118321
Mar 20 at 2:15
Win32 API is very complicated and uncomfortable to use, it requires writing tons of code. Even simple window creation can take dozens of lines of code.
â SOCIOPATH
Mar 20 at 6:36
That's official "Hello world" guide: msdn.microsoft.com/en-us/library/bb384843.aspx
â SOCIOPATH
Mar 20 at 7:00
Where are the actual implementations of your wrapper functions?
â Martin R
Mar 20 at 8:41
is it enough now?
â SOCIOPATH
Mar 20 at 9:30