我想改變一個列表視圖控件的Wndproc,所以wndproc中的第一個參數返回控制句柄接收消息,問題是當我改變它其他功能停止工作(我不能插入列或物品了),是不是我需要改變或爲了恢復使用相同的WndProc,以保持對所有的控件如何更改列表視圖控件的WndProc
WNDPROC同所有控件:
LRESULT CALLBACK staticWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam){
std::cout << (int)hwnd << " control received msg:" << uMsg << std::endl; //This must work
//event = { msg:uMsg, target:(int)hwnd, x:0, y:0, button:0, key:0 };
switch (uMsg){
case WM_DESTROY:
std::cout << "window says bye " << std::endl;
PostQuitMessage(WM_QUIT);
break;
default:
//msghandlercall(event); //this is handled not in c++
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
打電話來SetWindowLongPtr(handle, GWLP_WNDPROC, (LONG_PTR)staticWndProc);
後插入消息不起作用默認
int createColumn(HWND listhandle, int indexCol, char *Text, int width){
LVCOLUMN lvc={0};
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.fmt = LVCFMT_LEFT;
lvc.cx = width;
lvc.pszText = Text;
lvc.iSubItem = indexCol;
//return ListView_InsertColumn(listhandle, indexCol, &lvc);
SendMessage(listhandle,LVM_INSERTCOLUMN,indexCol,(LPARAM)&lvc);
return 1;
}
void createColumns(HWND listhandle, std::vector<LPSTR> columns){
for(int i=0; i<columns.size(); i++) createColumn(listhandle, i, columns[i], 50);
}
int createItem(HWND listhandle, const std::vector<LPSTR>& row){
LVITEM lvi = {0};
//lvi.mask = LVIF_TEXT;
// lvi.pszText = row[0];
int ret = ListView_InsertItem(listhandle, &lvi);
if(ret>-1) for(unsigned i=0; i<row.size(); i++)
ListView_SetItemText(listhandle, ret, i, row[i]);
return ret;
}
HWND createList(int parenthandle=0){
if(parenthandle==0) parenthandle=(int)GetDesktopWindow();
HWND handle = CreateWindow(WC_LISTVIEW, "",WS_VISIBLE|WS_BORDER|WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
10, 10, 300, 100, (HWND)parenthandle, /*(HMENU)ID_LIST*/NULL, GetModuleHandle(NULL), 0);
if(!handle){ std::cerr << "Failed to create list\n"; return 0; }
SetWindowLongPtr(handle, GWLP_WNDPROC, (LONG_PTR)staticWndProc);
ListView_SetExtendedListViewStyle(handle, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_DOUBLEBUFFER);
createColumns(handle,{"col1","col2","col3"});
createItem(handle,{"item1.1","item1.2","item1.3","item1.4"});
createItem(handle,{"item2.1","item2.2","item2.3","item2.4"});
return handle;
}
注意我嘗試添加的WndProc與SetWindowSubclass()
但出現錯誤: error C2664: 'BOOL IsolationAwareSetWindowSubclass(HWND,SUBCLASSPROC,UINT_PTR,DWORD_PTR)': cannot convert argument 2 from 'LRESULT (__cdecl *)(HWND,UINT,WPARAM,LPARAM)' to 'SUBCLASSPROC' [build\binding.vcxproj]
你只使用WNDPROC處理銷燬消息 - 所以這是唯一的想你可以使用該WNDPROC。您可能需要向WNDPROC添加更多消息。 –
您需要獲取控件的現有回調,並調用不是「DefWindowProc」的默認情況下的語句。 –
調用DefWindowProc()爲子類窗口實際*繞過窗口的WndProc()。所以很確定,很多東西停止工作。您必須調用* original * WndProc,這是您應該首先使用GetWindowLongPtr檢索的那個。這當然意味着你不能使用一個WndProc來銷燬它們。嘗試下降[在成功的坑中](https://msdn.microsoft。COM/EN-US /庫/窗/桌面/ bb762102(V = vs.85)的.aspx)。 –