为窗体加上滚动条
来源:互联网 发布:淘宝供货商怎么做 编辑:程序博客网 时间:2024/06/07 05:11
为windows32窗体加入滚动条,代码如下:
#include<windows.h>LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter);int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ){static TCHAR szAppName[]=TEXT("AppName");HWND hwnd;MSG msg;WNDCLASS wndclass;wndclass.cbClsExtra=0;wndclass.cbWndExtra=0;wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);wndclass.hInstance=hInstance;wndclass.lpfnWndProc=WindowProc;wndclass.lpszClassName=szAppName;wndclass.lpszMenuName=NULL;wndclass.style=CS_HREDRAW|CS_VREDRAW;if(!RegisterClass(&wndclass)){MessageBox(NULL,TEXT("This program requires Windows NT"),szAppName,MB_ICONERROR);}hwnd=CreateWindow( szAppName, // registered class name TEXT("This is title"), // window name WS_OVERLAPPEDWINDOW|WS_VSCROLL, // window style CW_USEDEFAULT, // horizontal position of window CW_USEDEFAULT, // vertical position of window CW_USEDEFAULT, // window width CW_USEDEFAULT, // window height NULL, // handle to parent or owner window NULL, // menu handle or child identifier hInstance, // handle to application instance NULL // window-creation data);ShowWindow(hwnd,nCmdShow);UpdateWindow(hwnd);while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter){static int cxClient,cyClient,cxChar,cyChar,iVscrollPos;HDC hdc;PAINTSTRUCT ps;int i=50,j=20;TCHAR szBuffer[20];TEXTMETRIC tm;int numberline=30;switch(uMsg){case WM_CREATE://hdc=GetDC(hwnd);//TextOut(hdc,0,0,TEXT("hello,world"),strlen("hello,world"));//ReleaseDC(hwnd,hdc);hdc=BeginPaint(hwnd,&ps);GetTextMetrics(hdc,&tm);SetScrollRange(hwnd,SB_VERT,0,numberline-1,FALSE);SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);cyChar=(tm.tmHeight+tm.tmExternalLeading);EndPaint(hwnd,&ps);return 0;case WM_PAINT:hdc=BeginPaint(hwnd,&ps);GetTextMetrics(hdc,&tm);TextOut(hdc,iVscrollPos,iVscrollPos,szBuffer,wsprintf(szBuffer,TEXT("%d"),tm.tmAveCharWidth));//字符宽度TextOut(hdc,iVscrollPos+30,iVscrollPos+40,szBuffer,wsprintf(szBuffer,TEXT("%d"),tm.tmHeight+tm.tmExternalLeading));//字符长度EndPaint(hwnd,&ps);return 0;case WM_VSCROLL:switch(LOWORD(wParam)){case SB_LINEUP:iVscrollPos-=100;//为了效果更明显由iVscrollPos-=1;改成了iVscrollPos-=100;break;case SB_LINEDOWN:iVscrollPos+=100;break;case SB_PAGEUP:iVscrollPos-=cyClient/cyChar;//cyClient/cyChar表示一页最多几行或一列最多包括多少个字符break;case SB_PAGEDOWN:iVscrollPos+=cyClient/cyChar;break;case SB_THUMBPOSITION:iVscrollPos=HIWORD(wParam);break;default:break;}iVscrollPos=max(0,min(iVscrollPos,numberline-1));if(iVscrollPos!=GetScrollPos(hwnd,SB_VERT)){SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);InvalidateRect(hwnd,NULL,TRUE);}return 0;case WM_SIZE:cyClient=HIWORD(lParam);return 0;/*case WM_SIZE:hdc=BeginPaint(hwnd,&ps);cxClient=LOWORD(lParam);cyClient=HIWORD(lParam);GetTextMetrics(hdc,&tm);TextOut(hdc,0,100,szBuffer,wsprintf(szBuffer,TEXT("%d"),cxClient/tm.tmAveCharWidth));//一行最多显示多少个字符TextOut(hdc,0,150,szBuffer,wsprintf(szBuffer,TEXT("%d"),cyClient/(tm.tmHeight+tm.tmExternalLeading)));//一列最多显示多少字符EndPaint(hwnd,&ps);return 0;*/case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd,uMsg,wParam,lParam);}
截图如下:
- 为窗体加上滚动条
- 为CListBox加上智能水平滚动条
- 为CListBox加上智能水平滚动条
- 为CListBox加上智能水平滚动条
- 为CListBox加上智能水平滚动条
- 为CListBox加上智能水平滚动条
- Qt---为窗体添加滚动条:QScrollArea
- TextView加上滚动条
- 为CListBox加上智能水平滚动条(转)
- 在vs2005中为listbox加上水平滚动条
- VC++------------为CListBox加上智能水平滚动条
- 给TextBlock加上滚动条
- 给控件加上滚动条
- 如何才能为窗体设计一个好的滚动条
- MFC窗体滚动条操作方法
- 给ListBox加上水平滚动条
- CListBox加上水平智能滚动条
- 限定GridView宽度并加上滚动条
- ThinkPHP框架中快捷键M和D的区别
- Beini 的6种攻击模式详解
- C#调用WindowsMediaPlayer控件实现音乐播放(一)
- jodd3.3
- java 编写webservice 接口(xfire) ,调用webservice接口
- 为窗体加上滚动条
- oracle 数据导入不同的表空间-dmp文件
- 硬盘分区表
- 获取字符串的像素长度
- 一个游戏程序员的学习资料
- explicit关键字的使用
- linux c 线程锁
- C#文件后缀名详解
- linux input输入子系统分析《四》:input子系统整体流程全面分析