whats wrong with this code!!

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
m trying to create application which accepts data from the user and place it in a listview... some hows i get one or the other error... please tell me what this error means "hListView is being used wihtout being initialised "... please help..

#define _WIN32_IE 0x300<br/>
#include <windows.h><br/>
#include <commctrl.h><br/>
#include <stdio.h><br/>
#pragma comment(lib, "gdi32.lib")<br/>
#pragma comment(lib, "comctl32.lib")<br/>
#include "resource.h"<br/>
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);<br/>
INT_PTR CALLBACK Add(HWND ,UINT , WPARAM, LPARAM); <br/>
HINSTANCE hThisInst;<br/>
HINSTANCE hInstance;<br/>
LVCOLUMN lvc = { 0 };<br/>
LVITEM lv = { 0 };<br/>
<br/>
<br/>
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,<br/>
LPSTR Args, int WinMode)<br/>
{<br/>
HWND hWnd;<br/>
MSG Message;<br/>
WNDCLASSEX WinClass = { 0 };<br/>
INITCOMMONCONTROLSEX icc = { 0 };<br/>
<br/>
hInstance = hThisInst;<br/>
<br/>
icc.dwSize = sizeof(icc);<br/>
icc.dwICC = ICC_LISTVIEW_CLASSES;<br/>
InitCommonControlsEx(&icc);<br/>
<br/>
WinClass.cbSize = sizeof(WNDCLASSEX); <br/>
WinClass.hInstance = hThisInst;<br/>
WinClass.lpszClassName = TEXT("ListViewSample");<br/>
WinClass.lpfnWndProc = WindowFunc;<br/>
WinClass.style = 0; <br/>
WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);<br/>
WinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);<br/>
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);<br/>
WinClass.lpszMenuName = MAKEINTRESOURCE( IDR_MENU1 );<br/>
WinClass.cbClsExtra = 0;<br/>
WinClass.cbWndExtra = 0;<br/>
WinClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);<br/>
<br/>
if(!RegisterClassEx(&WinClass)) return 0;<br/>
//hInst = hThisInst;<br/>
hWnd = CreateWindow(TEXT("ListViewSample"),<br/>
TEXT("List View Sample"),<br/>
WS_OVERLAPPEDWINDOW,<br/>
CW_USEDEFAULT,<br/>
CW_USEDEFAULT,<br/>
CW_USEDEFAULT,<br/>
CW_USEDEFAULT,<br/>
NULL,<br/>
NULL,<br/>
hThisInst,<br/>
NULL);<br/>
<br/>
ShowWindow(hWnd, WinMode);<br/>
UpdateWindow(hWnd);<br/>
<br/>
while(GetMessage(&Message,<br/>
NULL,<br/>
0,<br/>
0))<br/>
{<br/>
TranslateMessage(&Message);<br/>
DispatchMessage(&Message);<br/>
}<br/>
return Message.wParam;<br/>
}<br/>
<br/>
LRESULT CALLBACK WindowFunc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)<br/>
{<br/>
PAINTSTRUCT ps;<br/>
HDC hdc;<br/>
HWND hListView;<br/>
HWND edit,button_ok;<br/>
TCHAR buff[100];<br/>
INT_PTR a; <br/>
int wmID, wmEVENT;<br/>
switch(message)<br/>
{<br/>
case WM_CREATE:<br/>
{<br/>
edit = CreateWindow(L"EDIT",<br/>
NULL,<br/>
WS_CHILD | WS_VISIBLE | LVS_REPORT,<br/>
700,<br/>
300,<br/>
150,<br/>
50,<br/>
hWnd,<br/>
(HMENU) 600,<br/>
hInstance,<br/>
NULL);<br/>
<br/>
button_ok=CreateWindowEx(NULL,<br/>
(LPCWSTR)L"BUTTON",<br/>
(LPCWSTR)L"OK",<br/>
WS_TABSTOP|WS_VISIBLE|<br/>
WS_CHILD|BS_DEFPUSHBUTTON,<br/>
760,<br/>
370,<br/>
40,<br/>
20,<br/>
hWnd,<br/>
(HMENU)ID_EOK,<br/>
hInstance,<br/>
NULL);<br/>
hListView = CreateWindow(WC_LISTVIEW,<br/>
NULL,<br/>
WS_CHILD | WS_VISIBLE | LVS_REPORT,<br/>
10,<br/>
10,<br/>
500,<br/>
500,<br/>
hWnd,<br/>
(HMENU) 500,<br/>
hInstance,<br/>
NULL);<br/>
<br/>
ListView_SetExtendedListViewStyle(hListView, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);<br/>
<br/>
lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH | LVCF_FMT;<br/>
lvc.fmt = LVCFMT_CENTER;<br/>
<br/>
/* Add four columns to the list-view (first column contains check box). */<br/>
lvc.iSubItem = 0;<br/>
lvc.cx = 75;<br/>
lvc.pszText = TEXT("Branch Name");<br/>
lvc.cx = 100;<br/>
ListView_InsertColumn(hListView, 0, &lvc);<br/>
<br/>
lvc.iSubItem = 1;<br/>
lvc.cx = 100;<br/>
lvc.pszText = TEXT("Attribute 1");<br/>
ListView_InsertColumn(hListView, 1, &lvc);<br/>
<br/>
lvc.iSubItem = 2;<br/>
lvc.cx = 70;<br/>
lvc.pszText = TEXT("Attribute 2");<br/>
ListView_InsertColumn(hListView, 2, &lvc);<br/>
<br/>
lvc.iSubItem = 3;<br/>
lvc.cx = 150;<br/>
lvc.pszText = TEXT("Attribute 3");<br/>
ListView_InsertColumn(hListView, 3, &lvc);<br/>
/* // Add some rows. <br/>
lv.iItem = 0;<br/>
ListView_InsertItem(hListView, &lv);<br/>
ListView_SetItemText(hListView, 0 , 0, TEXT("ece"));<br/>
ListView_SetItemText(hListView, 0, 1, TEXT("Friends"));<br/>
ListView_SetItemText(hListView, 0, 2, TEXT("500"));<br/>
ListView_SetItemText(hListView, 0, 3, TEXT("Alright"));<br/>
ListView_SetCheckState(hListView, 0, TRUE);<br/>
<br/>
<br/>
lv.iItem = 1;<br/>
ListView_InsertItem(hListView, &lv);<br/>
ListView_SetItemText(hListView, 1, 1, TEXT("Survivor"));<br/>
ListView_SetItemText(hListView, 1, 2, TEXT("970,000"));<br/>
ListView_SetItemText(hListView, 1, 3, TEXT("Please, not again"));<br/>
ListView_SetCheckState(hListView, 1, FALSE);<br/>
*/ <br/>
break;<br/>
}<br/>
case WM_COMMAND:<br/>
wmID = LOWORD ( wParam);<br/>
wmEVENT = HIWORD (wParam);<br/>
switch (wmID)<br/>
{<br/>
/* case ID_EDIT_ADD:<br/>
a = DialogBox(hThisInst, MAKEINTRESOURCE(ID_DIALOG1),hWnd,Add);<br/>
MessageBox(NULL,(LPCWSTR)a, (LPCWSTR)a,MB_OK|MB_ICONINFORMATION);<br/>
break;<br/>
<br/>
case ID_FILE_NEW:<br/>
DeleteAllItem(hListView);<br/>
break;<br/>
*/<br/>
case ID_EOK:<br/>
int iItem;<br/>
TCHAR ItemText[100];<br/>
<br/>
//iItem=SendMessage(hListView,LVM_GETITEMCOUNT,0,0); // number of items<br/>
<br/>
GetDlgItemText(hWnd,ID_MEDIT,ItemText,100); // get text from editobx<br/>
<br/>
if((lstrlen(ItemText))==0) // check if items exist<br/>
{<br/>
MessageBox(hWnd,L"Please Write Some Text",L"Error",MB_OK|MB_ICONINFORMATION);<br/>
break;<br/>
}<br/>
<br/>
lv.iItem=iItem; // choose item to enter to
<br/>
lv.iSubItem=0; // Put in first coluom (no need subitems)<br/>
<br/>
// Text to display (can be from a char variable) (Items)<br/>
lv.pszText=ItemText;<br/>
// Send item text to the Listview<br/>
SendMessage(hListView,LVM_INSERTITEM,0,(LPARAM)&lv);<br/>
}break;<br/>
<br/>
case ID_FILE_EXIT:<br/>
DestroyWindow (hWnd);<br/>
break;<br/>
<br/>
case WM_PAINT:<br/>
hdc = BeginPaint(hWnd, &ps);<br/>
// TODO: Add any drawing code here...<br/>
EndPaint(hWnd, &ps);<br/>
break;<br/>
<br/>
case WM_DESTROY:<br/>
PostQuitMessage(0);<br/>
break;<br/>
default:<br/>
return DefWindowProc(hWnd,<br/>
message,<br/>
wParam,<br/>
lParam);<br/>
}<br/>
return 0;<br/>
}<br/>
INT_PTR CALLBACK Add (HWND Add,UINT message, WPARAM wParam, LPARAM lParam)<br/>
{<br/>
UNREFERENCED_PARAMETER(lParam);<br/>
TCHAR gg[100];<br/>
HINSTANCE hinst = LoadLibrary((LPCWSTR)L"RichEd20.dll");<br/>
switch (message)<br/>
{<br/>
case WM_COMMAND:<br/>
if(LOWORD(wParam) == IDCANCEL)<br/>
EndDialog( Add, LOWORD(wParam) );<br/>
//return INT_PTR(TRUE);<br/>
if(LOWORD(wParam) == IDOK)<br/>
{<br/>
GetDlgItemText(Add, ID_EDIT1, gg,100);<br/>
return (INT_PTR)gg;<br/>
/*if((lstrlen(gg))==0) // check if items exist
<br/>
{<br/>
MessageBox(Add,gg, L"THE BRANCH DOESN HAV A NAME ??!",MB_OK|MB_ICONINFORMATION);<br/>
break;<br/>
}*/<br/>
//EndDialog(Add, LOWORD(wParam) );<br/>
return (INT_PTR)gg;<br/>
}<br/>
<br/>
break;<br/>
}<br/>
return (INT_PTR)FALSE;<br/>
}

View the full article
 
Back
Top