單選按鈕和複選按鈕

#include <windows.h>
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 
HINSTANCE hInst;
 
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
  
    static TCHAR szClassName[] = TEXT("HelloWin"); /*窗口類名*/
    WNDCLASS wndclass;   /*窗口類*/ 
     
    hInst = hInstance;
     
    /***********第一步:註冊窗口類*************/
    /*爲窗口類各個字段賦值*/
    wndclass.style = CS_HREDRAW | CS_VREDRAW;  /*窗口風格*/
    wndclass.lpfnWndProc  = WndProc; /*窗口過程*/
    wndclass.cbClsExtra   = 0;
    wndclass.cbWndExtra   = 0;
    wndclass.hInstance    = hInstance;  /*當前窗口句柄*/
    wndclass.hIcon        = LoadIcon(NULL, IDI_APPLICATION);  /*窗口圖標*/
    wndclass.hCursor      = LoadCursor(NULL, IDC_ARROW);/*鼠標樣式*/
    wndclass.hbrBackground= (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);/*窗口背景畫刷(灰色)*/
    wndclass.lpszMenuName = NULL;  /*窗口菜單*/
    wndclass.lpszClassName= szClassName; /*窗口類名*/
     
     
    /*註冊窗口*/
    RegisterClass(&wndclass);
     
    /*************第二步:建立窗口(並讓窗口顯示出來)***************/
    HWND hwnd = CreateWindow(
        szClassName,  /*窗口名字*/
        TEXT("Welcome"), /*窗口標題*/
        WS_OVERLAPPEDWINDOW,  /*窗口風格*/
        CW_USEDEFAULT,  /*初始化x軸的位置*/
        CW_USEDEFAULT,  /*初始化y軸的位置*/
        500,  /*窗口寬度*/
        300,  /*窗口高度*/
        NULL, /*父窗口句柄*/
        NULL, /*窗口菜單句柄*/
        hInstance, /*當前窗口句柄*/
        NULL    /*不使用該值*/
        );
         
    if(hwnd == NULL)
    {
        MessageBox(NULL, "建立窗口出錯!", "Error", MB_OK);
        return -1;
    }
 
         
    /*顯示窗口*/
    ShowWindow(hwnd, nCmdShow);
    /*更新(繪製)窗口*/
    UpdateWindow(hwnd);
     
     
    /*************第三步:消息循環*************/
    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);  /*翻譯消息*/
        DispatchMessage(&msg);   /*分派消息*/
    }
    return msg.wParam; /*當GetMessage程序返回FALSE是程序結束*/
}
 
/*************第四步:窗口過程*****************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
     
    static HFONT hFont;       // 邏輯字體
    
    //一組單選按鈕 
    static HWND labSex;       //靜態文本框--性別 
    static HWND radioMale;    //單選按鈕--男
    static HWND radioFemale;  //單選按鈕--女
    
    //一組單選按鈕
    static HWND labMarriage;  //靜態文本框--婚姻情況
    static HWND radioMarried; //單選按鈕--已婚
    static HWND radioSingle;  //單選按鈕--未婚
    static HWND radioSecrecy; //單選按鈕--保密
    
    //一組複選框
    static HWND labPet;    //靜態文本框--你的寵物
    static HWND checkboxDog;//複選框--狗
    static HWND checkboxCat;//複選框--貓
    static HWND checkboxFish;//複選框--魚 
    static HWND checkboxOther;//複選框--其餘 
    
    switch(message)
    {
        case WM_CREATE:
        {
            //建立邏輯字體 
            hFont = CreateFont(
                -14/*高度*/, -7/*寬度*/,0,0,400/*通常設值爲400*/,
                FALSE/*不帶斜體*/, FALSE/*不帶下劃線*/,FALSE/*不帶刪除線*/,
                DEFAULT_CHARSET, //使用默認字符集
                OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
                DEFAULT_QUALITY, //默認輸出質量
                FF_DONTCARE, //不指定字體族
                TEXT("微軟雅黑") //字體名
            );
             
            //建立靜態文本框控件--選擇性別 
            labSex = CreateWindow(
                TEXT("static"), TEXT("你的性別:"),
                WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT/*文字水平居右*/,
                10, 10, 80, 26,
                hWnd,
                (HMENU)1,/*控件ID*/
                hInst,
                NULL
            );
            
            radioMale = CreateWindow(
                TEXT("button"),
                TEXT("男"),
                WS_CHILD | WS_VISIBLE | BS_LEFT/*文字居左*/ | BS_AUTORADIOBUTTON/*單選按鈕*/ | WS_GROUP, 
                95, 10, 50, 26,
                hWnd,
                (HMENU)2,
                hInst,
                NULL
            );
            
            radioFemale = CreateWindow(
                TEXT("button"),
                TEXT("女"),
                WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
                150, 10, 50, 26,
                hWnd,
                (HMENU)2,
                hInst,
                NULL 
            );
             
            //選擇婚姻情況 
            labMarriage = CreateWindow(
                TEXT("static"),
                TEXT("婚姻情況:"),
                WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT,
                10, 40, 80, 26,
                hWnd,
                (HMENU)4,
                hInst,
                NULL
            );
            
            radioMarried = CreateWindow(
                TEXT("button"), //按鈕控件類名
                TEXT("已婚"),
                WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON | WS_GROUP,
                95/*X座標*/,40/*Y座標*/, 65/*寬度*/,26/*高度*/,
                hWnd,
                (HMENU)5/*控件惟一標識符*/, 
                hInst,
                NULL 
            );
            
            radioSingle = CreateWindow(
                TEXT("button"),
                TEXT("未婚"),
                WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
                165, 40, 65, 26,
                hWnd,
                (HMENU)6,
                hInst,
                NULL 
            );
            
            radioSecrecy = CreateWindow(
                TEXT("button"),
                TEXT("保密"),
                WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
                235, 40, 100, 26,
                hWnd,
                (HMENU)7,
                hInst,
                NULL 
            );
            
            //你的寵物
            labPet = CreateWindow(
                TEXT("static"),
                TEXT("你的寵物:"),
                WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT,
                10, 70, 80, 26,
                hWnd,
                (HMENU)8,
                hInst,
                NULL 
            );
            
            checkboxDog = CreateWindow(
                TEXT("button"),
                TEXT("狗"),
                WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX/*複選框*/,
                95, 70, 50, 26,
                hWnd,
                (HMENU)9,
                hInst,
                NULL
            );
            
            checkboxCat = CreateWindow(
                TEXT("button"),
                TEXT("貓"),
                WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX,
                150, 70, 50, 26,
                hWnd,
                (HMENU)10,
                hInst,
                NULL
            );
            
            checkboxFish = CreateWindow(
                TEXT("button"),
                TEXT("魚"),
                WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX,
                205, 70, 50, 26,
                hWnd,
                (HMENU)11,
                hInst,
                NULL
            );
            
            checkboxOther = CreateWindow(
                TEXT("button"),
                TEXT("其餘"),
                WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX,
                260, 70, 65, 26,
                hWnd,
                (HMENU)11,
                hInst,
                NULL 
            );
            
            
         
            //依次設置控件字體
            SendMessage(labSex, WM_SETFONT, (WPARAM)hFont, 0);
            SendMessage(radioMale, WM_SETFONT, (WPARAM)hFont, 0);
            SendMessage(radioFemale, WM_SETFONT, (WPARAM)hFont, 0);
            
            SendMessage(labMarriage, WM_SETFONT, (WPARAM)hFont, 0);
            SendMessage(radioMarried, WM_SETFONT, (WPARAM)hFont, 0);
            SendMessage(radioSingle, WM_SETFONT, (WPARAM)hFont, 0);
            SendMessage(radioSecrecy, WM_SETFONT, (WPARAM)hFont, 0);
            
            SendMessage(labPet, WM_SETFONT, (WPARAM)hFont, 0);
            SendMessage(checkboxDog, WM_SETFONT, (WPARAM)hFont, 0);
            SendMessage(checkboxCat, WM_SETFONT, (WPARAM)hFont, 0);
            SendMessage(checkboxFish, WM_SETFONT, (WPARAM)hFont, 0);
            SendMessage(checkboxOther, WM_SETFONT, (WPARAM)hFont, 0);
            
            break;
        }
         
        case WM_PAINT:
        {
            hdc = BeginPaint(hWnd, &ps);
             
            //Ellipse(hdc, 0, 100, 200, 200);
             
            EndPaint(hWnd, &ps);
            break;
        }
             
        /*窗口銷燬消息*/
        case WM_DESTROY:
        {
            /*處理WM_DESTROY消息時刪除以前咱們建立的一切GDI對象*/
            DeleteObject(hFont); //刪除建立的字體
            PostQuitMessage(0);
            break;
        }
        default:
        {
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
    return 0;
}
相關文章
相關標籤/搜索