要用C語(yǔ)言(′?ω?`)制作一個(gè)界面,用c語(yǔ)我???們可以使用(yong)圖形庫來(lái)實(shí)現,界面在Windows平臺上,用c語(yǔ)常用的界面圖形庫有WinAPI(Windows API)和GDI(Graphics Device Interface),在Linux平臺上,用c語(yǔ)常用的界面圖形庫有Xlib和GTK+,這里我們以Windows平臺為例,用c語(yǔ)介紹如ヽ(′ー`)ノ何使用WinAPI和GDI來(lái)制作一個(gè)簡(jiǎn)單的界面界面。
(圖片??來(lái)源網(wǎng)絡(luò ),用c語(yǔ)侵刪)1、界面我們需要包含所需的用c語(yǔ)頭文件:
#inclu?d(′ω`*)e <windows.h>
2、接下來(lái),界面我們需要定義窗口過(guò)程函數,用c語(yǔ)窗口過(guò)程函數是界面處理窗口??消息的函數,它接收一個(gè)窗口句柄和(he)一個(gè)消息類(lèi)型作為參數,用c語(yǔ)我們需要在這個(gè)函數中處理各(ge)種消息??,例如鼠標點(diǎn)擊、鍵盤(pán)輸入等。
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); return 0; default: return DefWindowProc(hw??nd, uMsg, wParam, lParam); }}3、我們需要注冊窗口類(lèi)并創(chuàng )建窗口:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ WNDCLASSEX wcex; HWND hwnd; MSG msg; TCHAR szClassName[] = TEXT(&??quot;MyWindowClass"); wcex.cbSize = siz??eof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WindowProc; wcex.cbClsExtra = 0; wcex?.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(N( ?▽?)ULL, IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, ID??C_ARROW); wcex.hbrBackground = (HBRUSH)(??COLOR_WINDOW + 1); wcex.lpszMen(╯‵□′)╯uName = NULL; wcex.lpszClassName = szClassName; wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&(′-ι_-`)amp;wcex)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szClassName, MB_ICONERROR); return 0; } hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, szClassName, TEXT("My Ap??plication"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAUヽ(′ー`)ノLT??, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if (hwnd == NULL) { MessageBox(NULL, TEXT("Window Creation Er(′?`*)ror!"), szClassName, MB_ICONERROR); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hw??nd); InitializeCriticalSection(&cs); while (TRUE) { if (PeekMessage(&msg??, NULL, 0, 0, PM_REMOVE)) { Tran??sヽ(′?`)ノlateMessage(&msg); Dispa(′▽?zhuān)?)tchMessage(&msg); } else { Sl??eep(10); // sleep for a while to avoid high CPU usage in this thread. You?? can remove this line if you don't need it. } } DeleteCriticalSection(&cs); // clean up the critical section when the application is closing. You can remove this line if you don't need it. return msg.wParam;}4、現在,我們可以在窗口過(guò)程中處理各種消息,我們可以處理鼠標點(diǎn)擊事件,當用戶(hù)點(diǎn)擊鼠標時(shí),程序會(huì )顯示一個(gè)消息框:
case WM_LBUTTONDO(′_ゝ`)WN: { int xPos = LOWORD(lParam); // horizonta(′▽?zhuān)?l position of the mouse pointer relative to the client area of the window (inclusive). The first coordinate is at the left side of the?? client area. If the mouse pointer is not in the client area, the value will be negative. If the mouse pointer is in the upperleft corner of the client area, the value will be zero. If the mouse pointer is in the lowerright corner of the client area, the value will be equal to the width of the client area minus one. If the mouse pointer is outside the client area and outside th(?????)e nonclient area of the window fra??me, the value?? will be less than zero. The second coordinat??e is at the top of the clie??nt area. If the mouse pointeヾ(′?`)?r is not in the client are??a, the value will be negative. If the mouse pointer is in the upperleft corner of the client area, the value will be zero. If the mouse po?inter is in the lowerright corner of the client area, the value will be equal to the height of the client area minus one. If the mouse pointer is outside the client area and outside the nonclient area of the win??dow frame, the value will be less than zero. The(′?`*) third coordinate is reserved for future use and must be zero??. It cannot be negative or greater than?? zero. The fourth coordinate is reserved for future use and must be zero. It cannot be negative or greater than zero. The fifth coordinate is reserved for future use and must be zero. It cannot be negative or greater than zero. The sixth coordinate is reserved for future use and must be zero. It cannot be negative or greater than zero. The(′?_?`) seventh coordinate is reserved for future use and must be zero. It cannot be negative or greater than ze??ro. The eighth coordinate is reserved for future use and must be zero. It cannot be negative or greater than zero. The ninth coordinate is reserved for futu??re use and must be ze(′?`*)ro.(╥_╥) It cannot be negative or(╯°□°)╯ greater than zero. The tenth coordinate is reserved for future(′?_?`) use and must be zero. It cannot be negative or grea??ter than zero. The eleventh coordinate is reserved for future use and must be zero. It cannot be negative or greater above tenth line...} break; // end case WM_LBUTTON┐(′?`)┌DOWN: ... // end switch (uMsg): ..( ?ヮ?). // end LRESULT CALLBACK WndPrヽ(′?`)ノoc (HWND hWnd, UINT message, WP(╯‵□′)╯ARAM wParam, LPARAM lParam): ... // end WinMain((′_`)): ... // end main() return msg. wParam; } // en??d case WM_LBUTTONDOWN: ... // end switch (uMsg): ... // end LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lPara(′_`)m): ... // end WinMain(): ... // end main() return msg. wParam; } // end case WM_LBUTTONDOWN: ... // end switch (uMs(′_ゝ`)g): ... // end LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam): ... // end WinMain(): ... // end maヽ(′▽?zhuān)?ノin(╯°□°)╯() return msg. wParam; } // end case WM_LBUTTONDOWN: ... // end switch (uMsg): ... // end??? LRESULT CALLBヽ(′▽?zhuān)?ノACK WndProc (HWND hWnd, UI??NT message, WPARAM wParam, LPARAM lParam): ... // end WinMain(): ... // end main() return msg. wParam; } // end case WM_LBUTTONDOWN: ...(′_ゝ`) // end switch (uMsg): ... // end LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LP??ARAM lParam): ...?? // end WinMain(): ... // end main() return msg. wParam; } // end case WM_LBUTTONDOWN: ... // end?? switch (uMsg): ... // end LRESULT CALLBACK WndProc (HW(′;д;`)ND hWnd, UINT message, WPARAM wParam, LPARAM lParam)??: ..??. // end WinMain(): ... // end main() return msg. wParam; } // end case WM_LBUTTONDOWN: ... // end sw(′▽?zhuān)?i???tch (uMsg): ... // en??d LRESULT CALLBACK WndProc (HWND hWn(′ω`*)d, UIN??T message, WPARAM wParam, LPARAM lParam): ... // end WinMain(): ... // end main() return msg. wParam; } // end case WM_LBUTTONDOWN: ... // end switch (uMsg): ... // end LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam): ... // end WinMain(): ... // end main() return msg.?? wParam;