1
我有一個選項對話框,其中包含一個TreeView和另一個面板,旁邊是我選擇樹項目時更改的對話框。使用TreeView和麪板內存對話框的首選項對話框
我的第一個問題是當選項對話框加載焦點被設置爲默認按鈕,即使我刪除它。
我想把重點放在樹視圖中的第一個項目上,並將重點放在。
我試圖設置默認ID:
SendMessage(hDlgOptions, DM_SETDEFID, IDC_TREE, 0);
我試圖與NEXTDLGCTL沒有成功設置聚焦:
SendMessage(hDlgOptions, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlgOptions, IDC_TREE), TRUE);
我的第二個問題是窗口動畫的選項時,對話框顯示面板已被加載。我想在動畫演示之後加載它。
我爲此刻設置了一個計時器......也許你有更好的解決方案?
非常感謝您的專家評論我會採取一切優秀的想法來優化我的代碼。
對話框負載
我想要什麼
RESOURCE.RC
IDD_DIALOG_OPTIONS DIALOGEX 0, 0, 395, 175
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Options"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
PUSHBUTTON "OK",ID_BUTTON_SAVE,338,154,50,14
CONTROL "Save config to ini file",IDC_CHECK_SAVE_CONFIG,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,252,156,82,10
CONTROL "",IDC_TREE,"SysTreeView32",TVS_DISABLEDRAGDROP | TVS_SHOWSELALWAYS | TVS_SINGLEEXPAND | WS_HSCROLL | WS_TABSTOP,7,7,98,142
END
IDD_OPTIONS_CONSOLE DIALOGEX 0, 0, 277, 142
STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
CONTROL "Take Control",IDC_CHECK_TC,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,21,21,57,10
GROUPBOX "Console",IDC_STATIC_CONSOLE,7,7,134,66
COMBOBOX IDC_COMBO_CONSOLE_COLOR,50,56,85,63,CBS_DROPDOWN | WS_VSCROLL | WS_TABSTOP
LTEXT "Color",IDC_STATIC,17,59,18,8
END
IDD_OPTIONS_GENERAL DIALOGEX 0, 0, 277, 142
STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
LTEXT "GENERAL OPTION",IDC_STATIC,89,73,59,8
END
OptionsDialog
//GLOBAL VARIABLES
#define IDT_TIMER_PANEL 77
HWND hDlgOpt;
HWND hDlgPanelGeneral = NULL;
HWND hDlgPanelConsole = NULL;
UINT iPanelSelect = 1;
//HANDLE MESSAGES FROM GENERAL PANEL
LRESULT CALLBACK PanelGeneral(HWND hDlgPanel, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
return TRUE;
}
}
return FALSE;
}
//HANDLE MESSAGES FROM CONSOLE PANEL
LRESULT CALLBACK PanelConsole(HWND hDlgPanel, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
//INIT CONSOLE PANEL CONTROLS
return TRUE;
}
}
return FALSE;
}
//HANDLE MESSAGES FROM OPTIONS DIALOG
LRESULT CALLBACK Options(HWND hDlgOptions, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
hDlgOpt = hDlgOptions;
//LOAD PANEL GENERAL
HRSRC hrsrc = FindResourceEx(hInst, (LPCWSTR)RT_DIALOG, (LPCWSTR)IDD_OPTIONS_GENERAL, iLanguage);
HGLOBAL hglb = LoadResource(hInst, hrsrc);
LPVOID lpsz = LockResource(hglb);
hDlgPanelGeneral = CreateDialogIndirectW(GetModuleHandleW(NULL), lpsz, hDlgOptions, (DLGPROC)PanelGeneral);
//LOAD PANEL CONSOLE
hrsrc = FindResourceEx(hInst, (LPCWSTR)RT_DIALOG, (LPCWSTR)IDD_OPTIONS_CONSOLE, iLanguage);
hglb = LoadResource(hInst, hrsrc);
lpsz = LockResource(hglb);
hDlgPanelConsole = CreateDialogIndirectW(GetModuleHandleW(NULL), lpsz, hDlgOptions, (DLGPROC)PanelConsole);
//Await the animation window for displaying TreeView and Panel
SetTimer(hDlgOptions, IDT_TIMER_PANEL, 300, (TIMERPROC) NULL);
//TRY TO SET DEFAUT ID TO TREEVIEW
//SendMessage(hDlgOptions, DM_SETDEFID, IDC_TREE, 0);
//TRY TO FOCUS ON TREEVIEW
//SendMessage(hDlgOptions, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlgOptions, IDC_TREE), TRUE);
//INIT CONTROLS
if (bSaveConfig == TRUE)
{
EnableOptionsControls(hDlgPanelConsole);
SendMessage(GetDlgItem(hDlgOptions, IDC_CHECK_SAVE_CONFIG), BM_SETCHECK, BST_CHECKED, 0);
}
else
{
DisableOptionsControls(hDlgPanelConsole);
}
return TRUE;
}
case WM_NOTIFY:
{
switch (LOWORD(wParam))
{
case IDC_TREE:
NotifyTreeView(hDlgOptions, lParam, wParam);
break;
}
return TRUE;
}
case WM_MOVE:
{
//MAYBE ONLY USE WM_MOVING
UINT xPos = (UINT)LOWORD(lParam);
UINT yPos = (UINT)HIWORD(lParam);
SetWindowPos(hDlgPanelGeneral, HWND_TOP, xPos + 162, yPos + 11, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
SetWindowPos(hDlgPanelConsole, HWND_TOP, xPos + 162, yPos + 11, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
switch (iPanelSelect)
{
case 1: ShowWindow(hDlgPanelGeneral, SW_SHOW); break;
case 2: ShowWindow(hDlgPanelConsole, SW_SHOW); break;
}
return TRUE;
}
case WM_MOVING:
{
//MAYBE BEST MOVING PANEL WITH THE DIALOG
//BUT WIN7 HIDE THE CONTENT...
//NOW HIDE WHEN MOVING
ShowWindow(hDlgPanelGeneral, SW_HIDE);
ShowWindow(hDlgPanelConsole, SW_HIDE);
return TRUE;
}
case WM_TIMER:
{
switch (wParam)
{
case IDT_TIMER_PANEL:
//LOAD TREEVIEW
InitTreeView(hDlgOptions);
KillTimer(hDlgOptions, IDT_TIMER_PANEL);
}
return FALSE;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case ID_BUTTON_SAVE:
case IDOK:
case IDCANCEL:
{
EndDialog(hDlgOptions, LOWORD(wParam));
return TRUE;
}
break;
}
}
}
return FALSE;
}
void EnableOptionsControls(HWND hDlgOptions)
{
EnableWindow(GetDlgItem(hDlgOptions, IDC_CHECK_TC), TRUE);
EnableWindow(GetDlgItem(hDlgOptions, IDC_COMBO_CONSOLE_COLOR), TRUE);
}
void DisableOptionsControls(HWND hDlgOptions)
{
EnableWindow(GetDlgItem(hDlgOptions, IDC_CHECK_TC), FALSE);
EnableWindow(GetDlgItem(hDlgOptions, IDC_COMBO_CONSOLE_COLOR), FALSE);
}
void InitTreeView(HWND hDlgOptions)
{
TV_INSERTSTRUCT tviis;
HTREEITEM hitem;
HIMAGELIST hImageList;
HWND hTreeView;
hTreeView = GetDlgItem(hDlgOptions, IDC_TREE);
hImageList = ImageList_LoadImage(hInst, MAKEINTRESOURCE(IDB_BITMAP_TV), 16, 16,
CLR_DEFAULT, IMAGE_BITMAP, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_DEFAULTCOLOR);
TreeView_SetImageList(hTreeView, hImageList, TVSIL_NORMAL);
ZeroMemory(&(tviis.item), sizeof(TV_ITEM));
tviis.hInsertAfter = TVI_LAST;
tviis.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
tviis.hParent = TVI_ROOT;
tviis.item.iImage = 0;
tviis.item.iSelectedImage = 1;
tviis.item.lParam = 1;
tviis.item.pszText = L"General";
hitem = TreeView_InsertItem(hTreeView, &tviis);
tviis.hParent = hitem;
tviis.item.iImage = 2;
tviis.item.iSelectedImage = 2;
tviis.item.lParam = 3;
tviis.item.pszText = L"Dialog";
TreeView_InsertItem(hTreeView, &tviis);
tviis.item.iImage = 3;
tviis.item.iSelectedImage = 3;
tviis.item.lParam = 4;
tviis.item.pszText = L"Color";
TreeView_InsertItem(hTreeView, &tviis);
//SELECT THE FIRST ITEM
TreeView_SelectItem(hTreeView, hitem);
TreeView_Expand(hTreeView, hitem, TVE_EXPAND);
tviis.hParent = TVI_ROOT;
tviis.item.iImage = 0;
tviis.item.iSelectedImage = 1;
tviis.item.lParam = 2;
tviis.item.pszText = L"Console";
hitem = TreeView_InsertItem(hTreeView, &tviis);
tviis.hParent = hitem;
tviis.item.iImage = 3;
tviis.item.iSelectedImage = 3;
tviis.item.lParam = 5;
tviis.item.pszText = L"Color";
TreeView_InsertItem(hTreeView, &tviis);
tviis.item.iSelectedImage = 2;
tviis.item.iImage = 2;
tviis.item.lParam = 6;
tviis.item.pszText = L"Action";
TreeView_InsertItem(hTreeView, &tviis);
}
void NotifyTreeView(HWND hDlgOptions, LPARAM lParam, WPARAM wParam)
{
LPNMTREEVIEW pnm = (LPNMTREEVIEW)lParam;
//NOTIFY WHEN SELECTION IS CHANGE
if (((LPNMHDR)lParam)->code == TVN_SELCHANGED)
{
RECT wRECT;
GetWindowRect(hDlgOptions, &wRECT);
TVITEM tvi;
HTREEITEM Selected = NULL;
WCHAR Text[255] = { 0 };
Selected = TreeView_GetSelection(GetDlgItem(hDlgOptions, IDC_TREE));
if (Selected == NULL)
return;
ZeroMemory(&tvi, sizeof(tvi));
tvi.mask = TVIF_TEXT;
tvi.pszText = Text;
tvi.cchTextMax = 256;
tvi.hItem = Selected;
//CHANGE THE PANEL WITH THE SELECT ITEM
if (TreeView_GetItem(GetDlgItem(hDlgOptions, IDC_TREE), &tvi))
{
if (wcscmp(tvi.pszText, L"General") == 0)
{
iPanelSelect = 1;
ShowWindow(hDlgPanelConsole, SW_HIDE);
SetWindowPos(hDlgPanelGeneral, HWND_TOP, wRECT.left + 165, wRECT.top + 36, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
return;
}
if (wcscmp(tvi.pszText, L"Console") == 0)
{
iPanelSelect = 2;
ShowWindow(hDlgPanelGeneral, SW_HIDE);
SetWindowPos(hDlgPanelConsole, HWND_TOP, wRECT.left + 165, wRECT.top + 36, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
return;
}
}
}
}
此代碼不能編譯。請[編輯](http://stackoverflow.com/posts/34393112/edit)你的代碼格式,你會看到爲什麼。 – IInspectable