• Advertisement

Archived

This topic is now archived and is closed to further replies.

HOW CAN Ý REPAÝNT THE SCREEN in GDI

This topic is 5063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i am trying to repaint the screen in GDI but unfortunately i can''t What is Wrong in This code... NoteMenu item has a Repaint button)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include "resource.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


/////////////////////////////////////CONSTANTS////////////////////////////////////////////////

const char* const WINDOWCAPTION="One of My Windows";
const char* const CLASSNAME="MY CLASS";

//////////////////////////////////////GLOBALS/////////////////////////////////////////////////

int WindowCounter=0;  // Globally tracks Windows

HINSTANCE SparehInstance=NULL; // hInstance of Application


////////////////////////////////////WINPROC//////////////////////////////////////////////////

LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
	PAINTSTRUCT ps;
	switch(msg)
	{
	case WM_CREATE:
		{
			++WindowCounter;
			return 0;
		}
		break;
	case WM_PAINT:
		{
			HDC hdc;
			HBRUSH UsedBrush=CreateSolidBrush(RGB(120,120,120));
			HPEN  UsedPen=CreatePen(PS_SOLID,1,RGB(10,10,10));  
			hdc=BeginPaint(hwnd,&ps);
			GetClientRect(hwnd,&ps.rcPaint);
			Rectangle(hdc,ps.rcPaint.left,ps.rcPaint.top,ps.rcPaint.right,ps.rcPaint.bottom);
			EndPaint(hwnd,&ps);
			ReleaseDC(hwnd,hdc);
			DeleteObject(UsedBrush);
			DeleteObject(UsedPen);
			return 0;
		}
		break;
	case WM_ACTIVATE:
		{
			HDC hdc=GetDC(hwnd);
			char buffer[60];
			SetTextColor(hdc,RGB(220,220,220));
			switch(wparam)
			{
			case WA_ACTIVE:
				{
					sprintf(buffer,"Window is Activated");
					TextOut(hdc,50,50,buffer,strlen(buffer));
					return 0;
				}
				break;
			case WA_CLICKACTIVE:
				{
					sprintf(buffer,"Window is Activated by Mouse");
					TextOut(hdc,50,50,buffer,strlen(buffer));
					return 0;
				}
				break;
			case WA_INACTIVE:
				{
					sprintf(buffer,"Window is Deactivated");
					TextOut(hdc,50,50,buffer,strlen(buffer));
					return 0;
				}
				break;
			}
			return 0;
		}
		break;
	case WM_COMMAND:
		{
			switch(wparam)
			{
			case ID_FILES_EXIT:
				{
					PostQuitMessage(0);
					return 0;
				}
			case ID_FILES_CLOSE:
				{
					PostMessage(hwnd,WM_DESTROY,wparam,lparam);
					return 0;
				}
				break;
			case ID_WINDOW_REPAINT:
				{
					PostMessage(hwnd,WM_PAINT,0,0);
					return 0;
				}
				break;
			}
			return 0;
		}
		break;
	case WM_CLOSE:
		{
			int Answer=0;
			Answer=MessageBox(hwnd,"Are You Sure??","Sure??",MB_YESNO);
			if(Answer==IDYES)
			{
				SendMessage(hwnd,WM_DESTROY,wparam,lparam);
			}
			return 0;
		}
		break;
	case WM_DESTROY:
		{

			--WindowCounter;
			if(WindowCounter<=0)
			{
				PostMessage(hwnd,WM_QUIT,wparam,lparam);
			}
			return 0;
		}
		break;
	}
	return (DefWindowProc(hwnd,msg,wparam,lparam));
}


/////////////////////////////////WINPROC////////////////////////////////////////////////////


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	srand(time(NULL));
	SparehInstance=hInstance;
	WNDCLASSEX myclass;
	MSG msg;
	HWND hwnd;
	myclass.cbClsExtra=0;
	myclass.cbSize=sizeof(WNDCLASSEX);
	myclass.cbWndExtra=0;
	myclass.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
	myclass.hCursor=LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));
	myclass.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
	myclass.hIconSm=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
	myclass.hInstance=hInstance;
	myclass.lpfnWndProc=WinProc;
	myclass.lpszClassName=CLASSNAME;
	myclass.lpszMenuName=NULL;
	myclass.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_DBLCLKS;
	if(!(RegisterClassEx(&myclass)))
	{
		MessageBox(NULL,"Registration Error","Reg.Err",MB_OK);
		return 0;
	}
	if(!(hwnd=CreateWindowEx(NULL,CLASSNAME,WINDOWCAPTION,WS_OVERLAPPEDWINDOW|WS_VSCROLL,
		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL)))
	{
		MessageBox(NULL,"Creation Error","Crt.Err.",MB_OK);
		return 0;
	}
	SetMenu(hwnd,LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1)));
	ShowWindow(hwnd,SW_MAXIMIZE);
	UpdateWindow(hwnd);
	while(true)
	{
		if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE))
		{
			if(msg.message==WM_QUIT)
			{
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return (msg.wParam);
}

Share this post


Link to post
Share on other sites
Advertisement
You need to use the InvalideRect command. This is very useful as you only need to redraw the area of the screen which has changed. The syntax is InvalidateRect(HWND, *RECT, bool). The first is obviously your window, the second is a pointer to a RECT structure which is the area you want to invalidate and the bool states whether or not you want to clear the previous area before redraw.

Worship the holy trinity that is Blitz, Babes and Beers!

Share this post


Link to post
Share on other sites

  • Advertisement