#include<windows.h>
#include<tchar.h>
#include<iostream>
#include"resource.h" //很重要,要引入资源头文件
/*
窗口显示文字,目标:文字随着窗口大小变化变化。文字太多时候,可以通过Scroll滚动条来显示。
思路,在Create中利用GetTextMetric得到系统字体。(需要一次,运行中不会改变)
利用WM_SIZE 空lParam 获得当前系统的窗口大小
将Scroll的Range设置为你文字的行数,
显示的时候根绝Scroll postion 绘图
/*
这里需要说明下InvalidateRect(hWnd,&reClient,TRUE); 函数理解
最后一个参数,TRUE当前无效区域将被背景删除。FALSE不被删除。
背景是我们在设定WNDCLASS时候hBackGround
*/
const int MAX_LINE = 100;
void MessageBoxPrintf(char* pszCaputre,char* Format,...)
{
va_list vaList;//equal to Format + sizeof(FOrmat)
char szBuff[100];
memset(szBuff,0,sizeof(char)*100);
va_start(vaList,Format);
//vsPrintf 三个参数 buff,format,参数数组的指针,va_list类型的。这个函数
// 多用于实现多个参数的自定义函数
_vsnprintf(szBuff,100,Format,vaList);
va_end(vaList);
MessageBoxA(NULL,szBuff,pszCaputre,MB_OK);
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)//LRESULT long型的指针。 CALLBACK _stacall
{
HDC hDc =NULL;
CHAR aszScrollPosMsg[100];
static int iVerScrollPos = 0;
static RECT reClient;
struct tagPAINTSTRUCT ps;
static tagTEXTMETRICA tTestMetric;
static int icxClient = 0;
static int icyClient = 0;
static int icxChar = 0;
static int icyChar = 0;
static int iMaxRow = 0;
static int iMaxColum = 0;
static int iCurRow = 0;
switch(uMsg)
{
case WM_CREATE:
SetScrollRange(hWnd,SB_VERT,0,MAX_LINE - 1,TRUE);
SetScrollPos(hWnd,SB_VERT,0,TRUE);
hDc = GetDC(hWnd);
GetTextMetricsA(hDc,&tTestMetric);
icxChar = tTestMetric.tmAveCharWidth;
icyChar = tTestMetric.tmExternalLeading + tTestMetric.tmHeight;
ReleaseDC(hWnd,hDc);
break;
case WM_SIZE://本来通过GetSystemMetric 获得大小,但是Size消息传递了这些信息。
icxClient = LOWORD(lParam);
icyClient = HIWORD(lParam);
iMaxColum = icxClient / icxChar;
iMaxRow = icyClient / icyChar;
break;
case WM_PAINT:
iCurRow = 0;
hDc = BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&reClient);
for(int i =0;i<MAX_LINE;i++)
{
wsprintfA(aszScrollPosMsg,"the current line is %d ",i);
TextOutA(hDc,0, iCurRow - iVerScrollPos * icyChar ,aszScrollPosMsg,strlen(aszScrollPosMsg));
iCurRow += icyChar;
}
//DrawTextA(hDc,aszScrollPosMsg,-1,&reClient,SND_FILENAME|SND_ASYNC);
EndPaint(hWnd,&ps);
break;
case WM_CLOSE: //消息处理在WM_DESTORY 前面
if(IDOK == MessageBoxA(NULL,"Close","Warning",MB_OKCANCEL))//我们在编程过程中喜欢在if判断时候把常量放在左边,可以避免错误
{
DestroyWindow(hWnd); //发出WM_DESTORY 消息
}
break;
case WM_DESTROY:
PostQuitMessage(0);//使得GetMessage返回0
break;
case WM_VSCROLL:
iVerScrollPos = GetScrollPos(hWnd,SB_VERT);
switch(LOWORD(wParam))
{
case SB_LINEUP: //鼠标点击滚动条上面的按钮
iVerScrollPos -= 1;
break;
case SB_LINEDOWN:
iVerScrollPos += 1;
break;
case SB_THUMBTRACK:
iVerScrollPos = HIWORD(wParam);
break;
case SB_THUMBPOSITION:
iVerScrollPos = HIWORD(wParam);
break;
default:
break;
}
RECT re; //无效区域设置为0,0,0,0 也不会引起TextOut的绘制,加载在BeginPaint和EndPaint中间的绘图代码,windows会自动判断
//是否需要更新绘制,若果在有效区外面不会绘制。
re.bottom = 0;
re.top =0;
re.left =0;
re.right =0;
SetScrollPos(hWnd,SB_VERT,iVerScrollPos,TRUE);
InvalidateRect(hWnd,&reClient,TRUE); //发送WM_PAINT消息,此处不会发送WM_Paint消息,Scroll调用ScrollWindow滚动客户区函数才会发生消息
UpdateWindow(hWnd);
break;
default:
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0;
}
INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpszCmd,int nCmdShow)//win32 的时候hPreInstance总为NULL,不检查,16位时候检查运行可以共用窗口类
{
WNDCLASS wndClass;
HCURSOR hCursor = LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));//load 参数第一个NULL表示在系统内部找,适合第一默认的,系统共有,自己定义就设为自己句柄
HICON hIocn = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));// IDC_CURSOR1 是ID表示转化字符串表示,int 转化为字符串
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor = hCursor;
wndClass.hIcon =hIocn;
wndClass.hInstance = hInstance; //注意一定要赋值为当前进程的hInstance
wndClass.lpfnWndProc = WinProc;
wndClass.lpszClassName=_T("MyWindow");
wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wndClass.style =CS_HREDRAW|CS_VREDRAW;
if(RegisterClass(&wndClass)==NULL)
MessageBoxPrintf("warning","%s","Register");
HMENU hMenue = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));
HWND hWnd = CreateWindowA("MyWindow","mytest",WS_VSCROLL|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500,500,NULL,NULL,hInstance,NULL);
if(hWnd==NULL)
MessageBoxPrintf("warning","%s","create error");
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
MSG msg;
//NULL
// GetMessage retrieves messages for any window that belongs to the calling thread and thread messages posted to the calling thread using the PostThreadMessage function.
while(GetMessage(&msg,NULL,NULL,NULL))//注意GetMessage(&msg,hWnd,NULL,NULL)是不可以的,NULL表示接受自己创建的所有窗口
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
Win32 滚动条显示文本
最新推荐文章于 2024-02-04 02:38:35 发布