VC++ 用MOVETtoEX和Lineto繪製連貫流暢線條
之前學VC++嘗試了使用setpixel函式畫線,結果畫出來的線斷斷續續,一點都不連貫,那麼怎麼使其畫出連貫流暢的線條呢?
效果圖:
對比setpixel()
先來看程式碼:
#include<Windows.h>
#include<tchar.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOLEAN InitWindowsClass(HINSTANCE, int);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if (!InitWindowsClass(hInstance, nCmdShow))
{
MessageBox(NULL, _T("建立視窗失敗!"), _T("建立視窗"), NULL);
return 1;
}
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static bool isDrawPoint = 0;
static int x = 0, y = 0;
static int x1 = 0, y1 = 0;
x = LOWORD(lParam);
y = HIWORD(lParam);
switch (message)
{
case WM_LBUTTONDOWN:
isDrawPoint = true;
x1 = x;
y1 = y;
InvalidateRect(hWnd, NULL, false);
break;
case WM_LBUTTONUP:
isDrawPoint = false;
InvalidateRect(hWnd, NULL, false);
break;
case WM_RBUTTONDOWN:
InvalidateRect(hWnd, NULL, true);
break;
case WM_MOUSEMOVE:
if (isDrawPoint)
{
hdc = GetDC(hWnd);
x = LOWORD(lParam);
y = HIWORD(lParam);
MoveToEx(hdc, x1, y1, NULL);
LineTo(hdc, x, y);
x1 = x;
y1 = y;
ReleaseDC(hWnd,hdc);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
BOOLEAN InitWindowsClass(HINSTANCE hInstance, int nCmdShow)
{
WNDCLASSEX wndclass;
HWND hWnd;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(hInstance, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = _T("視窗1");
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wndclass))
return FALSE;
hWnd = CreateWindow(_T("視窗1"), _T("滑鼠畫線示例"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
核心程式碼:
if (isDrawPoint)
{
hdc = GetDC(hWnd);
x = LOWORD(lParam);
y = HIWORD(lParam);
MoveToEx(hdc, x1, y1, NULL);
LineTo(hdc, x, y);
x1 = x;
y1 = y;
ReleaseDC(hWnd,hdc);
}
break;
我也是初學VC++,第一次知道可以不用再paint中畫圖,直接在move中畫圖即可。
這裡使用類似動態跟蹤的功能,先定下滑鼠左鍵按下的初始點,在move到滑鼠指標現在的位置,接著將lineto的滑鼠座標賦值給初始點,就這樣迴圈往復即可實現不斷的連線。
之所以setpixel函式會出現斷斷續續,我認為可能是由於每一次的重新整理速率跟不上滑鼠位置變化率,當滑鼠飛快在使用者區滑動時,setpixel未能及時捕獲到每一個時刻的座標位點。
但同樣可以定義一個point陣列,例如point[100]={0,0}存放setpixel的值,再加一個判斷條件,當抬起滑鼠時,將陣列裡的元素統統連線,這樣也可以實現畫流暢線條。
如有疑問歡迎交流
相關文章
- canvas 繪製線條Canvas
- JavaScript WebGL 繪製一條直線JavaScriptWeb
- VC++儲存繪製的圖形-1C++
- canvas繪製經典星空連線效果Canvas
- [C#] 在控制檯繪圖, 如:放置影像, 繪製線條C#繪圖
- Python:用tqdm模組繪製進度條Python
- Python 在PDF中繪製線條、矩形、橢圓形Python
- Origin教程:DSC曲線的描述和繪製
- canvas繪製直線Canvas
- SVG 繪製直線SVG
- canvas lineTo()Canvas
- 關於外連線和where條件
- 【PySide6】QChart筆記(一)—— 用QDateTimeAxis作為x軸繪製多條折線圖IDE筆記
- 資料流圖繪製例項
- 用Python繪製移動均線【含原始碼】Python原始碼
- 用Python的random模組繪製折線圖Pythonrandom
- 流動的SVG線條SVG
- Android自定義View之Paint繪製文字和線AndroidViewAI
- amCharts繪製折線圖和柱狀圖混合
- 《暖雪》無限貫日流怎麼玩?無限貫日流玩法分享
- SVG <polyline> 繪製折線SVG
- canvas 繪製雙線技巧Canvas
- PyQtGraph繪製折線圖QT
- AnyChart繪製折線圖
- Matplotlib 繪製折線圖
- MATLAB 繪製折線圖Matlab
- 使用canvas繪製圓形進度條Canvas
- Python連線MySQL的條條大路PythonMySql
- Python 利用pandas和matplotlib繪製柱狀折線圖Python
- caffe的python介面繪製loss和accuracy曲線示例Python
- Movist Pro for Mac中文啟用版:暢享流暢影音盛宴Mac
- 【180620】VC++連連看遊戲原始碼C++遊戲原始碼
- canvas繪製sin正弦曲線Canvas
- mapboxgl繪製3D線3D
- JS的重繪和迴流JS
- 用Python繪製專業的K線圖【含原始碼】Python原始碼
- 前端迴流與重繪:概念及觸發條件前端
- amCharts繪製帶趨勢線折線圖