主要是通過調用Windows API界面的CreateWindows函數完畢,同一時候也能創建button,把參數設置為button,假設想響應該button,僅僅需在回調函數中添加消息WM_COMMAND推斷就可以.代碼例如以下:
#include<windows.h> #include<stdio.h> //聲明回調函數 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; //主函數 程序入口 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("HelloWin") ; HWND hwnd ; //用來保存成功創建窗口后返回的句柄 MSG msg ; //定義消息結構體變量 WNDCLASS wndclass ; //窗口類 wndclass.style = CS_HREDRAW | CS_VREDRAW ; //指定窗口風格 wndclass.lpfnWndProc = WndProc ; ////函數指針,指向處理窗口消息的函數入口 wndclass.cbClsExtra = 0 ; //結構體后附加的字節數,一般總為0 wndclass.cbWndExtra = 0 ; //窗口實例附加的字節數,一般總為0 wndclass.hInstance = hInstance ; //模塊句柄 wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; //圖標句柄 任務欄顯示的圖標 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; //光標句柄 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //背景顏色COLOR_BACKGROUND wndclass.lpszMenuName = NULL ; //菜單名的字符串 wndclass.lpszClassName = szAppName ; //自己定義類名,不要與其它類名反復 if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("注冊類失敗!"), szAppName, MB_ICONERROR) ; return 0 ; } int x =((GetSystemMetrics(SM_CXSCREEN)/2)-200); //x居中 int y =((GetSystemMetrics(SM_CYSCREEN)/2)-200); //y居中 //創建窗口API hwnd = CreateWindow(szAppName,TEXT("畢業設計"),WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,x,y,400,400,NULL,NULL,hInstance,NULL); //顯示窗口的API 傳入須要顯示的窗口句柄和顯示方式 ShowWindow(hwnd,iCmdShow); //刷新窗口的API UpdateWindow(hwnd); //從系統的應用程序線程消息隊列中取得一個消息 while(GetMessage(&msg,NULL,0,0) > 0) { DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; //句柄 PAINTSTRUCT ps; RECT rect; //矩形 HINSTANCE hInstance; //窗口實例 static HWND hwndButton[2]; //button句柄 switch (message) { case WM_CREATE: //創建button { hInstance = ((LPCREATESTRUCT)lParam)->hInstance; //button1 hwndButton[0] = CreateWindow("BUTTON","訓練", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10,10,100,100,hwnd,NULL, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); //button2 hwndButton[1] = CreateWindow("BUTTON","獲取", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10,250,100,100,hwnd,NULL, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); return 0; } case WM_PAINT: //繪制文字 hdc = BeginPaint(hwnd,&ps); GetClientRect(hwnd,&rect); DrawText(hdc,TEXT("By:Eastmount CSDN制作"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER); EndPaint(hwnd,&ps); return 0; case WM_COMMAND: //響應button消息 if((HWND)lParam == hwndButton[0]) { MessageBox(NULL,TEXT("是否訓練圖片?"),TEXT("提示"),MB_YESNO|MB_ICONQUESTION); } if((HWND)lParam == hwndButton[1]) { MessageBox(NULL,TEXT("是否獲取圖片?程序執行結果例如以下圖所看到的:"),TEXT("提示"),MB_YESNO|MB_ICONQUESTION); } return 0; case WM_CLOSE: //關閉 if(IDYES==MessageBox(hwnd,"是否關閉程序?","提示",MB_YESNO|MB_ICONQUESTION)) { DestroyWindow (hwnd); } return 0; case WM_DESTROY: //退出程序 PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }


當中主要涉及到的函數原型例如以下所看到的:
//創建窗口 hwnd = CreateWindow ( szClassName, /* Classname */ "Windows App", /* Title Text */ WS_OVERLAPPEDWINDOW, /* default window */ CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* where the window ends up on the screen */ 544, /* The programs width */ 375, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); //創建按鈕 hwndButton = CreateWindow( "BUTTON", // predefined class "OK", // button text WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // styles 10, // starting x position 10, // starting y position 100, // button width 100, // button height hwnd, // parent window NULL, // No menu (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), NULL // pointer not needed );最后,寫這篇文章主要是懷念自己大一時的生活,從一個什么都不知道的孩子,通過學習C語言,C語言能干大事開始接觸編程.同一時候,我覺得這個程序也是很還的入門程序,希望剛接觸程序的同學也能夠看看,編編自己感興趣的程序、寫寫博客、AC題目、編寫游戲、聊天軟件、移動開發,能從程序和生活中找到一些讓自己心靈美妙的東西.
(By:Eastmount 2014-5-25 夜2點半 原創CSDNhttp://blog.csdn.net/eastmount/)