Generic.mak:
這個檔案年代已經久遠,
Generic.h(書中code)。有稍加修改
BOOL InitApplication(HINSTANCE); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
Generic.cpp(書中為.c)
#include <windows.h> #include "resource.h" #include "generic.h" HINSTANCE _hInst; HWND _hWnd; char _szAppName[] = "Generic"; char _szTitle[] = "Generic Sample Application"; int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; UNREFERENCED_PARAMETER(lpCmdLine); if(!hPrevInstance) if(!InitApplication(hInstance) ) return (FALSE); if(!InitInstance(hInstance, nCmdShow)) return (FALSE); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); } BOOL InitApplication(HINSTANCE hInstance) { WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_ BRUSH); wc.lpszMenuName = "GenericMenu"; wc.lpszClassName = _szAppName; return (RegisterClass(&wc)); } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { _hInst = hInstance; _hWnd = CreateWindow( _szAppName, _szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); if(!_hWnd) return (FALSE); ShowWindow(_hWnd, nCmdShow); UpdateWindow(_hWnd); return (TRUE); } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; switch(message){ case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch(wmId){ case IDM_ABOUT: DialogBox(_hInst, "AboutBox", hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return (DefWindowProc(hWnd, message, wParam, lParam)); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return (DefWindowProc(hWnd, message, wParam, lParam)); } return (0); } LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam) ; switch(message){ case WM_INITDIALOG: return (TRUE); case WM_COMMAND: if(LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, TRUE); return (TRUE); } break; } return (FALSE); }
Generic.rc(書中code)。以VS產生之原始檔,
// Microsoft Visual C++ で生成されたリソース スクリプトです。 // #include "windows.h" // 增加這行 #include "resource.h" #define APSTUDIO_READONLY_SYMBOLS //////////////////////////////////////////////////////////// ///////////////// // // TEXTINCLUDE 2 リソースから生成されました。 // #include "afxres.h" ////////////////////////////// ////////////////////////////// ///////////////// #undef APSTUDIO_READONLY_SYMBOLS ////////////////////////////// ////////////////////////////// ///////////////// // 英語 (米国) リソース #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_JPN) LANGUAGE 17, 1 #pragma code_page(932) #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 #endif // 英語 (米国) リソース ////////////////////////////// ////////////////////////////// ///////////////// #ifndef APSTUDIO_INVOKED ////////////////////////////// ////////////////////////////// ///////////////// // // TEXTINCLUDE 3 リソースから生成されました。 // ////////////////////////////// ////////////////////////////// ///////////////// #endif // APSTUDIO_INVOKED でない場合 // 以下為自己加入 GenericMenu MENU DISCARDABLE BEGIN POPUP "&File" BEGIN MENUITEM "&New", IDM_NEW, GRAYED MENUITEM "&Open...", IDM_OPEN, GRAYED MENUITEM "&Save", IDM_SAVE, GRAYED MENUITEM "Save &As...", IDM_SAVEAS, GRAYED MENUITEM SEPARATOR MENUITEM "&Print...", IDM_PRINT, GRAYED MENUITEM "P&rint Setup...", IDM_PRINTSETUP, GRAYED MENUITEM SEPARATOR MENUITEM "E&xit", IDM_EXIT END POPUP "&Edit" BEGIN MENUITEM "&Undo\tCtrl+Z", IDM_UNDO, GRAYED MENUITEM SEPARATOR MENUITEM "Cu&t\tCtrl+X", IDM_CUT, GRAYED MENUITEM "&Copy\tCtrl+C", IDM_COPY, GRAYED MENUITEM "&Paste\tCtrl+V", IDM_PASTE, GRAYED MENUITEM "Paste &Link", IDM_LINK, GRAYED MENUITEM SEPARATOR MENUITEM "Lin&ks...", IDM_LINKS, GRAYED END POPUP "&Help" BEGIN MENUITEM "&Contents", IDM_HELPCONTENTS, GRAYED MENUITEM "&Search for Help on...", IDM_HELPSEARCH, GRAYED MENUITEM "&How to Use Help", IDM_HELPHELP, GRAYED MENUITEM SEPARATOR MENUITEM "&About Generic...", IDM_ABOUT END END AboutBox DIALOG DISCARDABLE 22, 17, 144, 75 STYLE DS_MODALFRAME | WS_CAPTION | WS_SYSMENU CAPTION "About Generic" BEGIN CTEXT "Windows 95", -1, 0, 5, 144, 8 CTEXT "Generic Application", -1, 0, 14, 144, 8 CTEXT "Version 1.0", -1, 0, 34, 144, 8 DEFPUSHBUTTON "OK", IDOK, 53, 59, 32, 14, WS_GROUP END
resource.h 原本應該是rc根據generic.rc生成。但是VS笨笨的,所以還是自己修改吧
//{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. // Used by generic.rc // 自己加入部分START #define IDM_NEW 201 #define IDM_OPEN 202 #define IDM_SAVE 203 #define IDM_SAVEAS 204 #define IDM_PRINT 205 #define IDM_PRINTSETUP 206 #define IDM_EXIT 207 #define IDM_UNDO 208 #define IDM_CUT 209 #define IDM_COPY 210 #define IDM_PASTE 211 #define IDM_LINK 212 #define IDM_LINKS 213 #define IDM_HELPCONTENTS 214 #define IDM_HELPSEARCH 215 #define IDM_HELPHELP 216 #define IDM_ABOUT 217 //#define IDOK 300 //#define IDCANCEL 301 // 自己加入部分END // 新しいオブジェクトの次の既定値 // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 101 #define _APS_NEXT_COMMAND_VALUE 40001 #define _APS_NEXT_CONTROL_VALUE 1001 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif
VS2008,打開一個新的win32程式用新專案,內容用空白
自己把上面這幾個檔案加入/修改之後就能編譯了
理論上,加入generic.rc的時候,resource.h檔案就會被自動生成,可是內容就點點點了
如果編譯出現囉嗦的字眼之類的,應該是編碼問題
把專案的編碼從UNICODE(預設)改成用多字元編碼就沒事了
0 件のコメント:
コメントを投稿