Untuk menambahkan combobox, nama class Window yang kita gunakan dalam
CreateWindow atau CreateWindowEx adalah "COMBOBOX". Kita perlu menggunakan CBS_DROPDOWNLIST supaya tampilan dari ComboBox berbentuk Dropdown. Penggunaan ComboBox hampir sama dengan ListBox. Bedanya ada di pembuatan window dan penggunaan SendMessage. Untuk menambah isi combobox, kita bisa menggunakan SendMessage dengan CB_ADDSTRING sebagai
argumen dari parameter keduanya. Untuk menghapus isi listbox pada posisi
tertentu, gunakan SendMessage dengan CB_DELETESTRING. Contoh
penggunaan SendMessage lainnya untuk combobox bisa dilihat dibawah ini.
#include <windows.h>
#include <string.h>
#define nkolom 4
#define nbaris 5
HWND cbbox;
char stombol[24][5]={"AC", "C", "<=", "+", "1", "2", "3", "-", "4", "5", "6", "x", "7", "8", "9", "/", "+/-", ",", "0", "="};
LRESULT CALLBACK wndproc(
HWND hwnd,
UINT pesan,
WPARAM wParam,
LPARAM lParam
);
int __stdcall WinMain(HINSTANCE hinst, HINSTANCE hprev, char *cmdl, int cmds){
HWND wind;
MSG msg;
WNDCLASS wcls={0};
wcls.hInstance=hinst;
wcls.hbrBackground=GetSysColorBrush(COLOR_3DFACE);
wcls.lpszClassName=TEXT("windowku");
wcls.lpfnWndProc=wndproc;
wcls.hCursor=LoadCursor(0,IDC_ARROW);
RegisterClass(&wcls);
wind=CreateWindowEx(0, TEXT("windowku"), "Programku", WS_VISIBLE | WS_OVERLAPPEDWINDOW , 400, 200, 198, 290, 0, 0, hinst, 0);
cbbox=CreateWindowEx(0, TEXT("COMBOBOX"), "", WS_VISIBLE | WS_CHILD | WS_BORDER | CBS_DROPDOWNLIST, 4, 4, 170, 180, wind, 0, hinst, 0);
//Buat textbox, static text dan tombol
for(int i=0;i<10;i++){
SendMessage(cbbox, CB_ADDSTRING, 0, (LPARAM)stombol[i]);
}
SendMessage(cbbox, CB_SETCURSEL, 0, 0);
CreateWindowEx(0, TEXT("BUTTON"), "Tampilkan", WS_VISIBLE | WS_CHILD | WS_BORDER , 4, 40, 170, 20, wind, (HMENU)100, hinst, 0);
while(GetMessage(&msg, NULL, 0, 0)){
DispatchMessage(&msg);
TranslateMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK wndproc(HWND hwnd, UINT pesan, WPARAM wParam, LPARAM lParam){
int x=0;
char s[100];
switch(pesan){
case WM_COMMAND:
if(LOWORD(wParam)==100){
x=SendMessage(cbbox, CB_GETCURSEL, 0, 0);
if(SendMessage(cbbox, CB_GETCOUNT, 0, 0)>3){
SendMessage(cbbox, CB_GETLBTEXT, x, (LPARAM)s);
MessageBox(hwnd, s, s, 0);
}
}
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, pesan, wParam, lParam);
};
Windows message yang digunakan untuk combobox dalam SendMessage umumnya diawali dengan CB. Beberapa "window message" khusus yang bisa digunakan untuk combobox, yaitu :
#include <windows.h>
#include <string.h>
#define nkolom 4
#define nbaris 5
HWND cbbox;
char stombol[24][5]={"AC", "C", "<=", "+", "1", "2", "3", "-", "4", "5", "6", "x", "7", "8", "9", "/", "+/-", ",", "0", "="};
LRESULT CALLBACK wndproc(
HWND hwnd,
UINT pesan,
WPARAM wParam,
LPARAM lParam
);
int __stdcall WinMain(HINSTANCE hinst, HINSTANCE hprev, char *cmdl, int cmds){
HWND wind;
MSG msg;
WNDCLASS wcls={0};
wcls.hInstance=hinst;
wcls.hbrBackground=GetSysColorBrush(COLOR_3DFACE);
wcls.lpszClassName=TEXT("windowku");
wcls.lpfnWndProc=wndproc;
wcls.hCursor=LoadCursor(0,IDC_ARROW);
RegisterClass(&wcls);
wind=CreateWindowEx(0, TEXT("windowku"), "Programku", WS_VISIBLE | WS_OVERLAPPEDWINDOW , 400, 200, 198, 290, 0, 0, hinst, 0);
cbbox=CreateWindowEx(0, TEXT("COMBOBOX"), "", WS_VISIBLE | WS_CHILD | WS_BORDER | CBS_DROPDOWNLIST, 4, 4, 170, 180, wind, 0, hinst, 0);
//Buat textbox, static text dan tombol
for(int i=0;i<10;i++){
SendMessage(cbbox, CB_ADDSTRING, 0, (LPARAM)stombol[i]);
}
SendMessage(cbbox, CB_SETCURSEL, 0, 0);
CreateWindowEx(0, TEXT("BUTTON"), "Tampilkan", WS_VISIBLE | WS_CHILD | WS_BORDER , 4, 40, 170, 20, wind, (HMENU)100, hinst, 0);
while(GetMessage(&msg, NULL, 0, 0)){
DispatchMessage(&msg);
TranslateMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK wndproc(HWND hwnd, UINT pesan, WPARAM wParam, LPARAM lParam){
int x=0;
char s[100];
switch(pesan){
case WM_COMMAND:
if(LOWORD(wParam)==100){
x=SendMessage(cbbox, CB_GETCURSEL, 0, 0);
if(SendMessage(cbbox, CB_GETCOUNT, 0, 0)>3){
SendMessage(cbbox, CB_GETLBTEXT, x, (LPARAM)s);
MessageBox(hwnd, s, s, 0);
}
}
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, pesan, wParam, lParam);
};
Windows message yang digunakan untuk combobox dalam SendMessage umumnya diawali dengan CB. Beberapa "window message" khusus yang bisa digunakan untuk combobox, yaitu :
- CB_GETCOUNT : Mendapatkan jumlah isi dari combobox
- CB_GETCURSEL : Mendapatkan posisi yang dipilih dalam combobox
- CB_ADDSTRING : Menambah isi combobox
- CB_DELETESTRING : Menghapus isi comboboxdi posisi tertentu
- CB_RESETCONTENT : Menghapus semua isi combobox
- CB_SETCURSEL : menentukan posisi pilihan dalam combobox
- CB_GETLBTEXT: Mendapatkan isi comboboxdi posisi tertentu