Prev: memory sticks
Next: CreateFile() create time
From: Anees on 27 Jul 2010 10:04 Hi, I am trying to create a tool which horizontally scrolls an image, but invalidaterect is not invalidating image, code is as below: // Win32Ticker.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "Win32Ticker.h" #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_WIN32TICKER, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32TICKER)); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage are 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; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32TICKER)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOWTEXT); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32TICKER); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } int xPos=0; int xInc = 1; int yPos = 320; int yInc = 0; HANDLE hThread=NULL; HANDLE hBitmap=NULL; HANDLE sem=NULL; BOOL Done = FALSE; DWORD WINAPI InvalidateWindowThread(LPVOID args) { HWND *hWnd = (HWND *) args; HDC hdc; hdc=GetWindowDC(*hWnd); while(!Done) { xPos += xInc; if(xPos > 800) xInc = -1; if(xPos < 0) xInc = 1; InvalidateRect(*hWnd, 0, TRUE); UpdateWindow(*hWnd); Sleep(1000); //WaitForSingleObject(sem, INFINITE); } ReleaseDC(*hWnd, hdc); return 1; } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 0, 0, 800, 300, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // 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) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_CREATE: hBitmap = LoadImage(NULL, _T("hello.bmp"), IMAGE_BITMAP, 0, 0,LR_LOADFROMFILE); hThread=CreateThread(NULL, 0, InvalidateWindowThread, (LPVOID) &hWnd, NULL, 0); sem=CreateSemaphore(NULL, 0, 1, NULL); break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: Done=TRUE; DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: { //ScrollWindow(hWnd, xInc, 0, NULL, NULL); /* DWORD ret; while((ret=WaitForSingleObject(sem, 1)) != WAIT_FAILED && ret!=WAIT_TIMEOUT);*/ hdc = BeginPaint(hWnd, &ps); HDC hdcMem = CreateCompatibleDC(hdc); HGDIOBJ oldBitmap = SelectObject(hdcMem, hBitmap); BITMAP bitmap; GetObject(hBitmap, sizeof(bitmap), &bitmap); BitBlt(hdc, xPos, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY); SelectObject(hdcMem, oldBitmap); DeleteDC(hdcMem); EndPaint(hWnd, &ps); /*ReleaseSemaphore(sem, 1, NULL);*/ } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Message handler for about box. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; }
From: Leo Davidson on 27 Jul 2010 15:40 On Jul 27, 3:04 pm, Anees <anees.hai...(a)gmail.com> wrote: > Hi, > > I am trying to create a tool which horizontally scrolls an image, but > invalidaterect is not invalidating image, code is as below: You should (almost) never directly manipulate a HWND or a DC from any other thread than the one which created it. That may be where things are going wrong. Instead, use a timer (SetTimer, WM_TIMER) instead of a thread to do the animation. (If you must use a thread, e.g. because the WM_TIMER messages are low- priority and get delayed when the window is doing other work, then you should have the thread send or post messages to the window so that the window, and the window's thread, still does the updating.)
From: Anees on 28 Jul 2010 02:40 On Jul 28, 12:40 am, Leo Davidson <leonudeldavid...(a)gmail.com> wrote: > On Jul 27, 3:04 pm, Anees <anees.hai...(a)gmail.com> wrote: > > > Hi, > > > I am trying to create a tool which horizontally scrolls an image, but > > invalidaterect is not invalidating image, code is as below: > > You should (almost) never directly manipulate a HWND or a DC from any > other thread than the one which created it. That may be where things > are going wrong. > > Instead, use a timer (SetTimer, WM_TIMER) instead of a thread to do > the animation. > > (If you must use a thread, e.g. because the WM_TIMER messages are low- > priority and get delayed when the window is doing other work, then you > should have the thread send or post messages to the window so that the > window, and the window's thread, still does the updating.) Thank you, The problem was exactly what you mentioned. also thank you for solutions especially for "send or post message" solution :)
|
Pages: 1 Prev: memory sticks Next: CreateFile() create time |