close

文本框控制元簡介

文本框控制元的主要作用在於接收用戶的鍵盤輸入。用戶可在文本框控制元中編輯輸入文本。文本框控制元是MicroWin 應用程式中一種重要的交互工具,也是最常用的交互控制元之一。

程式:

#include "windows.h"

#include "stdio.h"

#include "stdlib.h"

 

// 控件ID

#define IDS_1 101

#define IDS_2 102

#define IDE_RESULT 103

#define IDE_ADDITEM 104

#define IDB_RESET 105

#define IDB_ADD 106

 

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

 

HWND hEditItem, hEditResult, hButtonAdd, hButtonReset, hStatic1, hStatic2;

//====================================================

// 語法格式︰int WINAPI WinMain( HINSTANCE hInstance,

//                                      HINSTANCE hPrevInstance,

//                                      LPSTR     lpCmdLine,

//                                      int       nCmdShow)

// 功能描述: 應用程式入口

// 入口參數: hInstance - 應用程式當前實例句柄

//     hPrevInstance - 應用程式其他實例句柄

//     lpCmdLine - 應用程式命令行參數

//     nCmdShow - 應用程式開始執行時的視窗顯示模式

// 退場門參數:

//====================================================

int WINAPI WinMain( HINSTANCE hInstance,

                                        HINSTANCE hPrevInstance,

                                        LPSTR     lpCmdLine,

                                        int       nCmdShow)

{

       TCHAR szAppName[]  = "Calculator";

       TCHAR szAppTitle[] = "Calculator";

        HWND hwnd;

        MSG msg;

        WNDCLASS wndclass;

 

        MwRegisterEditControl(NULL);                        // 註冊文本框類

        MwRegisterStaticControl(NULL);                     // 註冊靜態文本類

        MwRegisterButtonControl(NULL);                   // 註冊按鈕類

        wndclass.style         = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;

        wndclass.lpfnWndProc   = (WNDPROC)WndProc;

        wndclass.cbClsExtra    = 0;

        wndclass.cbWndExtra    = 0;

        wndclass.hInstance     = hInstance;

        wndclass.hIcon         = 0;

        wndclass.hCursor       = 0;

        wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);

        wndclass.lpszMenuName  = NULL;

        wndclass.lpszClassName = szAppName;

 

        RegisterClass(&wndclass);

        hwnd = CreateWindowEx(  0L,

                                                        szAppName,        // 視窗類名

                                                        szAppTitle,           // 視窗標題

                                                        WS_OVERLAPPEDWINDOW | WS_VISIBLE,           // 視窗樣式

                                                        CW_USEDEFAULT,        // 視窗左上角坐標

                                                        CW_USEDEFAULT,

                                                        280,      // 視窗大小

                                                        200,

                                                        NULL,    // 父窗體句柄

                                                        NULL,    // 主選單句柄

                                                        NULL,    // 實例句柄

                                                        NULL);  // 視窗參數

 

        ShowWindow(hwnd, nCmdShow);             // 設定視窗顯示模式

        UpdateWindow(hwnd);                              // 立即繪製視窗

        while (GetMessage(&msg, NULL, 0, 0))      // 消息循環

        {

                TranslateMessage(&msg);

                DispatchMessage(&msg);

        }

        return msg.wParam;

}

 

//====================================================

//文本框控制元常用樣式及說明 :

//    ES_LEFT 文本框內的文字左對齊

//    ES_CENTER 文本框內的文字居中對齊

//    ES_RIGHT 文本框內的文字右對齊

//    ES_MULTILINE 文本框支援多行編輯(僅對“MEDIT”控制元有效)

//    ES_UPPERCASE 自動將輸入轉換為大寫

//    ES_LOWERCASE 自動將輸入轉換為小寫

//    ES_PASSWORD 將輸入替換成“*”顯示

//    ES_AUTOVSCROLL 當用戶輸入超出視窗時,視窗自動垂直滾動

//    ES_AUTOHSCROLL 當用戶輸入超出視窗時,視窗自動水準滾動

//    ES_NOHIDESEL 當文本框失去焦點時仍保持被選中文本的狀態

//    ES_READONLY 設置文本框唯讀

//    ES_WANTRETURN 設置該屬性後在按下回車時文本框將自動換行,而不是將其做為一個字元處理

//    ES_NUMBER 文本框只接受數位

//注意:ES_CENTER ES_RIGHT 在目前版本的MicroWin 中沒有實現,其效果與ES_LEFT 相同。

//文本框控制元常用通知碼及說明:

//EN_SETFOCUS 文本框獲得焦點

//EN_MAXTEXT 用戶輸入達到允許的最大位元組數
//EN_KILLFOCUS 文本框失去焦點

//EN_HSCROLL 文本框的內容水準滾動
//EN_CHANGE 文本框的內容發生變化

//EN_VSCROLL 文本框的內容垂直滾動
//EN_UPDATE 文本框的內容被更新

//====================================================

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

        int nMax;

        char lpszAddItem[50];

        char lpszResult[50];

        char *stop;

        long nAddItem;

        static long nResult = 1;

        switch(message)

        {

                case WM_CREATE:

                        // 創建各個控件

                        hStatic1 = CreateWindow("STATIC", "Multiplier:", WS_CHILD | WS_VISIBLE, 10, 20, 80, 20, hWnd, (HMENU)IDS_1, 0, NULL);

                        hStatic2 = CreateWindow("STATIC", "Result:", WS_CHILD | WS_VISIBLE, 10, 70, 80, 20, hWnd, (HMENU)IDS_2, 0, NULL);

                        hEditItem = CreateWindow("EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER | ES_LOWERCASE, 130, 20, 80, 20,hWnd, (HMENU)IDE_ADDITEM, 0, NULL);

                        hEditResult = CreateWindow("EDIT", "1", WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER, 130, 70, 80, 20, hWnd, (HMENU)IDE_RESULT, 0, NULL);

                        hButtonAdd = CreateWindow("BUTTON", "X", WS_CHILD | WS_VISIBLE | WS_BORDER | BS_DEFPUSHBUTTON, 50, 120, 70, 20, hWnd, (HMENU)IDB_ADD, 0, NULL);

                        hButtonReset = CreateWindow("BUTTON", "Restart", WS_CHILD | WS_VISIBLE | WS_BORDER, 150, 120, 70, 20, hWnd, (HMENU)IDB_RESET, 0, NULL);

                        break;

                case WM_SETFOCUS:

                        SetFocus(hEditItem);

                        break;

                case WM_COMMAND:

                        switch(LOWORD(wParam))

                        {

                                case IDB_ADD:

                                nMax = GetWindowTextLength(hEditItem) + 1;

                                GetWindowText(hEditItem, lpszAddItem, nMax); // 得到乘數

                                 nAddItem = strtol(lpszAddItem, &stop, 10);    // 轉換為數字

                                 nResult *= nAddItem;                         // 計算累乘結果

                                 sprintf(lpszResult, "%ld", nResult);     // 轉換為字元串

                                 SetWindowText(hEditResult, lpszResult);    // 送結果文本框顯示

                                 SetWindowText(hEditItem, "");           // 清空乘數

                                 SendMessage(hWnd, WM_SETFOCUS, 0, 0);        

                                 // 將焦點移至乘數文本框, 準備下一次輸入

                                   break;

                                case IDB_RESET:

                                        SetWindowText(hEditItem, "");                 // 清空乘數文本框

                                        SetWindowText(hEditResult, "1");            // 重置結果

                                        SendMessage(hWnd, WM_SETFOCUS, 0, 0);         

                                        // 將焦點移至乘數文本框, 準備接受輸入

                                        nResult = 1;

                                        break;

                        }

                        break;

                case WM_DESTROY:

                        PostQuitMessage(0);

                        break;

                default:

                        return DefWindowProc(hWnd, message, wParam, lParam);

        }

        return 0;

}

運行結果:

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 pixnote21 的頭像
    pixnote21

    pixnote21的隨手筆記

    pixnote21 發表在 痞客邦 留言(0) 人氣()