設置 | 登錄 | 註冊

目前共有10篇帖子。

【程序】用命令行編譯對話框程序

1樓 巨大八爪鱼 2016-9-24 23:47
2樓 巨大八爪鱼 2016-9-24 23:48
先編譯資源文件,再編譯源文件。
【編譯資源文件用的批處理:compile_rc.bat】
@echo off
REM 編譯資源文件
"C:\Program Files (x86)\Windows Kits\8.0\bin\x86\rc.exe" /i "C:\Program Files (x86)\Windows Kits\8.0\Include\um" /i "C:\Program Files (x86)\Windows Kits\8.0\Include\shared" resource.rc
pause
【編譯源文件用的批處理:compile_wnd.bat】
@echo off
path=%path%;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE
"C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\cl.exe" -c wnd.c -I "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include" -I "C:\Program Files (x86)\Windows Kits\8.0\Include\um" -I "C:\Program Files (x86)\Windows Kits\8.0\Include\shared"
"C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\link.exe" wnd.obj resource.res "C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86\uuid.lib" "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib\libcmt.lib" "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib\oldnames.lib" "C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86\kernel32.lib" "C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86\user32.lib"
pause
3樓 巨大八爪鱼 2016-9-24 23:48
【資源頭文件:resource.h】
#define MYICON 101
#define MYMENU 102
#define IDM_OPEN 103
#define IDM_EXIT 104
#define IDM_ABOUT 105
#define IDM_NEW_DAT 106
#define IDM_NEW_TEXT 107

#define MYBITMAP 108
#define MYDIALOG 109


// 資源頭文件的最後一行必須是空行, 否則編譯會出錯

4樓 巨大八爪鱼 2016-9-24 23:48
【資源文件:resource.rc】
#include <Windows.h>
#include "resource.h"

#pragma code_page(936) // 簡體中文

// 位圖
MYBITMAP BITMAP "bmp_example.bmp"

// 圖標
MYICON ICON "shell32_142.ico"

// 菜單
MYMENU MENU
BEGIN
    POPUP "文件(&F)"
    BEGIN
        POPUP "新建(&N)"
        BEGIN
            MENUITEM "數據文件", IDM_NEW_DAT // 必須指定ID
            MENUITEM "文本文件", IDM_NEW_TEXT
        END
        MENUITEM "打開(&O)...\tCtrl+O", IDM_OPEN
        MENUITEM SEPARATOR
        MENUITEM "退出(&E)", IDM_EXIT
    END
   
    POPUP "幫助(&H)"
    BEGIN
        MENUITEM "關於(&A)...", IDM_ABOUT
    END
END

// 對話框
MYDIALOG DIALOGEX 0, 0, 200, 150
STYLE WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_CENTER | DS_MODALFRAME | DS_FIXEDSYS
CAPTION "示例對話框"
BEGIN
    DEFPUSHBUTTON "確定", IDOK, 100, 14, 32, 14, WS_GROUP
END
5樓 巨大八爪鱼 2016-9-24 23:49
【源文件:wnd.c】
#include <tchar.h>
#include <Windows.h>
#include "resource.h"

INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    HICON hIcon, hIconSm;
    HMENU hMenu;
    int wmId;
   
    switch (uMsg)
    {
    case WM_COMMAND:
        wmId = LOWORD(wParam);
        switch (wmId)
        {
        case IDOK:
            MessageBox(hDlg, TEXT("Hello World!"), TEXT("Hello"), MB_OK);
            break;
        case IDCANCEL:
            EndDialog(hDlg, wmId);
            break;
        case IDM_OPEN:
            MessageBox(hDlg, TEXT("Cannot open!"), TEXT("Error"), MB_ICONERROR);
            break;
        }
        break;
    case WM_INITDIALOG:
        hIcon = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(MYICON), IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), (UINT)NULL);
        hIconSm = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(MYICON), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), (UINT)NULL);
        SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
        SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
       
        hMenu = LoadMenu(GetModuleHandle(NULL), MAKEINTRESOURCE(MYMENU));
        SetMenu(hDlg, hMenu);
        break;
    }
    return FALSE;
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    DialogBox(hInstance, MAKEINTRESOURCE(MYDIALOG), NULL, DlgProc);
    return 0;
}
巨大八爪鱼NULL在C語言裏面特指「空指針」,而不是其他編程語言裏的空值。所以(UINT)NULL這種寫法嚴格來說是不符合規範的,最好直接寫成0。
6樓 巨大八爪鱼 2016-9-24 23:49
【運行結果】

7樓 巨大八爪鱼 2016-9-25 22:49
啟用XP風格控件的方法很簡單。
在rc文件中加入:1 24 "24.txt"

然後創建一個24.txt的文本文件,內容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
    version="1.0.0.0"
    processorArchitecture="*"
    name="CompanyName.ProductName.YourApplication"
    type="win32"
/>
<description>Your application description here.</description>
<dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="*"
            publicKeyToken="6595b64144ccf1df"
            language="*"
        />
    </dependentAssembly>
</dependency>
</assembly>
8樓 巨大八爪鱼 2016-9-25 22:50
重新編譯資源和程序,在Win7下已經可以看到XP風格的控件了:

9樓 巨大八爪鱼 2016-9-25 22:54
對於更早的系統版本,則需要執行InitCommonControls函數初始化控件。
方法如下:
打開wnd.c文件,在#include <Windows.h>下加入#include <CommCtrl.h>,然後在DialogBox函數前加上InitCommonControls();,保存文件。
然後打開compile_wnd.bat文件,在link.exe那一行的最後加上 "C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86\comctl32.lib",注意引號前有空格。
最後運行compile_wnd.bat文件編譯程序。

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。