Animax97 Temat założony przez niniejszego użytkownika |
[WinAPI] ListView » 2011-03-10 21:40:52 Cześć, chciałbym się zacząć bawić ListView, ale kontrolka mi się nie chce wyświetlić.
#include <windows.h> #include <commctrl.h>
HWND hListView, hWnd, hButton;
CHAR szClassName[] = "OknoRodzica"; HINSTANCE * hInst;
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR lStart, INT nShow ) { hInst =& hInstance; WNDCLASSEX wc; wc.hInstance =* hInst; wc.lpszClassName = szClassName; wc.lpfnWndProc = WndProc; wc.style = 0; wc.cbSize = sizeof( WNDCLASSEX ); wc.hIcon = LoadIcon( 0, IDI_APPLICATION ); wc.hIconSm = LoadIcon( 0, IDI_APPLICATION ); wc.hCursor = LoadCursor( 0, IDC_ARROW ); wc.lpszMenuName = 0; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground =( HBRUSH ) COLOR_BACKGROUND; if( !RegisterClassEx( & wc ) ) return 0; hWnd = CreateWindowEx( 0, szClassName, "Tested", WS_OVERLAPPEDWINDOW, 20, 20, 600, 300, 0, 0, * hInst, 0 ); ShowWindow( hWnd, nShow ); MSG msgs; while( GetMessage( & msgs, 0, 0, 0 ) ) { TranslateMessage( & msgs ); DispatchMessage( & msgs ); } return msgs.wParam; }
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wPar, LPARAM lPar ) { switch( msg ) { case WM_CREATE: hButton = CreateWindowEx( 0, WC_BUTTON, "Przycisk", WS_CHILD | WS_VISIBLE, 20, 20, 200, 70, hwnd,( HMENU ) 1, * hInst, 0 ); RECT rcl; GetClientRect( hwnd, & rcl ); hListView = CreateWindowEx( 0, WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_EDITLABELS, 20, 50, rcl.right, rcl.bottom, hwnd,( HMENU ) 1000, * hInst, NULL ); LVCOLUMN lvc; lvc.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; lvc.iSubItem = 0; lvc.cx = 200; lvc.pszText = "Nazwa pliku"; ListView_InsertColumn( hListView, 0, & lvc ); lvc.iSubItem = 1; lvc.cx = 50; lvc.pszText = "Rozmiar"; ListView_InsertColumn( hListView, 1, & lvc ); lvc.iSubItem = 2; lvc.cx = 100; lvc.pszText = "Data"; ListView_InsertColumn( hListView, 2, & lvc ); ListView_SetItemText( hListView, 0, 1, "15" ); ListView_SetItemText( hListView, 0, 2, "14 kwietnia 2003" ); ListView_SetItemText( hListView, 1, 1, "3701" ); ListView_SetItemText( hListView, 1, 2, "31 lutego 1999" ); ListView_SetItemText( hListView, 2, 1, "100" ); ListView_SetItemText( hListView, 2, 2, "7 sierpnia 2006" ); break; case WM_DESTROY: DestroyWindow( hListView ); PostQuitMessage( 0 ); break; default: return DefWindowProc( hwnd, msg, wPar, lPar ); } return 0; }
Pojawia się tylko Przycisk, a ListView nie :( Kompiluje w Code::Blocks, trzeba jakąś specjalną bibliotekę a może zły kod?
Proszę o pomoc. |
|
DejaVu |
» 2011-03-10 21:44:41 |
|
Animax97 Temat założony przez niniejszego użytkownika |
» 2011-03-10 22:00:02 #DeJaVu
Dalej nic.
#include <windows.h> #include <commctrl.h> #define WINVER 0x0501 #define _WIN32_WINNT 0x0501 HWND hListView, hWnd, hButton;
CHAR szClassName[] = "OknoRodzica"; HINSTANCE * hInst;
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR lStart, INT nShow ) { hInst =& hInstance; WNDCLASSEX wc; wc.hInstance =* hInst; wc.lpszClassName = szClassName; wc.lpfnWndProc = WndProc; wc.style = 0; wc.cbSize = sizeof( WNDCLASSEX ); wc.hIcon = LoadIcon( 0, IDI_APPLICATION ); wc.hIconSm = LoadIcon( 0, IDI_APPLICATION ); wc.hCursor = LoadCursor( 0, IDC_ARROW ); wc.lpszMenuName = 0; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground =( HBRUSH ) COLOR_BACKGROUND; if( !RegisterClassEx( & wc ) ) return 0; hWnd = CreateWindowEx( 0, szClassName, "Tested", WS_OVERLAPPEDWINDOW, 20, 20, 600, 300, 0, 0, * hInst, 0 ); ShowWindow( hWnd, nShow ); MSG msgs; while( GetMessage( & msgs, 0, 0, 0 ) ) { TranslateMessage( & msgs ); DispatchMessage( & msgs ); } return msgs.wParam; }
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wPar, LPARAM lPar ) { switch( msg ) { case WM_CREATE: RECT rcl; GetClientRect( hwnd, & rcl ); hListView = CreateWindowEx( 0, WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_EDITLABELS, 20, 50, rcl.right, rcl.bottom, hwnd,( HMENU ) 1000, * hInst, NULL ); LVCOLUMN lvc; lvc.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; lvc.iSubItem = 0; lvc.cx = 200; lvc.pszText = "Nazwa pliku"; ListView_InsertColumn( hListView, 0, & lvc ); lvc.iSubItem = 1; lvc.cx = 50; lvc.pszText = "Rozmiar"; ListView_InsertColumn( hListView, 1, & lvc ); lvc.iSubItem = 2; lvc.cx = 100; lvc.pszText = "Data"; ListView_InsertColumn( hListView, 2, & lvc ); ListView_SetItemText( hListView, 0, 1, "15" ); ListView_SetItemText( hListView, 0, 2, "14 kwietnia 2003" ); ListView_SetItemText( hListView, 1, 1, "3701" ); ListView_SetItemText( hListView, 1, 2, "31 lutego 1999" ); ListView_SetItemText( hListView, 2, 1, "100" ); ListView_SetItemText( hListView, 2, 2, "7 sierpnia 2006" ); break; case WM_DESTROY: DestroyWindow( hListView ); PostQuitMessage( 0 ); break; default: return DefWindowProc( hwnd, msg, wPar, lPar ); } return 0; }
|
|
DejaVu |
» 2011-03-10 22:00:53 Przeczytaj jeszcze ze 2 razy mojego posta... bo nie zastosowałeś się do niego :)
/edit:
Pod Visualem też nie działa - zerknę na to.
/edit2:
Wywołanie funkcji CreateWindowEx zwraca następujący błąd:
CreateWindowEx failed with error 1407: Nie można odnaleźć klasy okna.
|
|
Animax97 Temat założony przez niniejszego użytkownika |
» 2011-03-10 22:17:01 O, którą linijkę chodzi, bo ta funkcja się powtarza kilka razy? |
|
DejaVu |
» 2011-03-10 22:17:54 #define WINVER 0x0501 #define _WIN32_WINNT 0x0501 #include <windows.h> #include <commctrl.h> #include <strsafe.h>
HWND hListView, hWnd, hButton;
CHAR szClassName[] = "OknoRodzica"; HINSTANCE * hInst;
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR lStart, INT nShow ) { RECT rcl; INITCOMMONCONTROLSEX icex; icex.dwSize = sizeof( INITCOMMONCONTROLSEX ); icex.dwICC = ICC_LISTVIEW_CLASSES; InitCommonControlsEx( & icex ); hInst =& hInstance; WNDCLASSEX wc; wc.hInstance =* hInst; wc.lpszClassName = szClassName; wc.lpfnWndProc = WndProc; wc.style = 0; wc.cbSize = sizeof( WNDCLASSEX ); wc.hIcon = LoadIcon( 0, IDI_APPLICATION ); wc.hIconSm = LoadIcon( 0, IDI_APPLICATION ); wc.hCursor = LoadCursor( 0, IDC_ARROW ); wc.lpszMenuName = 0; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground =( HBRUSH ) COLOR_BACKGROUND; if( !RegisterClassEx( & wc ) ) return 0; hWnd = CreateWindowEx( 0, szClassName, "Tested", WS_OVERLAPPEDWINDOW, 20, 20, 600, 300, 0, 0, * hInst, 0 ); ShowWindow( hWnd, nShow ); MSG msgs; while( GetMessage( & msgs, 0, 0, 0 ) ) { TranslateMessage( & msgs ); DispatchMessage( & msgs ); } return msgs.wParam; }
void ShowLastError( LPTSTR lpszFunction ) { LPVOID lpMsgBuf; LPVOID lpDisplayBuf; DWORD dw = GetLastError(); FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), ( LPTSTR ) & lpMsgBuf, 0, NULL ); lpDisplayBuf =( LPVOID ) LocalAlloc( LMEM_ZEROINIT, ( lstrlen(( LPCTSTR ) lpMsgBuf ) + lstrlen(( LPCTSTR ) lpszFunction ) + 40 ) * sizeof( TCHAR ) ); StringCchPrintf(( LPTSTR ) lpDisplayBuf, LocalSize( lpDisplayBuf ) / sizeof( TCHAR ), TEXT( "%s failed with error %d: %s" ), lpszFunction, dw, lpMsgBuf ); OutputDebugStr(( LPCTSTR ) lpDisplayBuf ); MessageBox( NULL,( LPCTSTR ) lpDisplayBuf, TEXT( "Error" ), MB_OK ); LocalFree( lpMsgBuf ); LocalFree( lpDisplayBuf ); }
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wPar, LPARAM lPar ) { switch( msg ) { case WM_CREATE: RECT rcl; GetClientRect( hwnd, & rcl ); hListView = CreateWindowEx( 0, WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_EDITLABELS, 20, 50, rcl.right, rcl.bottom, hwnd,( HMENU ) 1000, * hInst, NULL ); ShowLastError( "CreateWindowEx" ); LVCOLUMN lvc; lvc.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; lvc.iSubItem = 0; lvc.cx = 200; lvc.pszText = "Nazwa pliku"; ListView_InsertColumn( hListView, 0, & lvc ); lvc.iSubItem = 1; lvc.cx = 50; lvc.pszText = "Rozmiar"; ListView_InsertColumn( hListView, 1, & lvc ); lvc.iSubItem = 2; lvc.cx = 100; lvc.pszText = "Data"; ListView_InsertColumn( hListView, 2, & lvc ); ListView_SetItemText( hListView, 0, 1, "15" ); ListView_SetItemText( hListView, 0, 2, "14 kwietnia 2003" ); ListView_SetItemText( hListView, 1, 1, "3701" ); ListView_SetItemText( hListView, 1, 2, "31 lutego 1999" ); ListView_SetItemText( hListView, 2, 1, "100" ); ListView_SetItemText( hListView, 2, 2, "7 sierpnia 2006" ); break; case WM_DESTROY: DestroyWindow( hListView ); PostQuitMessage( 0 ); break; default: return DefWindowProc( hwnd, msg, wPar, lPar ); } return 0; }
Do linkera musisz jeszcze dodać: Comctl32.lib
Generalnie sprawa się rozchodzi o:
RECT rcl; INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof( INITCOMMONCONTROLSEX ); icex.dwICC = ICC_LISTVIEW_CLASSES; InitCommonControlsEx( & icex );
Więcej możesz doczytać tu: http://msdn.microsoft.com/en-us/library/bb774736(v=vs.85).aspx |
|
« 1 » |