- Messages
- 829
- Country

I am also I C++ programmer, thought I have been inactive with it for quite some time now, work, life issues, you know the do.
[TUTORIALS I've done for people at cplusplus.com]
A mock up of the NIBBLES game that was packaged with QBasic, we all remember QBasic? Wow man, the memories.
Good night folks.
[TUTORIALS I've done for people at cplusplus.com]
// ComboBox.cpp ; 1/17/2011
// By: Rafael Perez-Santana (mr.rafael.ps@gmail.com)
// This program demonstrates how to work with Combo Boxes, it is commented
// to serve as a tutorial. Do not worry about the hWindow.h & HdcOut.h; I
// created them to hide the window framework code. HdcOut just uses TextOut()
// and some GDI functions to set colors, fonts and text output that would
// otherwise be tedious to code into an application.
// RATED: PG-13, more on this later.
#include <windows.h>
#include "hWINDOW.h" // window framework.
#include "HdcOut.h"
// pointer offsets in window memory.
#define OffsetTxtBox 0
#define OffsetComboBox 4
// :: 8 bytes of window memory.
// control IDs
#define HBT_ADD 1001
#define HTEXTBOX 1002
#define HCOMBOBOX 1003
#define HBT_CLEAR 1004
// declare our window procedure.
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,int)
{
// create the window.
hWINDOW hWindow(hInstance,WinProc);
hWindow.Size(412,180);
hWindow.Caption("ComboBox Example");
hWindow.ExtraBytes(8); // we will store 2 pointers.
hWindow.Style(WS_EX_LAYERED); // don't worry about this.
hWindow.Present(); // display the window.
MSG Msg; // window messages are saved here.
// start the message pump.
while(GetMessage(&Msg,NULL,0,0)) // get it.
{
TranslateMessage(&Msg); // translate it.
DispatchMessage(&Msg); // pump it.
}
return 0; // end.
}
// this is our window procedure, called by DispatchMessage()
LRESULT CALLBACK WinProc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
switch (Message) // pumped messages, come out from here.
{
case WM_CREATE: // this message is called before window is displayed.
{
// create the text box control.
HWND hTextBox=CreateWindowEx
(WS_EX_CLIENTEDGE,"EDIT",NULL,
WS_CHILD,10,30,300,25,hWnd,(HMENU)HTEXTBOX,
GetModuleHandle(NULL),NULL);
// create the Add button.
HWND hBtAdd=CreateWindowEx
(NULL,"BUTTON","Add",
WS_CHILD|BS_PUSHBUTTON,320,26,80,30,hWnd,(HMENU)HBT_ADD,
GetModuleHandle(NULL),NULL);
// create the Clear button.
HWND hBtClear=CreateWindowEx
(NULL,"BUTTON","Clear",
WS_CHILD|BS_PUSHBUTTON,320,75,80,30,hWnd,(HMENU)HBT_CLEAR,
GetModuleHandle(NULL),NULL);
// create the combo box control.
HWND hComboBox=CreateWindowEx
(NULL,"COMBOBOX",NULL,WS_CHILD|CBS_DROPDOWNLIST,
10,80,300,300,hWnd,(HMENU)HCOMBOBOX,
GetModuleHandle(NULL),NULL);
// pre-load the combo box with some crazy stuff.
LPCSTR szcrazystuff[10]; // create an array of 10 strings.
szcrazystuff[0]="A bear ate a big fat man."; // poor man.
szcrazystuff[1]="I love peanut butter!"; // with jelly.
szcrazystuff[2]="It's peanut butter jelly time!";
szcrazystuff[3]="The brown dog ate a fat squirrel."; // ewww.
szcrazystuff[4]="RipeMelons, we got it! Huh!?"; // ok?
szcrazystuff[5]="Bomb blew up on East 38 St."; // glad I wasn't there.
szcrazystuff[6]="Bananas are delicious!";
szcrazystuff[7]="http://www.cplusplus.com"; // good site!
szcrazystuff[8]="Fluffy squishy kitten."; // awww.
szcrazystuff[9]="The fat man ate the big bear."; // poor bear.
for(int i=0;i<10;i++) // this loop loads text into the combo box.
{ // we send the combo box a message, it is that simple!
SendMessage(hComboBox,CB_ADDSTRING,0,(LPARAM)szcrazystuff);
} SendMessage(hComboBox,CB_SETCURSEL,(WPARAM)0,0); // make first item visible.
// store the control handles in window memory.
SetWindowLongPtr(hWnd,OffsetTxtBox,(long)hTextBox);
SetWindowLongPtr(hWnd,OffsetComboBox,(long)hComboBox);
// show the controls.
ShowWindow(hTextBox,SW_SHOW);
ShowWindow(hBtAdd,SW_SHOW);
ShowWindow(hComboBox,SW_SHOW);
ShowWindow(hBtClear,SW_SHOW);
break;
}
case WM_PAINT: // this message is called when the window must be painted.
{
PAINTSTRUCT ps; // our paint structure.
HDC hDC=BeginPaint(hWnd,&ps); // start painting!
HDCOUT HdcOut(hDC); // device-context output utility.
// what to paint inside the client area...
HdcOut.Txt(10,10,"Type text, then click Add!",RGB(0,0,255)); // what to do.
HdcOut.Txt(10,120,"RipeMelon Software, Inc.",RGB(155,128,128)); // wtf? lol.
HdcOut.Font("Arial",15); // melons look good in Arial 15 *cough*
HdcOut.Txt(30,140,"( . Y . ) © 2011",RGB(255,128,128)); // are those boobs?
// NOTE: still PG-13 but viewer discretion is advised, too late!
EndPaint(hWnd,&ps); // stop painting!
break;
}
case WM_COMMAND: // here we check for control events.
{
char chbuffer[128]; // temporary char buffer.
// retrieve control handles from window memory.
HWND hTextBox=(HWND)GetWindowLongPtr(hWnd,OffsetTxtBox);
HWND hComboBox=(HWND)GetWindowLongPtr(hWnd,OffsetComboBox);
if(LOWORD(wParam)==HBT_ADD) // the add button was clicked.
{
if(!GetWindowText(hTextBox,chbuffer,128)) // get string from text box.
{ // display a message if no string was entered.
MessageBox(hWnd,"Type something first, then click Add.",
"Information",MB_ICONINFORMATION);
break; // abort operation.
}
// if the first entry is null, clear the combo box.
if(!SendMessage(hComboBox,CB_GETLBTEXTLEN,(WPARAM)0,0))
SendMessage(hComboBox,CB_RESETCONTENT,0,0);
// append string from text box into the combo box!
SendMessage(hComboBox,CB_ADDSTRING,0,(LPARAM)chbuffer);
// now make the new added string visible.
int iCount=SendMessage(hComboBox,CB_GETCOUNT,0,0);
SendMessage(hComboBox,CB_SETCURSEL,(WPARAM)iCount-1,0); // last item.
// make combo box drop down so we can see the contents.
SendMessage(hComboBox,CB_SHOWDROPDOWN,(WPARAM)(BOOL)true,0);
SetWindowText(hTextBox,NULL); // clear the text box.
}
if(LOWORD(wParam)==HBT_CLEAR) // the clear button was clicked.
{
// remove everything inside the combo box.
SendMessage(hComboBox,CB_RESETCONTENT,0,0);
// send a null string to avoid drawing errors.
SendMessage(hComboBox,CB_ADDSTRING,0,(LPARAM)"");
}
break;
}
case WM_DESTROY: // this message is called when the application closes.
{
PostQuitMessage (0); // post a WM_QUIT message in queue.
break;
}
// pump non-trapped messages to default procedure.
default:return DefWindowProc(hWnd,Message,wParam,lParam);
}
return 0; // a value of 0 indicates a message was trapped.
}
A mock up of the NIBBLES game that was packaged with QBasic, we all remember QBasic? Wow man, the memories.
Good night folks.






