控制元概述:

    控制元是MicroWin 圖形用戶介面的主要組成部分之一,用戶通過操作控制元物件完成與應用程式之間的交互。控制元的使用集中體現了MicroWin 系統对象導向的特點。

 

程式:

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

//程式目的:應用靜態控制元件

//說明:

//    創造一個主視窗,並再主視窗內放置一個靜態控制元件用於顯示圖案,替主視窗設置一個計時器

//    在計時器中完成圖片地替換刷新,實現動畫效果

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

#include "windows.h"

 

#define IDC_STATIC1 100

extern MWIMAGEHDR *imgList_ImgRes[];

 

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

 

int WINAPI WinMain( HINSTANCE hInstance,

                                        HINSTANCE hPrevInstance,

                                        LPSTR     lpCmdLine,

                                        int       nCmdShow)

{

       TCHAR szAppName[]  = "MY_APP_CLASS";

       TCHAR szAppTitle[] = "My APP Title";

        HWND hwnd;

        MSG msg;

        WNDCLASS wndclass;

 

        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(LTGRAY_BRUSH);

        wndclass.lpszMenuName  = NULL;

        wndclass.lpszClassName = szAppName;

 

        RegisterClass(&wndclass);

        hwnd = CreateWindowEx(  0L,

                                    szAppName,                  // 視窗類名

                                    szAppTitle,                   // 視窗標題

                                    WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL,

                                    // 視窗樣式

                                    CW_USEDEFAULT,      // 視窗左上角坐標

                                    CW_USEDEFAULT,

                                     200,                            // 視窗大小

                                     200,

                                     NULL,                          // 父窗體執行緒

                                      NULL,                         // 主選單執行緒

                                      NULL,                         // 實例執行緒

                                      NULL);                        // 視窗參數

 

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

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

 

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

        {

                TranslateMessage(&msg);

                DispatchMessage(&msg);

        }

        return msg.wParam;

}

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

//程式功能:

//        回調函數

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

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

{

        static HWND hStatic;

        static int CurImgId = 0;

      //CreateWindow()和CreateWindowex()都是創造靜態控制元件的函數 

/*hWnd = CreateWindow(“STATIC”,  // 預定義靜態控制元類名

   NULL,              // 靜態控制元一般無標題

   WS_CHILD | WS_VISIBLE | ..., // 靜態控制元樣式

   X, Y,                                 // 靜態控制元在視窗中的位置

   nWidth, nHeight, // 靜態控制元的寬度和高度

   hWndParent, // 擁有該靜態控制元的視窗控制碼

   (HMENU)ID, // 靜態控制元標識

  hInstance, // 應用程式當前實例控制碼,一般為0

  NULL); // 靜態控制元私有資料               */     


                                 

   /*靜態控制元樣式

     SS_LEFT 文字左對齊

     SS_CENTER 文本居中

     SS_RIGHT 文本右對齊

     SS_ICON 在靜態控制元上顯示圖示

     SS_GROUPBOX 使靜態控制元作為一個具有標題的分組框

     SS_LEFTNOWORDWRAP 文本左對齊顯示,並且當文本長度超過靜態控制元寬度時不進行自動換行

      SS_BITMAP 在靜態控制元上顯示位元映射(同SS_ICON)

      SS_NOTIFY 當有用戶輸入(如單擊)時強制使靜態控制元向父視窗發出消息

      SS_CENTERIMAGE 位元映射居中(同時設置SS_BITMAP 或SS_ICON 時有效)

      SS_REALSIZEIMAGE 不對位元映射進行自動縮放    */                                        

  /*注意::SS_LEFT、SS_CENTER、SS_RIGHT、SS_ICON、SS_GROUPBOX、SS_LEFTNOWORDWRAP

    和SS_BITMAP 只能選擇其中一種樣式,不能使用“|”選擇多個。*/                                        

        switch(message)

        {

                case WM_CREATE:

                        hStatic = CreateWindow("STATIC",

                                                            NULL,

                                                           WS_CHILD | WS_VISIBLE | SS_NOTIFY | SS_BITMAP | SS_CENTERIMAGE,

                                                            5, 5,

                                                            90, 90,

                                                            hWnd,

                                                            (HMENU)IDC_STATIC1,

                                                            NULL,

                                                            imgList_ImgRes[CurImgId]);

                                                            SetTimer(hWnd, 0, 80, NULL);

                         break;

                case WM_TIMER:

                        if(++CurImgId >= 7)

                                CurImgId = 0;

                        SendMessage(hStatic, STM_SETIMAGE, (WPARAM)imgList_ImgRes[CurImgId], 0);

                        break;

                case WM_DESTROY:

                        PostQuitMessage(0);

                        break;

                default:

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

        }

        return 0;

}

運行結果:

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

    pixnote21的隨手筆記

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