Who likes comboboxes? lol

closed account (3pj6b7Xj)
So you want to learn how to make combo boxes? Here is a program that uses one and many other windows controls such as EDIT, buttons, etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// 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[i]);
            }   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.
}


Have fun, if you need the rest of the code, I can post it.
closed account (3pj6b7Xj)
The storing pointers in window memory stuff is very handy, I learned this from freedie1. Just have to give him credit as this has made my windows programming life so much easier :) Oh yeah, I wanted to throw some humor into this..the fat bear, etc lol or the RipeMelon Software, Inc. I was out of ideas but I hope I dont get in trouble, lol!
Last edited on
Topic archived. No new replies allowed.