-1有可能是指针,如果是,要获取指针之后,提取指针的内容,以下我贴上代码:
// Testcpp : 定义应用程序的入口点。
//
#include "stdafxh"
#include "Testh"
#define MAX_LOADSTRING 100
HWND hListBox = NULL;
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msghwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msgwParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcexcbSize = sizeof(WNDCLASSEX);
wcexstyle = CS_HREDRAW | CS_VREDRAW;
wcexlpfnWndProc = WndProc;
wcexcbClsExtra = 0;
wcexcbWndExtra = 0;
wcexhInstance = hInstance;
wcexhIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST));
wcexhCursor = LoadCursor(NULL, IDC_ARROW);
wcexhbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcexlpszMenuName = MAKEINTRESOURCE(IDC_TEST);
wcexlpszClassName = szWindowClass;
wcexhIconSm = LoadIcon(wcexhInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent, nCurIndex;
PAINTSTRUCT ps;
HDC hdc;
static TCHAR buf[20] = _T("");
switch (message)
{
case WM_CREATE:
hListBox = CreateWindow(_T("listbox"), NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_TABSTOP | LBS_STANDARD ,20, 20, 60, 60, hWnd, (HMENU)10000, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)_T("中国"));
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)_T("美国"));
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)_T("英国"));
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case 10000:
if (LBN_SELCHANGE == wmEvent)
{
nCurIndex = SendMessage((HWND)lParam, LB_GETCURSEL, 0, 0);
SendMessage((HWND)lParam, LB_GETTEXT, nCurIndex, (LPARAM)buf);
MessageBox(NULL, buf, _T("提示"), MB_OK);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
建议楼主看看这个代码,重点在于
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)_T("中国"));//列表框内容加上中国
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)_T("美国"));//列表框内容加上美国
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)_T("英国"));//列表框内容加上英国
nCurIndex = SendMessage((HWND)lParam, LB_GETCURSEL, 0, 0);//获取指针
SendMessage((HWND)lParam, LB_GETTEXT, nCurIndex, (LPARAM)buf);//将指针的内容给buf)
MessageBox(NULL, buf, _T("提示"), MB_OK);//跳出提示框显示列表框内容所点击的内容
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32));
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msghwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
还要建立加速键表资源和相应项,指向菜单,或自定义的ID,
在wm_command或wm_syscommand中处理,和菜单类似
你可以建立默认的win32工程试试,或者是helloworld的win32工程,vs的几个版本应该差的不太多,里面应该含有例子
ATOM MyRegisterClass(HINSTANCE hInstance)//注册窗口类 { WNDCLASSEX wcex;//声明变量 wcexcbSize = sizeof(WNDCLASSEX); //WNDCLASSEX 的大小 wcexstyle = CS_HREDRAW | CS_VREDRAW;//从这个窗口类派生的窗口具有的风格 wcexlpfnWndProc = (WNDPROC)WndProc;//窗口处理函数的指针 wcexcbClsExtra = 0;//指定紧跟在窗口类结构后的附加字节数 wcexcbWndExtra = 0;//指定紧跟在窗口事例后的附加字节数 wcexhInstance = hInstance;//本模块的事例句柄 wcexhIcon = LoadIcon(hInstance, (LPCTSTR)IDI_AAA);//图标的句柄 wcexhCursor = LoadCursor(NULL, IDC_ARROW);//光标的句柄 wcexhbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景画刷的句柄 wcexlpszMenuName = (LPCSTR)IDC_AAA;//指向菜单的指针 wcexlpszClassName = szWindowClass;//指向类名称的指针 wcexhIconSm = LoadIcon(wcexhInstance, (LPCTSTR)IDI_SMALL);//和窗口类关联的小图标 return RegisterClassEx(&wcex); [D1] //注册窗口类 } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); [D2] //创建 if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); [D3] //显示窗口 UpdateWindow(hWnd); [D4] //刷新窗口 return TRUE; }
希望采纳
1 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam , LPARAM 45 lparam)
这里不应该有45, 去掉
2
case WM_DESTROY(0);
去掉(0), ";"号换成":"
3
return DefWindowProc(hwnd,message,wParam,lParm);
lParm 拼写错误,传进的参数是lparam, 同时注意大小写
以上就是关于LB_GETCURSEL获取索引失败的原因有哪些全部的内容,包括:LB_GETCURSEL获取索引失败的原因有哪些、一个简单的win32窗口程序、下面这一段程序,希望能给出详细解释(最好每一行都有)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)