C++ Win32 列表框和滑块创建 Windows 应用程序

c++ win32 listbox and slider create windows application

本文关键字:创建 Windows 应用程序 Win32 列表 C++      更新时间:2023-10-16

我想在我的窗口中实现一个列表框和一个滑块。 我使用devcpp,而不是Visual Studio。

我一直在寻找一种方法来做到这一点,但我什么也没找到,除了像 msdn.microsoft.com 提供的理论内容。

我想要一个例子,实现列表框和滑块的最小代码。 这是更紧密和最有用的链接,但它仍然使用Visual Studio。 谢谢。

我找到了我想要的东西,似乎这是整个互联网上制作列表框的唯一示例代码。为了运行它:下载并打开devc ++(我使用(或任何其他类型的编译器,打开一个新的Windows应用程序项目(否则它将不起作用(,擦除任何默认代码,粘贴此代码并运行它。

这就是你帮助别人学习代码的方式。

http://www.dreamincode.net/forums/topic/291276-win32-listbox/

#include <Windows.h>

#define IDC_MAIN_BUTTON 101         // Button identifier

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HWND hWndListL;
HWND hWndListR;
HWND hWndButton;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
HWND hWnd;

//Settings All Window Class Variables
WNDCLASSEX WndClsEx;
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProcedure;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = "My Window";

//Register Window Class
RegisterClassEx(&WndClsEx);

//Create Window
hWnd = CreateWindowEx(NULL, "My Window", "Windows Application", WS_OVERLAPPEDWINDOW, 200, 200, 640, 480, NULL, NULL, hInstance, NULL);

SendMessage(hWndListL, LB_ADDSTRING, NULL, (LPARAM)"one");
SendMessage(hWndListL, LB_ADDSTRING, NULL, (LPARAM)"two");

//Show Window
ShowWindow(hWnd, SW_SHOWNORMAL);

while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

return 0;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
char buffer[50] = "";

switch(Msg)
{
case WM_CREATE:
//Create Listbox's
hWndListL = CreateWindowEx(NULL, "LISTBOX", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | LBS_NOTIFY, 50, 35, 200, 100, hWnd, NULL, GetModuleHandle(NULL), NULL);
hWndListR = CreateWindowEx(NULL, "LISTBOX", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | LBS_NOTIFY, 350, 35, 200, 100, hWnd, NULL, GetModuleHandle(NULL), NULL);
//Create Button
hWndButton = CreateWindowEx(NULL, "BUTTON", "OK", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_DISABLED, 50, 220, 100, 24, hWnd, (HMENU)IDC_MAIN_BUTTON, GetModuleHandle(NULL), NULL);
break;
case WM_COMMAND:
switch(HIWORD(wParam))
{
case LBN_SELCHANGE:
{
//EnableWindow( GetDlgItem( hWnd, (HMENU)IDC_BUTTON_MAIN ), TRUE );
EnableWindow(hWndButton, true);
break;
}
}
switch(LOWORD(wParam))
{
case IDC_MAIN_BUTTON:
{
//length = SendMessage(hWndListL, LB_GETTEXTLEN, NULL, NULL);
SendMessage(hWndListL, LB_GETTEXT, NULL, (LPARAM)buffer);
SendMessage(hWndListR, LB_ADDSTRING, NULL, (LPARAM)buffer);
SendMessage(hWndListL, LB_DELETESTRING, NULL, NULL);
EnableWindow(hWndButton, false);
break;
}
}
break;
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}

以下是带有列表框的窗口的示例代码:

#include <Windows.h>
/// unique class name
#define CLASS_NAME "MyWinapiClassNameWithUniqeSetOfCharactersThatAreNotMyPassword_50kz5S99g2Q88bTi3ne"
/// unique ID of our listbox command
#define IDC_LISTBOX_ID 123

static LRESULT WINAPI wndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ){
switch(msg){
case WM_CREATE:{
HWND listboxHwnd= CreateWindow( "LISTBOX", NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD | LBS_NOTIFY, 10, 10, 200, 100, hwnd, (HMENU)IDC_LISTBOX_ID, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
SendMessage( listboxHwnd, LB_ADDSTRING, 0, (LPARAM)"List Item 0" );
SendMessage( listboxHwnd, LB_ADDSTRING, 0, (LPARAM)"List Item 1" );
SendMessage( listboxHwnd, LB_ADDSTRING, 0, (LPARAM)"List Item 2" );
SendMessage( listboxHwnd, LB_ADDSTRING, 0, (LPARAM)"List Item 3" );
SendMessage( listboxHwnd, LB_ADDSTRING, 0, (LPARAM)"List Item 4" );
SendMessage( listboxHwnd, LB_ADDSTRING, 0, (LPARAM)"List Item 5" );
SendMessage( listboxHwnd, LB_ADDSTRING, 0, (LPARAM)"List Item 6" );
SendMessage( listboxHwnd, LB_ADDSTRING, 0, (LPARAM)"List Item 7" );
SendMessage( listboxHwnd, LB_ADDSTRING, 0, (LPARAM)"List Item 8" );
SendMessage( listboxHwnd, LB_ADDSTRING, 0, (LPARAM)"List Item 9" );
}break;
case WM_COMMAND:{
switch( LOWORD(wParam) ){
case IDC_LISTBOX_ID:{
switch(HIWORD(wParam)){
case LBN_SELCHANGE:{ /// user have selected item in our listbox
int id= SendMessage( (HWND)lParam, LB_GETCARETINDEX, 0, 0 ); /// id of seleted item, starting from 0
char text[]= "Item 0 selected";
text[5] += id; /// thats one way of converting int to string :D
MessageBox( NULL, text, "Debug", MB_OK );
}break;
}
}break;
}
}break;
case WM_DESTROY:{
::PostQuitMessage(0);
}break;
}
return DefWindowProc( hwnd, msg, wParam, lParam);
}

int WINAPI WinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd ){
WNDCLASSEX wc= {
sizeof(WNDCLASSEX),
CS_CLASSDC,
wndProc,
0,
0,
hInstance,
LoadIcon( NULL, IDI_APPLICATION ),
LoadCursor( NULL, IDC_ARROW ),
(HBRUSH)(COLOR_WINDOW+0),
NULL,
CLASS_NAME,
NULL
};
if( !RegisterClassEx(&wc) ){
MessageBox( NULL, "Fail to register window class.", "Error - Keyboardlord", MB_ICONERROR );
return -2;
}
HWND hwnd= CreateWindow( CLASS_NAME, "App Browser - Keyboardlord", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 250, 150, NULL, NULL, hInstance, NULL );
if( hwnd==NULL ){
MessageBox( NULL, "Fail to create window", "Error - Keyboardlord", MB_ICONERROR );
return -3;
}
ShowWindow( hwnd, nShowCmd );
MSG msg;
while( 0 < GetMessage( &msg, NULL, 0, 0) ){
TranslateMessage( &msg ); 
DispatchMessage( &msg );
}
::DestroyWindow( hwnd );
::UnregisterClass( CLASS_NAME, hInstance );
return 0;
}