Window C

来源:互联网 发布:ye2系列电机绕组数据书 编辑:程序博客网 时间:2024/06/12 21:47


/*============================================================================ Name        : Exercise.cpp Author      : Haier Version     : 1.01 Copyright   : Copyright (c) 2014 Description : Exercise in C, Ansi-style, Compile by Code:Block ============================================================================*/#include <windows.h>#include <tchar.h>#if !defined LR_SHARED#define LR_SHARED 0x00008000#endifenum{    #if defined IDC_STATIC    #undef IDC_STATIC    #endif // defined    IDC_STATIC=-1,    IDC_ERRNUM=100,    IDC_BTNGO,    IDC_ERRMSG,    IDM_HELP=40000,    IDM_ABOUT};/*===========================================================================*/LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);HACCEL BuildAccelTable(void);void CentreWnd(HWND hwnd);BOOL CALLBACK SetChildFonts(HWND hChild,LPARAM lParam);void ShowErrMsg(HWND hEdit,int errnum);void AddSysMenu(HWND hwnd);void ShowAboutBox(HWND hParent);void ShowHelpBox(HWND hParent);/*===========================================================================*/int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR pStr,int nCmd){    HWND    hwnd;    MSG     msg;    TCHAR   classname[]=_T("kenf's_wnd");    WNDCLASSEX wcx={0};    wcx.cbSize      =sizeof(wcx);    wcx.lpfnWndProc =WndProc;    wcx.hInstance   =hInst;    wcx.hIcon       =(HICON)LoadImage(0,IDI_INFORMATION,IMAGE_ICON,0,0,LR_SHARED);    wcx.hCursor     =(HCURSOR)LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED);    wcx.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);    wcx.lpszClassName=classname;    if(RegisterClassEx(&wcx))    {        hwnd=CreateWindowEx(0,classname,_T("Error Message Tool"),                            WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX&~WS_THICKFRAME,                            0,                            0,                            278,                            178,                            0,0,hInst,0);        if(hwnd)        {            CentreWnd(hwnd);            ShowWindow(hwnd,nCmd);            UpdateWindow(hwnd);            while(GetMessage(&msg,0,0,0)>0)            {                if(!TranslateAccelerator(hwnd,(HACCEL)GetWindowLongPtr(hwnd,GWL_USERDATA),&msg))                {                    if(!IsDialogMessage(hwnd,&msg))                    {                        TranslateMessage(&msg);                        DispatchMessage(&msg);                    }                }            }            return (int)msg.wParam;        }        else        {            MessageBox(0,_T("Wnd creation failure"),_T("Error"),MB_OK|MB_ICONERROR);            return 0;        }    }    MessageBox(0,_T("Wnd registration failure"),_T("Error"),MB_OK|MB_ICONERROR);    return 0;}LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam){    static HWND hEditNum,hBtnGo,hEditMsg;    switch(uMsg)    {    case WM_CREATE:        {            SetWindowLongPtr(hwnd,GWL_USERDATA,(LONG_PTR)BuildAccelTable());            AddSysMenu(hwnd);            hEditNum=CreateWindowEx(WS_EX_CLIENTEDGE,_T("edit"),_T("0"),                                    WS_CHILD|WS_VISIBLE|ES_NUMBER|WS_TABSTOP,                                    56,8,138,28,                                    hwnd,(HMENU)IDC_ERRNUM,GetModuleHandle(0),0);            SendMessage(hEditNum,EM_SETLIMITTEXT,5,0);            CreateWindowEx(0,_T("static"),_T("Value:"),                           WS_VISIBLE|WS_CHILD|SS_CENTERIMAGE,                           8,8,48,28,                           hwnd,(HMENU)IDC_STATIC,GetModuleHandle(0),0);            hBtnGo=CreateWindowEx(0,_T("button"),_T("Show"),                                  WS_VISIBLE|WS_CHILD|WS_TABSTOP,                                  208,8,56,28,                                  hwnd,(HMENU)IDC_BTNGO,GetModuleHandle(0),0);            CreateWindowEx(0,_T("static"),_T("Message:"),                           WS_CHILD|WS_VISIBLE|SS_CENTERIMAGE,                           8,42,48,28,                           hwnd,(HMENU)IDC_STATIC,GetModuleHandle(0),0);            hEditMsg=CreateWindowEx(WS_EX_CLIENTEDGE,_T("edit"),0,                                    WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_READONLY|WS_TABSTOP,                                    8,70,256,68,                                    hwnd,(HMENU)IDC_ERRNUM,GetModuleHandle(0),0);            EnumChildWindows(hwnd,SetChildFonts,0);            SetFocus(hEditNum);            SendMessage(hEditNum,EM_SETSEL,0,-1);            return 0;        }    case WM_COMMAND:        {            WORD wID,wNotify;            wID=LOWORD(wParam);            wNotify=HIWORD(wParam);            if(wNotify==BN_CLICKED)            {                switch(wID)                {                case IDCANCEL:                    {                        DestroyWindow(hwnd);                        return 0;                    }                case IDOK:                    {                    }                case IDC_BTNGO:                    {                        ShowErrMsg(hEditMsg,GetDlgItemInt(hwnd,IDC_ERRNUM,0,0));                        return 0;                    }                }            }            return 0;        }    case WM_DESTROY:        {            DestroyAcceleratorTable((HACCEL)GetWindowLongPtr(hwnd,GWL_USERDATA));            PostQuitMessage(0);            return 0;        }    case WM_SYSCOMMAND:        {            UINT uCmd=(UINT)wParam;            switch(uCmd)            {            case IDM_ABOUT:                {                    ShowAboutBox(hwnd);                    return 0;                }            case IDM_HELP:                {                    ShowHelpBox(hwnd);                    return 0;                }                return DefWindowProc(hwnd,uMsg,wParam,lParam);            }        }      default:            return DefWindowProc(hwnd,uMsg,wParam,lParam);    }}/*===========================================================================*/void AddSysMenu(HWND hwnd){    HMENU hMenu;    hMenu=GetSystemMenu(hwnd,0);    AppendMenu(hMenu,MF_SEPARATOR,0,0);    AppendMenu(hMenu,MF_STRING,IDM_HELP,_T("Help..."));    AppendMenu(hMenu,MF_STRING,IDM_ABOUT,_T("About..."));}/*===========================================================================*/HACCEL BuildAccelTable(void){    ACCEL ac;    ac.fVirt=FVIRTKEY;    ac.key  =VK_F1;    ac.cmd  =IDM_HELP;    return CreateAcceleratorTable(&ac,1);}/*===========================================================================*/void CentreWnd(HWND hwnd){    RECT rc,rcDesk;    SystemParametersInfo(SPI_GETWORKAREA,0,&rcDesk,0);    GetWindowRect(hwnd,&rc);    MoveWindow(hwnd,(rcDesk.right-rc.right+rc.left)/2,               (rcDesk.bottom-rc.bottom+rc.top)/2,               rc.right-rc.left,               rc.bottom-rc.top,0);}/*===========================================================================*/BOOL CALLBACK SetChildFonts(HWND hChild,LPARAM lParam){    SendMessage(hChild,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),                MAKELPARAM(1,0));    return TRUE;}/*===========================================================================*/void ShowAboutBox(HWND hParent){    HWND        hFocus;    int         len;    MSGBOXPARAMS mbp;    TCHAR       *txt;    TCHAR       ab[]=_T("Abot ");    len=lstrlen(ab)+GetWindowTextLength(hParent)+1;    if((txt=(TCHAR*)malloc(len*sizeof(TCHAR)))!=0)    {        lstrcpy(txt,ab);        GetWindowText(hParent,&txt[lstrlen(ab)],len);    }    hFocus=GetFocus();    mbp.cbSize=sizeof(mbp);    mbp.hwndOwner=hParent;    mbp.hInstance=0;    mbp.lpszText =_T("Copyright (c) 2004\n\nKen Fitlike");    if(txt)    {        mbp.lpszCaption=txt;    }    else    {        mbp.lpszCaption=ab;    }    mbp.dwStyle=MB_USERICON;    mbp.lpszIcon=IDI_INFORMATION;    mbp.dwContextHelpId=0;    mbp.lpfnMsgBoxCallback=0;    mbp.dwLanguageId=MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_UK);    MessageBoxIndirect(&mbp);    if(txt)    {        free(txt);    }    SetFocus(hFocus);}void ShowErrMsg(HWND hEdit,int errnum){    HWND hFocus;    void *pMsg;    hFocus=GetFocus();    if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,                     0,                     errnum,                     MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),                     (LPTSTR)&pMsg,                     0,                     NULL)==0)    {        MessageBox(0,_T("There is no message associated\n with that error code."),                   _T("Error"),MB_OK|MB_ICONEXCLAMATION);    }    else    {        SetWindowText(hEdit,(LPTSTR)pMsg);        LocalFree(pMsg);    }    SetFocus(hFocus);}/*===========================================================================*/void ShowHelpBox(HWND hParent){    HWND hFocus;    hFocus=GetFocus();    MessageBox(hParent,               _T("F1\tHelp\nESC\tQuit\n\n")           _T("Enter numerical error code in 'value' field\n")           _T("and click the 'show' button to display any\n")           _T("error message that corresponds to that value.\n"),           _T("Help"),           MB_OK);    SetFocus(hFocus);}


0 0
原创粉丝点击