Visual Studio的MFC向導可以生成基於對話框的程序,而win32向導卻不支持 ,是不是很不給力呢?雖然VAX可以直接插入對話框的代碼段,但還是沒有直接生 成來得爽,下面就動手來改造吧。
本文所做的修改僅在自己的VS裡測試通過,大家修改之前請備份原文件。
那我們需要改什麼呢?看圖吧。
首先,找到"VS安裝目錄\VC\VCWizards\AppWiz\Generic\Application\",如 我電腦上的目錄就是
"D:\Program Files\Microsoft Visual Studio 11.0 \VC\VCWizards\AppWiz\Generic\Application\"
這個目錄下就是Win向導的文件了,有四個文件夾,分別是:
不同語言版本的VS,這些目錄下會有不同名字的文件夾,都是按LCID來命名, 關於LCID,可以參考http://msdn.microsoft.com/en-us/library/ms912047 (WinEmbedded.10).aspx
例如英文版的LCID就是1033,簡體中文的就是2052。
html\1033目錄下有兩個htm文件,AppSettings.htm、和Default.htm,這裡先 修改AppSettings.htm,
我這裡用DW打開AppSettings.htm,然後定位到35行,按下圖插入代碼:
這一段就是定義了頂部的圖片,依樣畫葫蘆,插入的代碼內容如下:
<DIV CLASS="SMALL" ID="WIN_DLG_APP_IMAGE" STYLE="DISPLAY:NONE;"> <IMG CLASS="SMALL" SRC="..\..\Images\Dialog.gif" ALT="" BORDER="0"> </DIV>
這裡我為了偷懶,直接將
"D:\Program Files\Microsoft Visual Studio 11.0 \VC\VCWizards\AppWiz\MFC\Application\images\Dialog.gif"
復制到
"D:\Program Files\Microsoft Visual Studio 11.0 \VC\VCWizards\AppWiz\Generic\Application\images\"目錄下。
接著,定位到197行,按圖插入代碼:
看到這裡懂點html的同學應該知道了吧?這裡就是向導對話框中那幾單選按鈕 。還是和之前一樣,依樣畫葫蘆。插入的代碼內容如下:
<INPUT TYPE="radio" CLASS="Radio" onPropertyChange="InitControls();" NAME="apptype" ID="WIN_DLG_APP" ACCESSKEY="B" TITLE="Creates a dialog-based Windows application. The application files include a <projectname>.cpp file that contains the _tWinMain function, and stdafx.cpp and stdafx.h files that are used to create the precompiled header file."> <DIV CLASS="itemTextRadioB" ID="WIN_DLG_APP_DIV" TITLE="Creates a dialog-based Windows application. The application files include a <projectname>.cpp file that contains the _tWinMain function, and stdafx.cpp and stdafx.h files that are used to create the precompiled header file."> <LABEL FOR="WIN_DLG_APP" ID="WIN_DLG_APP_LABEL">Dialog- <U>b</U>ased windows application</LABEL> </DIV> <BR>
然後再定位到370行,按圖插入代碼:
插入的代碼如下
WIN_DLG_APP.disabled = true;
然後定位到425行,由於我們是要創建基於對話框的的程序,所以沒理由是空 項目。。所以這裡不需要添加空項目的開關;插入的代碼如圖:
插入的代碼如下:
if (WIN_DLG_APP.checked) { SUPPORT_ATL.disabled = false; support_atl_title.disabled = false; }
定位到526行,插入代碼如圖:
插入的代碼內容如下:
if (WIN_DLG_APP.checked) { SUPPORT_ATL.disabled = false; support_atl_title.disabled = false; }
定位到第562行,插入代碼如圖:
插入的代碼如下
WIN_DLG_APP_IMAGE.style.display = 'none';
if (WIN_DLG_APP.checked) { WIN_DLG_APP_IMAGE.style.display = 'block'; return; }
至此第一個文件修改完了。。好長吶。。。
繼續修改第二個文件default.htm,依然是用DW來打開。定位到11行,插入代 碼如圖:
插入的代碼內容:
<SYMBOL NAME="WIN_DLG_APP" TYPE=checkbox VALUE=false></SYMBOL>
定位到342行,插入代碼如圖:
順便吐槽下:微軟的coder原來用的是多層嵌套的if。。看著DT。於是我便改 成了else if的。。看起來舒服。
插入的代碼內容如下:
var L_APPTYPEWIN_DLG_TEXT = "Dialog-based windows application";
if (window.external.FindSymbol("DLL_APP")) { APPTYPE.innerText = L_APPTYPEDLL_TEXT; } else if (window.external.FindSymbol("CONSOLE_APP")) { APPTYPE.innerText = L_APPTYPECON_TEXT; } else if (window.external.FindSymbol("LIB_APP")) { APPTYPE.innerText = L_APPTYPELIB_TEXT; } else if (window.external.FindSymbol("WIN_DLG_APP")) { APPTYPE.innerText = L_APPTYPEWIN_DLG_TEXT; } else { APPTYPE.innerText = L_APPTYPEWIN_TEXT; }
hoho,第二個文件改好了。
此時打開向導就可以看到已經添加好了,剩下的就是修改代碼模板了,繼續吧 。
打開"scripts\1033\default.js",定位到96行,插入代碼如圖:
插入代碼如下:
if (wizard.FindSymbol("CONSOLE_APP")) { strAppType = "CONSOLE"; } else if (wizard.FindSymbol("WIN_APP")) { strAppType = "WIN"; } else if (wizard.FindSymbol("WIN_DLG_APP")) { strAppType = "WIN_DLG"; } else if (wizard.FindSymbol("DLL_APP")) { strAppType = "DLL"; } else if (wizard.FindSymbol("LIB_APP")) { strAppType = "LIB"; }
然後再定位到176行,插入代碼如圖:
插入代碼內容如下:
case "WIN_DLG":
定位到192行,插入代碼如圖:
然後再定位到241行和261行做同樣的修改。又改完一個文件了。。。。
找到"templates\1033"目錄,打開readme.txt,分別定位到22行、73行、88行 分別修改成如下文本:
22行:
[!if WIN_APP || WIN_DLG_APP]
73行:
[!if WIN_APP || WIN_DLG_APP || SUPPORT_MFC]
88行:
[!if WIN_APP || WIN_DLG_APP]
這個文件就完事了,下一個文件resource.h
將6-17行替換成如下代碼:
[!if !WIN_DLG_APP] #define IDS_APP_TITLE 103 [!endif] [!if WIN_APP || WIN_DLG_APP] [!if WIN_APP] #define IDR_MAINFRAME 128 #define IDD_ABOUTBOX 103 #define IDM_ABOUT 104 #define IDM_EXIT 105 #define IDC_[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME] 109 #define IDC_MYICON 2 [!endif] #define IDD_[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME]_DIALOG 102 #define IDI_[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME] 107 #define IDI_SMALL 108
接下來需要先在這個目錄下創建一個文件,名為"CmnHdr.h",這個頭文件的代 碼如下:
#pragma once // Include this header file once per compilation unit /////////////////////////// chHANDLE_DLGMSG Macro ///////////////////////////// // The normal HANDLE_MSG macro in WindowsX.h does not work properly for dialog // boxes because DlgProc returns a BOOL instead of an LRESULT (like // WndProcs). This chHANDLE_DLGMSG macro corrects the problem: #define chHANDLE_DLGMSG(hWnd, message, fn) \ case (message): return (SetDlgMsgResult(hWnd, uMsg, \ HANDLE_##message((hWnd), (wParam), (lParam), (fn)))) //////////////////////// Dialog Box Icon Setting Macro //////////////////////// // Sets the dialog box icons inline void chSETDLGICONS(HWND hWnd, int idi) { SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon((HINSTANCE) GetWindowLongPtr(hWnd, GWLP_HINSTANCE), MAKEINTRESOURCE(idi))); SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon((HINSTANCE) GetWindowLongPtr(hWnd, GWLP_HINSTANCE), MAKEINTRESOURCE(idi))); }
看過《Windows 核心編程》的都應該見過上面這段代碼吧。作用我就不解釋了 。
接著修改root.cpp,這個文件裡就引用到了剛才上一步添加的頭文件,定位到 第4行和第13行,修改成如下代碼:
4行:
[!if WIN_APP || WIN_DLG_APP]
13行:
[!if WIN_APP || SUPPORT_MFC || WIN_DLG_APP]
然後再定位240行,在[!else]之後插入如下代碼:
[!if WIN_DLG_APP] #include "CmnHdr.h" // Global Variables: const TCHAR g_szAppName[] = _T("[!output PROJECT_NAME]"); // The application title text // Forward declarations of functions included in this code module: INT_PTR CALLBACK Dlg_Proc(HWND, UINT, WPARAM, LPARAM); BOOL Dlg_OnInitDialog(HWND, HWND, LPARAM); void Dlg_OnCommand(HWND, int, HWND, UINT); // entry function int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); INT_PTR nResult = DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_[! output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME]_DIALOG), NULL, Dlg_Proc, _ttoi(lpCmdLine)); return nResult; } // // FUNCTION: Dlg_Proc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Message handler for Dialog box // INT_PTR CALLBACK Dlg_Proc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { chHANDLE_DLGMSG(hwndDlg, WM_INITDIALOG, Dlg_OnInitDialog); chHANDLE_DLGMSG(hwndDlg, WM_COMMAND, Dlg_OnCommand); } return FALSE; } // // FUNCTION: Dlg_OnInitDialog(HWND, HWND, LPARAM) // // PURPOSE: Initialize the dialog box. // BOOL Dlg_OnInitDialog(HWND hwndDlg, HWND hwndFocus, LPARAM lParam) { // Sets the dialog box icons chSETDLGICONS(hwndDlg, IDI_[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME]); return TRUE; } // // FUNCTION: Dlg_OnCommand(HWND, int, HWND, UINT) // // PURPOSE: Command message handler for dialog box // void Dlg_OnCommand(HWND hwndDlg, int id, HWND hwndCtl, UINT codeNotify) { switch (id) { case IDOK: case IDCANCEL: EndDialog(hwndDlg, id); break; default:break; } } [!endif]
又一文件改好,繼續下一個文件stdafx.h,將38行-43行的內容替換成如下的 代碼:
[!if DLL_APP || WIN_APP || WIN_DLG_APP] // Windows Header Files: #include <windows.h> [!endif] [!if WIN_DLG_APP] // Message Crackers, Child Control Macros, and API Macros Header file: #include <Windowsx.h> [!endif] [!if WIN_APP || WIN_DLG_APP]
然後在文件末尾加上下面這段代碼:
#ifdef _UNICODE #if defined _M_IX86 #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") #elif defined _M_X64 #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"") #else #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") #endif #endif
目的是為了在UNICODE環境下支持XP/Vista/Win7的樣式.
然後再修改下一個文件,Templates.inf,將13行、17行、24行修改成如下代 碼:
13行:
[!if WIN_APP || WIN_DLG_APP || SUPPORT_MFC]
17行:
[!if WIN_APP || WIN_DLG_APP || SUPPORT_MFC]
24行:
[!if WIN_APP || WIN_DLG_APP]
然後再定位18行,在這行後面加上CmnHdr.h,這樣在創建時就會自動把這個文 件復制到工程目錄下。
最後一個要修改的文件,root.rc,這是一個資源腳本文件,為了方便,我調 整了代碼結構,這裡將整個文件的代碼發出來:
//Microsoft Visual C++ generated resource script. // #include "resource.h" #define APSTUDIO_READONLY_SYMBOLS /////////////////////////////////////////////////////////////////////// ////// // // Generated from the TEXTINCLUDE 2 resource. // [!if WIN_APP || WIN_DLG_APP] #ifndef APSTUDIO_INVOKED #include "targetver.h" #endif #define APSTUDIO_HIDDEN_SYMBOLS #include "windows.h" #undef APSTUDIO_HIDDEN_SYMBOLS [!else] #include "afxres.h" [!endif] /////////////////////////////////////////////////////////////////////// ////// #undef APSTUDIO_READONLY_SYMBOLS #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_[!output LANG_SUFFIX]) LANGUAGE [!output PRIMARY_LANG_ID], [!output SUB_LANG_ID] [!if WIN_APP || WIN_DLG_APP] /////////////////////////////////////////////////////////////////////// ////// // // Icon // // Icon with lowest ID value placed first to ensure application icon // remains consistent on all systems. IDI_[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME] ICON "[!output PROJECT_NAME].ico" IDI_SMALL ICON "small.ico" [!if WIN_DLG_APP] /////////////////////////////////////////////////////////////////////// ////// // // DESIGNINFO // #ifdef APSTUDIO_INVOKED GUIDELINES DESIGNINFO BEGIN IDD_[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME]_DIALOG, DIALOG BEGIN LEFTMARGIN, 7 RIGHTMARGIN, 313 TOPMARGIN, 7 BOTTOMMARGIN, 193 END END #endif // APSTUDIO_INVOKED /////////////////////////////////////////////////////////////////////// ////// // // Dialog // IDD_[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME]_DIALOG DIALOGEX 0, 0, 320, 200 STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME EXSTYLE WS_EX_APPWINDOW CAPTION "[!output PROJECT_NAME]" FONT 8, "MS Shell Dlg" BEGIN DEFPUSHBUTTON "OK",IDOK,209,179,50,14 PUSHBUTTON "Cancel",IDCANCEL,263,179,50,14 CTEXT "TODO: Place dialog controls here.",IDC_STATIC,10,96,300,8 END [!else] /////////////////////////////////////////////////////////////////////// ////// // // Menu // IDC_[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME] MENU BEGIN POPUP "&File" BEGIN MENUITEM "E&xit", IDM_EXIT END POPUP "&Help" BEGIN MENUITEM "&About ...", IDM_ABOUT END END /////////////////////////////////////////////////////////////////////// ////// // // Accelerator // IDC_[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME] ACCELERATORS BEGIN "?", IDM_ABOUT, ASCII, ALT "/", IDM_ABOUT, ASCII, ALT END /////////////////////////////////////////////////////////////////////// ////// // // String Table // STRINGTABLE BEGIN IDC_[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME] "[!output UPPER_CASE_SAFE_PROJECT_IDENTIFIER_NAME]" IDS_APP_TITLE "[!output PROJECT_NAME]" END /////////////////////////////////////////////////////////////////////// ////// // // Dialog // IDD_ABOUTBOX DIALOGEX 0, 0, 170, 62 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "About [!output PROJECT_NAME]" FONT [!output DLG_FONT_SIZE], "[!output DLG_FONT_NAME]" BEGIN ICON IDR_MAINFRAME,IDC_STATIC,14,14,21,20 LTEXT "[!output PROJECT_NAME], Version 1.0",IDC_STATIC,42,14,114,8,SS_NOPREFIX LTEXT "Copyright (C) [!output YEAR]",IDC_STATIC,42,26,114,8 DEFPUSHBUTTON "OK",IDOK,113,41,50,14,WS_GROUP END /////////////////////////////////////////////////////////////////////// ////// // // DESIGNINFO // #ifdef APSTUDIO_INVOKED GUIDELINES DESIGNINFO BEGIN IDD_ABOUTBOX, DIALOG BEGIN LEFTMARGIN, 7 RIGHTMARGIN, 163 TOPMARGIN, 7 BOTTOMMARGIN, 55 END END #endif // APSTUDIO_INVOKED [!endif] #ifdef APSTUDIO_INVOKED /////////////////////////////////////////////////////////////////////// ////// // // TEXTINCLUDE // 1 TEXTINCLUDE BEGIN "resource.h\0" END 2 TEXTINCLUDE BEGIN "#ifndef APSTUDIO_INVOKED\r\n" "#include ""targetver.h""\r\n" "#endif\r\n" "#define APSTUDIO_HIDDEN_SYMBOLS\r\n" "#include ""windows.h""\r\n" "#undef APSTUDIO_HIDDEN_SYMBOLS\r\n" "\0" END 3 TEXTINCLUDE BEGIN "\r\n" "\0" END #endif // APSTUDIO_INVOKED [!else] #ifdef APSTUDIO_INVOKED /////////////////////////////////////////////////////////////////////// ////// // // TEXTINCLUDE // 1 TEXTINCLUDE BEGIN "resource.h\0" END 2 TEXTINCLUDE BEGIN "#include ""afxres.h""\r\n" "\0" END 3 TEXTINCLUDE BEGIN "\r\n" "\0" END #endif // APSTUDIO_INVOKED /////////////////////////////////////////////////////////////////////// ////// // // String Table // STRINGTABLE BEGIN IDS_APP_TITLE "[!output PROJECT_NAME]" END [!endif] #endif /////////////////////////////////////////////////////////////////////// ////// #ifndef APSTUDIO_INVOKED /////////////////////////////////////////////////////////////////////// ////// // // Generated from the TEXTINCLUDE 3 resource. // [!if SUPPORT_MFC] #ifndef _AFXDLL #include "[!output MFC_RC_INCLUDE_PREFIX]afxres.rc" #endif [!endif] /////////////////////////////////////////////////////////////////////// ////// #endif // not APSTUDIO_INVOKED
終於修改完成了,趕緊創建一個新項目試試吧。。。
測試截圖:
下面提供兩份已經改好的文件,直接覆蓋即可:
VS2010英文旗艦版:Generic_for_VS10.7z
VS2012英文旗艦版:Generic_for_VS11.7z
下載地址
以上兩份均測試通過,其他版本請自行修改測試。
配套源碼:
http://www.bianceng.net/dotnet/201210/528.htm
原文地址:
http://www.cnblogs.com/hwangbae/archive/2012/07/26/2610469.html