參考資訊:
http://www.winprog.org/tutorial/
http://winapi.freetechsecrets.com/win32/
https://github.com/gammasoft71/Examples_Win32
http://masm32.com/board/index.php?topic=3584.0
https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles
線的起始點位置是位於(x=0, y=0),使用者可以呼叫MoveToEx()設定新的起始點,而使用LineTo()就可以畫出一條直線,新的起始點則是線的結束位置
main.c
#include <windows.h> HWND hWin = NULL; WNDPROC defWndProc = NULL; LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hdc = NULL; HPEN r_pen = NULL; HPEN g_pen = NULL; HPEN b_pen = NULL; PAINTSTRUCT ps = {0}; switch (uMsg) { case WM_CLOSE: DestroyWindow(hWnd); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); r_pen = CreatePen(PS_SOLID, 3, RGB(0xff, 0x00, 0x00)); g_pen = CreatePen(PS_SOLID, 3, RGB(0x00, 0xff, 0x00)); b_pen = CreatePen(PS_SOLID, 3, RGB(0x00, 0x00, 0xff)); SelectObject(hdc, r_pen); MoveToEx(hdc, 10, 100, NULL); LineTo(hdc, 250, 100); SelectObject(hdc, g_pen); MoveToEx(hdc, 10, 150, NULL); LineTo(hdc, 250, 150); SelectObject(hdc, b_pen); MoveToEx(hdc, 10, 200, NULL); LineTo(hdc, 250, 200); EndPaint(hWnd, &ps); DeleteObject(r_pen); DeleteObject(g_pen); DeleteObject(b_pen); break; } return CallWindowProc(defWndProc, hWnd, uMsg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { hWin = CreateWindow(WC_DIALOG, "main", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, NULL); defWndProc = (WNDPROC)SetWindowLongPtr(hWin, GWLP_WNDPROC, (long int)WndProc); MSG msg = {0}; while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } ExitProcess(0); return 0; }
Line 23~37:產生三支Pen並且畫出三條直線,需要注意的是,同一時間只能選擇一支Pen
編譯、執行
$ winegcc main.c -o main -lgdi32 $ wine ./main.exe