在窗口上设计一行滚动文字

致物软件 2014-05-24 05:09:21
程序要求:在窗口上设计一行文字,要求文字能在窗口中向左滚动显示,而且每显示一轮, 改变一次颜色和字体
目前写到能让文字从右往左滚动显示,但只能滚动一次,无法循环滚动,就无法写后续程序。
我是用一个整形变量right记录字符串末尾的x坐标,如果right等于了屏幕窗口的左端值,就让right回到屏幕窗口的右边,但是为什么循环不起来?
// 实验三.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_MY;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
/*编写程序: 在窗口上设计一行文字,
要求文字能在窗口中向左滚动显示,
而且每显示一轮, 改变一次颜色和字体.*/
static int right,
i = 1;
int wmId, wmEvent,
length; //字符串字节数
PAINTSTRUCT ps;
HDC hdc;
TEXTMETRIC tm;
LPSTR lpsz = "Visual C++面向对象与可视化程序设计";
length = _tcslen(lpsz);
switch (message)
{
case WM_CREATE:
SetTimer(hWnd,9,100,NULL);
break;
case WM_TIMER:
InvalidateRect(hWnd,NULL,true);
i++;
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
GetTextMetrics(hdc,&tm);
right = rt.right-10*i;
if(right==rt.left){
right = rt.right;
i = 0;
}
TextOut(hdc,right-tm.tmAveCharWidth*length,rt.bottom/2,lpsz,length);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
KillTimer(hWnd,9);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
...全文
392 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
张不 2014-11-26
  • 打赏
  • 举报
回复
你的那个resourse.h头文件写的是些什么东西呢
movsd 2014-05-25
  • 打赏
  • 举报
回复

right    = rt.right-10*i;
if(right==rt.left){
    right    = rt.right;
    i    = 0;
}
由于i乘了10,所以right不一定能刚好与rt.left相等,比如rt.right=100, rt.left=15,应该改成小于等于
致物软件 2014-05-25
  • 打赏
  • 举报
回复
引用 3 楼 movsd 的回复:

right    = rt.right-10*i;
if(right==rt.left){
    right    = rt.right;
    i    = 0;
}
由于i乘了10,所以right不一定能刚好与rt.left相等,比如rt.right=100, rt.left=15,应该改成小于等于
嗯嗯,我改成了小于,问题已经解决了
致物软件 2014-05-24
  • 打赏
  • 举报
回复
有没有SDK实现的,我这个是win32 Application程序
ningto.com 2014-05-24
  • 打赏
  • 举报
回复
正好前段时间找了个滚动文字的,很好使,楼主研究下吧 http://download.csdn.net/detail/aidy22/1588643

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧