别在MFC了,先分析下,上图
我们以左上角为坐标原点,用position_width和position_height来保存当前显示坐标。
根据msdn说明,滚动条默认情况下的值在0~100之间。
根据图可以知道positon_width的活动范围是0到canvas_width-screen-width,另一边类似。
所以有恒等式1:position_width/(canvas_width-screen_width) = hb_pos/100,其中hb_pos是水平方向滚动条当前值。
滚动块长度公式2:screen_width/canvas_width = 滚动块长度/滚动条可滚动区域长度,滚动条可滚动区域长度大概是screen_width-40差不多,可以设置大写留余地。
下面直接上完整代码,可以运行的,只实现了拖动滚动块事件,其他事件自己补充吧
- #include <windows.h>
-
- #define IDC_CANVAS 200
-
- HWND hwnd_screen = NULL;
- HWND hwnd_canvas = NULL;
- HINSTANCE Ghinstance = NULL;
-
-
- int canvas_width = 2000;
- int canvas_height = 1500;
-
- int screen_width = 0;
- int screen_height = 0;
-
- int position_width = 0;
- int position_height = 0;
-
- int hb_pos = 0;
- int vb_pos = 0;
-
-
- LRESULT CALLBACK ScreenProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
- LRESULT CALLBACK CanvasProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
-
-
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
-
- WNDCLASSEX wc;
- MSG Msg;
-
- memset(&wc,0,sizeof(wc));
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.lpfnWndProc = ScreenProc;
- wc.hInstance = hInstance;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszClassName = "WindowClass";
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
-
- if(!RegisterClassEx(&wc)) {
- MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
- return 0;
- }
-
-
- Ghinstance = hInstance;
- hwnd_screen = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 800,
- 600,
- NULL,NULL,hInstance,NULL);
-
- if(hwnd_screen == NULL) {
- MessageBox(NULL, "Screen Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
- return 0;
- }
-
-
-
- wc;
-
- memset(&wc,0,sizeof(wc));
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.lpszClassName = "Canvas";
- wc.lpfnWndProc = CanvasProc;
- wc.hInstance = Ghinstance;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
-
- if(!RegisterClassEx(&wc)) {
- MessageBox(NULL, "Canvas Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
- return 0;
- }
-
- hwnd_canvas= CreateWindow(
- "Canvas",
- "",
- WS_CHILD | WS_VISIBLE | WS_BORDER,
- 0, 0, canvas_width, canvas_height,
- hwnd_screen,
- (HMENU)IDC_CANVAS,
- Ghinstance,
- 0
- );
-
- if(hwnd_canvas == NULL) {
- MessageBox(NULL, "Canvas Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
- return 0;
- }
-
-
-
- ShowWindow(hwnd_screen, nCmdShow);
- UpdateWindow(hwnd_screen);
-
-
- ShowWindow(hwnd_canvas, SW_SHOW);
- UpdateWindow(hwnd_canvas);
-
-
-
- while(GetMessage(&Msg, NULL, 0, 0) > 0) {
- TranslateMessage(&Msg);
- DispatchMessage(&Msg);
- }
- return Msg.wParam;
- }
-
- LRESULT CALLBACK ScreenProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
-
-
- switch(Message) {
-
-
- case WM_SIZE: {
-
-
- screen_width = LOWORD (lParam);
- screen_height = HIWORD (lParam);
-
-
-
- hb_pos = position_width * 100 / (canvas_width - screen_width);
- vb_pos = position_height * 100 / (canvas_height - screen_height);
-
- SCROLLINFO si;
- si.cbSize=sizeof(SCROLLINFO);
- si.fMask=SIF_POS;
-
- si.nPos = vb_pos;
- SetScrollInfo(hwnd_screen,SB_VERT,&si,true);
-
- si.nPos = hb_pos;
- SetScrollInfo(hwnd_screen,SB_HORZ,&si,true);
-
-
-
-
-
-
-
- break;
- }
-
-
-
- case WM_HSCROLL : {
-
- SCROLLINFO si;
- si.cbSize=sizeof(SCROLLINFO);
- si.fMask=SIF_ALL;
- GetScrollInfo(hwnd_screen,SB_HORZ,&si);
-
- switch(LOWORD(wParam)){
-
-
- case SB_THUMBTRACK: {
- position_width = si.nTrackPos * (canvas_width - screen_width) / 100;
- MoveWindow(hwnd_canvas, 0 - position_width, 0 - position_height, canvas_width, canvas_height, true);
-
- si.nPos=si.nTrackPos;
- break;
- }
-
-
-
- default: {
- break;
- }
- }
-
-
- si.fMask=SIF_POS;
- SetScrollInfo(hwnd_screen, SB_HORZ, &si, true);
-
- break;
- }
-
-
- case WM_VSCROLL : {
-
- SCROLLINFO si;
- si.cbSize=sizeof(SCROLLINFO);
- si.fMask=SIF_ALL;
- GetScrollInfo(hwnd_screen, SB_VERT, &si);
-
- switch(LOWORD(wParam)){
- case SB_THUMBTRACK: {
-
- position_height = si.nTrackPos * (canvas_height - screen_height) / 100;
- MoveWindow(hwnd_canvas, 0 - position_width, 0 - position_height, canvas_width, canvas_height, true);
-
- si.nPos=si.nTrackPos;
- break;
- }
-
- default: {
- break;
- }
- }
-
- si.fMask=SIF_POS;
- SetScrollInfo(hwnd_screen, SB_VERT, &si, true);
-
- break;
- }
-
-
-
- case WM_CLOSE: {
- DestroyWindow(hwnd);
- break;
- }
-
- case WM_DESTROY: {
- PostQuitMessage(0);
- break;
- }
-
- default:
- return DefWindowProc(hwnd, Message, wParam, lParam);
- }
- return 0;
- }
-
- LRESULT CALLBACK CanvasProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
-
- switch(Message) {
-
-
- case WM_PAINT: {
- PAINTSTRUCT ps;
- HDC hdc;
-
- RECT rc;
- GetClientRect(hwnd_canvas, &rc);
-
- hdc = BeginPaint(hwnd_canvas, &ps);
-
- MoveToEx(hdc, 0 , 0 , NULL);
- LineTo(hdc, rc.right, rc.bottom);
-
- EndPaint(hwnd_canvas, &ps);
-
- break;
- }
-
- case WM_CLOSE: {
- DestroyWindow(hwnd);
- break;
- }
-
- case WM_DESTROY: {
- PostQuitMessage(0);
- break;
- }
-
- default:
- return DefWindowProc(hwnd, Message, wParam, lParam);
- }
- }
|
请发表评论