Réponse acceptée !
"à la personne qui vient de m'écrire un message"
Heu !
Un message privé ?
Si oui peux tu donner le contenu du message ainsi que le pseudo de celui qui te l'a envoyé ?
Merci.
Pour ton problème c'est expliqué dans la doc,
ici précisément.
Globalement, on intercepte les LPNMTVDISPINFO.
Si pszText n'est pad renseigné, c'est que l'utilisateur à annulé donc on ne fait rien.
Si pszText est là, suffit de faire un TVM_SETITEM.
case WM_NOTIFY:
if (((NMHDR*)lParam)->code == TVN_ENDLABELEDIT)
{
lpDispInfo = (LPNMTVDISPINFO)lParam;
/* Si le texte est à zéro, c'est que l'utilisateur a annulé */
if (lpDispInfo->item.pszText)
SendMessage(_hTreeView, TVM_SETITEM, 0, (LPARAM)&lpDispInfo->item);
}
break;
Code complet :
Application GUI.
Pour compiler sous gcc, ajouter -nostartfiles -nodefaultlibs -nostdlib -ffreestanding dans les options du lieur.
Pour compiler sous VC, s'arranger pour que le lieur ignore toutes les librairies par défaut (/nodefaultlib).
S'assurer que kernel32 et user32 sont en entrée du lieur.
Sous VC, en cas d'erreur LNK2001 : __chkesp symbole externe non résolu -> Enlever /GZ des options de compilation.
#ifdef UNICODE
#define _UNICODE
#endif /* UNICODE */
#define _WIN32_IE 0x0400
#include <windows.h>
#include <tchar.h>
#include <commctrl.h>
HINSTANCE _hThisInstance; /* Handle du module */
HWND _hWnd; /* Handle de la fenêtre */
HWND _hStatusBar; /* Handle sur la bar de status */
HWND _hTreeView; /* Handle sur le treeview */
LPTSTR _lpAppName = _T("TreeView"); /* Nom de l'appli */
DWORD _nStatusBarHeight; /* Redimenssionement du treeview */
/**
* Affiche un message d'erreur correspondant à la dernière erreur Win32
*/
DWORD __stdcall ShowLastError()
{
DWORD nLastError; /* Numéro de l'erreur */
LPTSTR lpMessageBuffer; /* Récupération du message */
nLastError = GetLastError();
/* Formatage du message */
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, nLastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(void*)&lpMessageBuffer, 0, NULL);
/* Affichage du message */
MessageBox(NULL, lpMessageBuffer, _T("ERROR"), MB_OK | MB_ICONERROR);
LocalFree(lpMessageBuffer);
return nLastError;
}
/**
* Traitement des messages
*/
LRESULT __stdcall WindowProcedure(HWND hWnd, UINT nMessage, WPARAM wParam, LPARAM lParam)
{
MINMAXINFO *lpMinMaxInfo; /* Info sur les tailles min et max de la fenêtre */
RECT clientRect; /* Taille de la zone cliente */
int bHandled; /* Pour savoir si le message est traité */
LPNMTVDISPINFO lpDispInfo; /* Info sur l'édition du noeud */
long nResult;
nResult = 0;
bHandled = 0;
switch (nMessage)
{
case WM_GETMINMAXINFO:
lpMinMaxInfo = (MINMAXINFO*)lParam;
lpMinMaxInfo->ptMinTrackSize.x = 300;
lpMinMaxInfo->ptMinTrackSize.y = 200;
bHandled = 1;
break;
case WM_SIZE:
/* On signale à la barre de status que la fenêtre a été redimenssionnée */
SendMessage(_hStatusBar, WM_SIZE, wParam, lParam);
/* On ajuste la taille de le treeview */
GetClientRect(_hWnd, &clientRect);
SetWindowPos(_hTreeView, 0, 0, 0,
clientRect.right - clientRect.left,
clientRect.bottom - clientRect.top - _nStatusBarHeight,
SWP_NOMOVE | SWP_NOZORDER);
break;
case WM_NOTIFY:
if (((NMHDR*)lParam)->code == TVN_ENDLABELEDIT)
{
lpDispInfo = (LPNMTVDISPINFO)lParam;
/* Si le texte est à zéro, c'est que l'utilisateur a annulé */
if (lpDispInfo->item.pszText)
SendMessage(_hTreeView, TVM_SETITEM, 0, (LPARAM)&lpDispInfo->item);
}
break;
case WM_DESTROY:
/* On signale que le thread va s'arrêter */
PostQuitMessage(0);
bHandled = 1;
break;
}
if (! bHandled)
nResult = DefWindowProc(hWnd, nMessage, wParam, lParam);
return nResult;
}
/**
* Ajoute la barre de status
*/
int __stdcall CreateStatusBar()
{
int nWidth; /* Largeur de la partie 0 */
RECT statusBarRect; /* Taille de la barre de status */
int nResult;
nResult = 0;
/* Création de la barre de status */
_hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, (LPCTSTR)NULL,
SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE,
0, 0, 0, 0,
_hWnd, NULL, _hThisInstance, NULL);
if (! _hStatusBar) goto the_end;
/* Affectation du nombre de parties */
nWidth = -1;
SendMessage(_hStatusBar, SB_SETPARTS, (WPARAM)1, (LPARAM)&nWidth);
/* Calcul de la hauteur de la barre de status */
GetWindowRect(_hStatusBar, &statusBarRect);
_nStatusBarHeight = statusBarRect.bottom - statusBarRect.top;
nResult = 1;
the_end:
return nResult;
}
/**
* Crée une imagelist utilisée par le treeview
*/
int __stdcall CreateImageList()
{
HIMAGELIST hImageList; /* Handle sur la imagelist */
int nResult;
nResult = 0;
/* Création de la liste */
hImageList = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
ILC_MASK, 1, 1);
if (! hImageList) goto the_end;
/* Ajout d'un icône */
if (ImageList_ReplaceIcon(hImageList, -1, LoadIcon(NULL, IDI_APPLICATION)) == -1) goto the_end;
/* Affectation de la imagelist à le treeview */
SendMessage(_hTreeView, TVM_SETIMAGELIST, (WPARAM)TVSIL_NORMAL, (LPARAM)hImageList);
nResult = 1;
the_end:
return nResult;
}
/**
* Crée les items de le treeview
*/
int __stdcall CreateItems()
{
TVINSERTSTRUCT insertStruct; /* Structure contenant les infos d'insertion */
HTREEITEM hRoot; /* Handle sur la racine */
insertStruct.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
insertStruct.item.pszText = _T("Item1");
insertStruct.item.cchTextMax = lstrlen(insertStruct.item.pszText) + 1;
insertStruct.item.iImage = 0;
insertStruct.item.iSelectedImage = 0;
insertStruct.hParent = TVI_ROOT;
insertStruct.hInsertAfter = TVI_ROOT;
hRoot = (HTREEITEM)SendMessage(_hTreeView, TVM_INSERTITEM, 0, (LPARAM)&insertStruct);
insertStruct.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
insertStruct.item.pszText = _T("Item2");
insertStruct.item.cchTextMax = lstrlen(insertStruct.item.pszText) + 1;
insertStruct.item.iImage = 0;
insertStruct.item.iSelectedImage = 0;
insertStruct.hParent = hRoot;
insertStruct.hInsertAfter = TVI_FIRST;
SendMessage(_hTreeView, TVM_INSERTITEM, 0, (LPARAM)&insertStruct);
insertStruct.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
insertStruct.item.pszText = _T("Item3");
insertStruct.item.cchTextMax = lstrlen(insertStruct.item.pszText) + 1;
insertStruct.item.iImage = 0;
insertStruct.item.iSelectedImage = 0;
insertStruct.hParent = hRoot;
insertStruct.hInsertAfter = TVI_LAST;
SendMessage(_hTreeView, TVM_INSERTITEM, 0, (LPARAM)&insertStruct);
/* Expand */
SendMessage(_hTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
return 1;
}
/**
* Ajoute la treeview
*/
int __stdcall CreateTreeView()
{
int nResult;
nResult = 0;
_hTreeView = CreateWindowEx(0, WC_TREEVIEW, (LPCTSTR)NULL,
TVS_HASLINES | TVS_HASBUTTONS |
TVS_LINESATROOT | TVS_EDITLABELS |
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0,
_hWnd, NULL, _hThisInstance, NULL);
if (! _hTreeView) goto the_end;
if (! CreateImageList()) goto the_end;
if (! CreateItems()) goto the_end;
nResult = 1;
the_end:
return nResult;
}
/**
* Initialise la fenêtre principale de l'appli.
*/
int __stdcall CreateMyWindow()
{
WNDCLASSEX wincl; /* Classe de la fenêtre utilisée */
int nResult;
nResult = 0;
/* Création de la classe de fenêtre */
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.style = 0;
wincl.lpfnWndProc = WindowProcedure;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hInstance = _hThisInstance;
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wincl.lpszMenuName = 0;
wincl.lpszClassName = _lpAppName;
wincl.hIconSm = NULL;
/* Enregistrement de la classe */
if (! RegisterClassEx(&wincl)) goto the_end;
/* Création de la fenêtre */
_hWnd = CreateWindowEx(0, _lpAppName, _lpAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 300,
HWND_DESKTOP, NULL, _hThisInstance, NULL);
if (! _hWnd) goto the_end;
/* Création de la barre de status */
if (! CreateStatusBar()) goto the_end;
/* Création de la treeview */
if (! CreateTreeView()) goto the_end;
/* Affichage de la fenêtre */
ShowWindow (_hWnd, SW_SHOW);
nResult = 1;
the_end:
return nResult;
}
/**
* Main
*/
int __cdecl WinMainCRTStartup()
{
MSG messages; /* Messages envoyés à l'application */
INITCOMMONCONTROLSEX initCommon; /* Initialisation de comctl32 */
int nResult;
initCommon.dwSize = sizeof(initCommon);
initCommon.dwICC = ICC_BAR_CLASSES | ICC_TREEVIEW_CLASSES;
InitCommonControlsEx(&initCommon);
/* Récupération du handle du module */
_hThisInstance = GetModuleHandle(NULL);
if (! CreateMyWindow())
{
nResult = ShowLastError();
goto the_end;
}
/* Boucle de traitement des messages */
while (GetMessage(&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
nResult = messages.wParam;
the_end:
/* ExitProcess nécessaire car sinon c'est un ExitThread */
ExitProcess(nResult);
/* Pour esquiver le warning */
return 0;
}