I develop this program that works fine and draws some figures to the screen:
#include <Windows.h>
#include<windows.h>
#include<iostream>
using namespace std;
int main() {
cin.ignore();
//Get a console handle
HWND myconsole = GetConsoleWindow();
//Get a handle to device context
HDC mydc = GetDC(myconsole);
//Choose any color
COLORREF COLOR= RGB(255,255,255);
HPEN hBluePen = CreatePen(PS_SOLID, 1, COLOR);
HGDIOBJ hPen = SelectObject(mydc, hBluePen);
//Lines
MoveToEx(mydc, 10, 40, NULL);
LineTo(mydc, 44, 10);
LineTo(mydc, 78, 40);
//Rectangles
cin.ignore();
Rectangle(mydc, 16, 36, 72, 70);
Rectangle(mydc, 60, 80, 80, 90);
//Elipse
cin.ignore();
Ellipse(mydc, 40, 55, 48, 65);
ReleaseDC(myconsole, mydc);
cin.ignore();
return 0;
}
But when I resize or minimize the console all the drawed stuff disapear, someone could gave me an example of how this could be fixed?
Related
I'm attempting to draw to an off-screen device context / bitmap and move the image to the main hdc using bitblt. Here's the result I'm currently seeing:
The blue, yellow, and green bars on the left are being drawn directly to the window's hdc. The strange-looking ones on the right were drawn to the back buffer and copied over as a single frame. They should be identical, but clearly that's not the case.
Here's the code I'm using, reduced to a minimal example:
COLORREF color_yellow = RGB (224, 224, 0);
COLORREF color_green = RGB (0, 192, 0);
COLORREF color_blue = RGB (0, 0, 192);
HBRUSH brush_yellow = CreateSolidBrush (color_yellow);
HBRUSH brush_green = CreateSolidBrush (color_green);
HBRUSH brush_blue = CreateSolidBrush (color_blue);
HDC hdc = GetDC (Window);
HDC hdc_buffer = CreateCompatibleDC (hdc);
HBITMAP bitmap_buffer = CreateCompatibleBitmap (hdc_buffer, blit.screen_width, blit.screen_height);
SelectObject (hdc_buffer, bitmap_buffer);
draw_rectangle (hdc, 0, 0, 100, 30, brush_blue);
draw_rectangle (hdc, 0, 30, 100, 60, brush_yellow);
draw_rectangle (hdc, 0, 60, 100, 90, brush_green);
draw_rectangle (hdc_buffer, 0, 0, 100, 30, brush_blue);
draw_rectangle (hdc_buffer, 0, 30, 100, 60, brush_yellow);
draw_rectangle (hdc_buffer, 0, 60, 100, 90, brush_green);
BitBlt (hdc, 120, 0, 100, 90, hdc_buffer, 0, 0, SRCCOPY);
void draw_rectangle (HDC hdc, int left, int top, int right, int bottom, HBRUSH brush)
{
RECT rect;
SetRect (&rect, left, top, right, bottom);
FillRect (hdc, &rect, brush);
}
I'm creating a new hdc (compatible with the window's), creating a compatible bitmap, selecting it, drawing the rectangles, and bit blitting over with SRCCOPY. All of this looks right to me.
I'm sure there's some small thing I'm not doing, but I can't find it.
This is explained in documentation for CreateCompatibleBitmap:
Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the HDC that was used to create the memory device context
Therefore, change
CreateCompatibleBitmap(hdc_buffer, width, height);//monochrome
to
CreateCompatibleBitmap(hdc, width, height);//colored bitmap
I'm using floodfill() and it's not coloring the places I want it to, instead it colors the entire window.
I want the Cyan background inside the rectangle and the magenta under the line line(conx(30) - 2, cony(0)+2, conx(100) + 2, cony(30) - 2); but still within the rectangle boundary.
Here's the code, with relevant libraries included:
#include <iostream>
#include <graphics.h>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
//convert to pixel value (scale of 6)
double conx(double x)
{
return x * (600/100) + 50;
}
double cony(double y)
{
return -y * (600/100) + 650;
}
int main()
{
initwindow(700,700);
rectangle(50, 0, 650, 650);
setfillstyle(SOLID_FILL, CYAN);
floodfill(100, 100, CYAN);
setfillstyle(SOLID_FILL, MAGENTA);
floodfill(620, 620, MAGENTA);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 3);
outtextxy(150, 655, "ELASTIC PARTICLE");
setcolor(15);
setcolor(15);
line(0, 0, 700, 0);
line(50, 0, 50, 650);
line(650, 0, 650, 652);
line(50, 650, 652, 650);
//drawing the line for the wedge/incline
line(conx(30) - 2, cony(0)+2, conx(100) + 2, cony(30) - 2);
//borders
setcolor(15);
line(0, 0, 700, 0);
line(50, 0, 50, 650);
line(650, 0, 650, 652);
line(50, 650, 652, 650);
}
Here's the sample of the output
This is what i Have so far.
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rect);
GetWindowRect(hwnd, &size);
width = size.right - size.left;
itoa(width, Swidth, 10);
height = size.bottom - size.top;
itoa(height, Sheight, 10);
itoa(rect.bottom, sBottom, 10);
itoa(rect.top, sTop, 10);
itoa(rect.left, sLeft, 10);
itoa(rect.right, sRight, 10);
TextOut(hdc, 0, 0, "Here is my width: ", 18);
TextOut(hdc, 125, 0, Swidth, 5);
TextOut(hdc, 175, 0, "Here is my height: ", 18);
TextOut(hdc, 300, 0, Sheight, 4);
TextOut(hdc, 0, 20, sBottom, strlen(sBottom));
TextOut(hdc, 50, 20, sTop, strlen(sTop));
TextOut(hdc, 100, 20, sRight, strlen(sRight));
TextOut(hdc, 150, 20, sLeft, strlen(sLeft));
TextOut(hdc, 0, 40, "Right Button Clicked: ", 23);
itoa(rightButtonClicked, SrightButtonClicked, 10);
TextOut(hdc, 150, 40, SrightButtonClicked, strlen(SrightButtonClicked));
if(rightButtonClicked > 20)
TextOut(hdc, 0, 60, SrightButtonClicked, strlen(SrightButtonClicked));
EndPaint (hwnd, &ps);
return 0;
case WM_LBUTTONDOWN:
return 0;
case WM_RBUTTONDOWN:
rightButtonClicked++;
return 0
Now I'm not sure what I"m doing wrong but I'm supposed to make it output the number of times I right click within the windows. The counter I have for rightButtonClicked incremetns but it won't display properly. Yes this is homework and I have researched the topic quite a bit so I'm looking for a little help.
I'm trying to use an off-screen buffer so that I can keep track of changes to the screen before/after WM_PAINT and just copy them through one line in WM_PAINT. Here's some code I have to set up the graphics:
hdc = GetDC(hWnd);
hdcmem = CreateCompatibleDC(hdc);
hbcmem = CreateCompatibleDC(hdcmem);
// Load bitmaps
bg = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BACKGROUND));
side = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_SIDEINFO));
mainCont = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_GAME_CONT));
if(bg == NULL || side == NULL || mainCont == NULL)
ThrowError("A bitmap failed to load.");
// Background
hdcold = (HBITMAP)SelectObject(hbcmem, bg);
BitBlt(hdcmem, 0, 0, 237, 196, hbcmem, 0, 0, SRCCOPY);
BitBlt(hdcmem, 237, 0, 237, 196, hbcmem, 0, 0, SRCCOPY);
BitBlt(hdcmem, 237 * 2, 0, 237, 196, hbcmem, 0, 0, SRCCOPY);
BitBlt(hdcmem, 0, 196, 237, 196, hbcmem, 0, 0, SRCCOPY);
BitBlt(hdcmem, 237, 196, 237, 196, hbcmem, 0, 0, SRCCOPY);
BitBlt(hdcmem, 237 * 2, 196, 237, 196, hbcmem, 0, 0, SRCCOPY);
// Side Info
hdcold = (HBITMAP)SelectObject(hbcmem, side);
BitBlt(hdcmem, 339, 26, 154, 300, hbcmem, 0, 0, SRCCOPY);
// Main Game Container
hdcold = (HBITMAP)SelectObject(hbcmem, mainCont);
BitBlt(hdcmem, 26, 26, 300, 300, hbcmem, 0, 0, SRCCOPY);
hdc, hdcmem, hbcmem, hdcold, bg, side, and mainCont are declared previously. Their scope includes everything in this file (including this code and the code in WM_PAINT).
Here's the code in WM_PAINT:
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
BitBlt(hdc, 0, 0, 518, 401, hdcmem, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
For some reason, nothing is being drawn to the screen. I'm racking my brain trying to figure it out. A pointer in the right direction would be much appreciated.
Create a compatible bitmap for your memory device context first, then select that bitmap to the memory dc and it should work !
hdc = GetDC(hWnd); // used only to create compatibles.
hdcmem = CreateCompatibleDC(hdc);
hbcmem = CreateCompatibleDC(hdc);
// Create client-area-sized compatible bitmap.
RECT rc;
GetClientRect(hWnd, &rc);
HBITMAP hbm_memdc = CreateComptibleBitmap(hdc, rc.right, rc.bottom);
HBITMAP hbm_memdc_old = (HBITMAP)SelectObject(hdcmem, hbm_memdc)
ReleaseDC(hdc); // this no longer needed
// now start rendering into hdcmem...
You'll want to keep the old bitmap handle selected out to put it back before destroying your custom one on shutdown. How you manage that is entirely up to you.
I'm writing a simple program for testing mouse. It compiles fine, but doesn't work. When I launch it, the window freezes. What am I doing wrong?
#include <SDL/SDL.h>
#undef main
int main()
{
if (SDL_Init (SDL_INIT_EVERYTHING) != 0)
return 1;
SDL_Surface* Scr;
if ((Scr = SDL_SetVideoMode (300, 200, 32, 0)) == 0)
return 2;
SDL_Rect Mouse1 = {50, 50, 50, 100};
SDL_Rect Mouse3 = {150, 50, 50, 100};
SDL_Rect Mouse2 = {250, 50, 50, 100};
SDL_Surface Colors;
SDL_Rect Click = {0, 0, 50, 100};
SDL_Rect NoClick = {50, 0, 50, 100};
SDL_FillRect (Scr, 0, SDL_MapRGB (Scr->format, 255, 255, 255));
SDL_FillRect (&Colors, &Click, SDL_MapRGB (Colors.format, 255, 0, 0));
SDL_FillRect (&Colors, &NoClick, SDL_MapRGB (Colors.format, 0, 0, 255));
while (true)
{
if (SDL_GetMouseState (0, 0) & SDL_BUTTON(1))
SDL_BlitSurface (&Colors, &Click, Scr, &Mouse1);
else
SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse1);
if (SDL_GetMouseState (0, 0) & SDL_BUTTON(2))
SDL_BlitSurface (&Colors, &Click, Scr, &Mouse2);
else
SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse2);
if (SDL_GetMouseState (0, 0) & SDL_BUTTON(3))
SDL_BlitSurface (&Colors, &Click, Scr, &Mouse3);
else
SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse3);
if (SDL_GetKeyState (0) [SDLK_ESCAPE])
return 0;
SDL_Delay (17);
}
}
You are not processing any events. In your case, call SDL_PumpEvents to make SDL process them and update all its internal states:
while (true)
{
SDL_PumpEvents();
// The rest is the same ...
}