对话框演示程序2.0

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc     (HWND, UINT, WPARAM, LPARAM);
BOOL    CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);

int iCurrentColor  = IDC_BLACK,
    iCurrentFigure = IDC_RECT;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 
{
    static TCHAR    szAppName[] = TEXT("About2");    
    HWND            hwnd; 
    MSG             msg; 
    WNDCLASS        wndclass;

    wndclass.style          = CS_HREDRAW | CS_VREDRAW ;
    wndclass.lpfnWndProc    = WndProc;
    wndclass.cbClsExtra     = 0;
    wndclass.cbWndExtra     = 0;
    wndclass.hInstance      = hInstance;
    wndclass.hIcon          = LoadIcon(hInstance, szAppName);
    wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName   = szAppName;
    wndclass.lpszClassName  = szAppName;

    if(!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("This program requires Windows NT !"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName, TEXT("About Box Demo Program"),
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, CW_USEDEFAULT,
                        CW_USEDEFAULT, CW_USEDEFAULT,
                        NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg, NULL, 0, 0))
    { 
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
    return msg.wParam;
}

void PaintWindow(HWND hwnd, int iColor, int iFigure)
{
    static COLORREF crColor[8] = {  RGB(0,0,0),        RGB(0,0,255),
                                    RGB(0,255,0),    RGB(0,255,255),
                                    RGB(255,0,0),    RGB(255,0,255),
                                    RGB(255,255,0),    RGB(255,255,255)};
    HBRUSH hBrush;
    HDC    hdc;
    RECT   rect;
    hdc = GetDC(hwnd);
    GetClientRect(hwnd, &rect);
    hBrush=CreateSolidBrush(crColor[iColor-IDC_BLACK]);
    hBrush=(HBRUSH)SelectObject(hdc,hBrush);

    if(iFigure == IDC_RECT)
        Rectangle(hdc,rect.left, rect.top, rect.right, rect.bottom);
    else
        Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom);

    DeleteObject(SelectObject(hdc, hBrush));
    ReleaseDC(hwnd, hdc);
}

void PaintTheBlock(HWND hCtrl, int iColor, int iFigure)
{
    InvalidateRect(hCtrl, NULL, TRUE);
    UpdateWindow(hCtrl);
    PaintWindow(hCtrl, iColor, iFigure);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{
    static HINSTANCE hInstance;
    PAINTSTRUCT      ps;

    switch(message) 
    {
    case WM_CREATE:
        hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
        return 0;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        case IDM_APP_ABOUT:
            if(DialogBox(hInstance, TEXT("AboutBox"), hwnd, AboutDlgProc))
                InvalidateRect(hwnd, NULL, TRUE);
            return 0;
        }
        break;

    case WM_PAINT:
        BeginPaint(hwnd, &ps);
        EndPaint(hwnd, &ps);

        PaintWindow(hwnd, iCurrentColor, iCurrentFigure);
        return 0;

    case WM_DESTROY: 
        PostQuitMessage(0);
        return 0;            
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hCtrlBlock;
    static int  iColor, iFigure;

    switch(message)
    {
    case WM_INITDIALOG:
        iColor = iCurrentColor;
        iFigure = iCurrentFigure;
        CheckRadioButton(hDlg, IDC_BLACK, IDC_WHITE, iColor);
        CheckRadioButton(hDlg, IDC_RECT, IDC_ELLIPSE, iFigure);
        hCtrlBlock = GetDlgItem(hDlg, IDC_PAINT);
        SetFocus(GetDlgItem(hDlg, iColor));
        return FALSE;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        case IDOK:
            iCurrentColor = iColor;
            iCurrentFigure = iFigure;
            EndDialog(hDlg,TRUE);
            return TRUE;

        case IDCANCEL:
            EndDialog(hDlg,FALSE);
            return TRUE;

        case IDC_BLACK:
        case IDC_RED:
        case IDC_GREEN:
        case IDC_YELLOW:
        case IDC_BLUE:
        case IDC_MAGENTA:
        case IDC_CYAN:
        case IDC_WHITE:
            iColor = LOWORD(wParam);
            CheckRadioButton(hDlg, IDC_BLACK, IDC_WHITE, LOWORD(wParam));
            PaintTheBlock(hCtrlBlock, iColor, iFigure);
            return TRUE;

        case IDC_RECT:
        case IDC_ELLIPSE:
            iFigure = LOWORD(wParam);
            CheckRadioButton(hDlg, IDC_RECT, IDC_ELLIPSE, LOWORD(wParam));
            PaintTheBlock(hCtrlBlock, iColor, iFigure);
            return TRUE;
        }
        break;

    case WM_PAINT:
        PaintTheBlock(hCtrlBlock, iColor, iFigure);
        break;
    }
    return FALSE;
}

about2.rc

//Microsoft Developer Studio generated resource script.
//
#include "resource.h"
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

ABOUTBOX DIALOG DISCARDABLE  32, 32, 187, 172
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About Box Demo Program"
FONT 10, "System"
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,25,151,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,115,151,50,14
    ICON            "About2",IDC_STATIC,7,7,20,20
    CTEXT           "About2",IDC_STATIC,70,15,47,12
    CTEXT           "About Box Demo Program",IDC_STATIC,50,27,86,18
    EDITTEXT        IDC_PAINT,114,52,40,41,ES_AUTOHSCROLL
    GROUPBOX        "&Color",IDC_STATIC,16,40,64,102
    RADIOBUTTON     "&Black",IDC_BLACK,30,50,34,10,WS_GROUP | WS_TABSTOP
    RADIOBUTTON     "B&lue",IDC_BLUE,30,62,31,10
    RADIOBUTTON     "&Green",IDC_GREEN,30,74,36,10
    RADIOBUTTON     "Cya&n",IDC_CYAN,30,85,33,10
    RADIOBUTTON     "&Red",IDC_RED,30,96,29,10
    RADIOBUTTON     "&Magenta",IDC_MAGENTA,30,106,44,10
    RADIOBUTTON     "&Yellow",IDC_YELLOW,30,118,38,10
    RADIOBUTTON     "&White",IDC_WHITE,30,130,35,10
    GROUPBOX        "&Figure",IDC_STATIC,108,105,61,36,WS_GROUP
    RADIOBUTTON     "Rec&tangle",IDC_RECT,117,116,49,10,WS_GROUP | WS_TABSTOP
    RADIOBUTTON     "&Ellipse",IDC_ELLIPSE,117,128,39,10
END
/////////////////////////////////////////////////////////////////////////////
//
// Icon
//
ABOUT2                  ICON    DISCARDABLE     "icon1.ico"
/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

ABOUT2 MENU DISCARDABLE 
BEGIN
    POPUP "&Help"
    BEGIN
        MENUITEM "&About",                      IDM_APP_ABOUT
    END
END

resource.h

// Microsoft Developer Studio generated include file.
// Used by about2.rc
//
#define IDC_PAINT                       1000
#define IDC_BLACK                       1001
#define IDC_BLUE                        1002
#define IDC_GREEN                       1003
#define IDC_CYAN                        1004
#define IDC_RED                         1005
#define IDC_MAGENTA                     1006
#define IDC_YELLOW                      1007
#define IDC_WHITE                       1008
#define IDC_RECT                        1009
#define IDC_ELLIPSE                     1010
#define IDM_APP_ABOUT                   40001
#define IDC_STATIC                      -1

icon1.ico

对话框演示程序2.0

上一篇:IDC如何发力精细化运营?


下一篇:公有云服务对比IDC服务,有哪些优势?