《Windows》程式設計之對話方塊3

來源:互聯網
上載者:User
#include <Windows.h>#include "RESOURCE.H"LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);BOOL CALLBACK AboutDlgProc(HWND,UINT,WPARAM,LPARAM);LRESULT CALLBACK EllipPushWndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow){static TCHAR szAppName[]=TEXT("About3");MSG msg;HWND hwnd;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.lpszClassName=szAppName;wndclass.lpszMenuName=szAppName;if(!RegisterClass(&wndclass)){MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);return 0;}wndclass.style=CS_VREDRAW|CS_HREDRAW;wndclass.lpfnWndProc=EllipPushWndProc;wndclass.cbClsExtra=0;wndclass.cbWndExtra=0;wndclass.hInstance=hInstance;wndclass.hIcon=NULL;wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);wndclass.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);wndclass.lpszMenuName=NULL;wndclass.lpszClassName=TEXT("EllipPush");//在WinMain中註冊了EllipPush類,並指定了回呼函數為EllipPushWndProcRegisterClass(&wndclass);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;}LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){static HINSTANCE hInstance;switch(message){case WM_CREATE:hInstance=((LPCREATESTRUCT)lParam)->hInstance;return 0;case WM_COMMAND:switch(LOWORD(wParam)){case IDM_APP_ABOUT:DialogBox(hInstance,TEXT("AboutBox"),hwnd,AboutDlgProc);return 0;}break;case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd,message,wParam,lParam);}BOOL CALLBACK AboutDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam){switch(message){case WM_INITDIALOG:return TRUE;case WM_COMMAND:switch(LOWORD(wParam)){case IDOK:EndDialog(hDlg,0);return TRUE;}break;}return FALSE;}LRESULT CALLBACK EllipPushWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){TCHAR szText[40];HBRUSH hBrush;HDC hdc;PAINTSTRUCT ps;RECT rect;switch(message){case WM_PAINT://這邊這個rect的大小隻針對那個小的ok控制項的GetClientRect(hwnd,&rect);//得到文本,後面用DrawText畫上GetWindowText(hwnd,szText,sizeof(szText));hdc=BeginPaint(hwnd,&ps);//建立畫筆hBrush=CreateSolidBrush(GetSysColor(COLOR_WINDOW));//選入裝置描述表hBrush=(HBRUSH)SelectObject(hdc,hBrush);//設定顏色SetBkColor(hdc,GetSysColor(COLOR_WINDOW));SetTextColor(hdc,GetSysColor(COLOR_WINDOWTEXT));//畫圓Ellipse(hdc,rect.left,rect.top,rect.right,rect.bottom);//寫字DrawText(hdc,szText,-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);DeleteObject(SelectObject(hdc,hBrush));EndPaint(hwnd,&ps);return 0;//下面這個case語句沒有return 0,直接執行到下面觸發SendMessagecase WM_KEYUP:if(wParam!=VK_SPACE)break;case WM_LBUTTONUP://向上一層視窗發訊息,觸發WM_COMMAND下面IDOK,最後一個參數感覺沒有用到,不知道為什麼要傳遞這個SendMessage(GetParent(hwnd),WM_COMMAND,GetWindowLong(hwnd,GWL_ID),(LPARAM)hwnd);return 0;}return DefWindowProc(hwnd,message,wParam,lParam);}//////////////////////////////////////////////////////////////////////////資源中是用到了Custom Control,我寫的代碼,資源檔直接複製的原程式中的。////////////////////////////////////////////////////////////////////////

中午跟老師去喝酒了,下午睡了會,晚上還普通話考試,還好,今天算是寫完了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.