How to launch a window as a function of the size of the screen? - c++

I am trying to find a solution to launch a window as a function of the size of the screen. I know there is the method resize() of the Gtk::Window but it is only pixels and not percent that's the problem.
Thank you !

You can get the screen width and height in pixels in a quick and dirty way like this:
#include <Windows.h> // Need this to get the screen resolution.
// Get the horizontal and vertical screen sizes in pixels:
static void GetDesktopResolution(int& horizontal, int& vertical) {
SetProcessDPIAware();
horizontal = GetSystemMetrics(SM_CXVIRTUALSCREEN);
vertical = GetSystemMetrics(SM_CYVIRTUALSCREEN);
}
For more advanced functionality, like dealing with multiple monitors, check out the link from the first comment on your question. The answers there are not just for OpenGL.

Related

Fullscreen mode in processing

I'm making a little game in Processing. But I don't know how to set the coordinates to be the same on every monitor type using fullscreen. Like in a big monitor the coordinates are different from a smaller monitor.
I tried to translate the center to the middle of the screen!
Can anyone help me?
It sounds like you're using absolute coordinates, like this:
void setup(){
size(500, 500);
}
void draw(){
ellipse(250, 250, 400, 400);
}
This code draws a large circle in the middle of the screen. The problem is, this code does not consider the size of the window at all. So if you make it fullscreen, the circle keeps the same size and location.
Instead, you can use the width and height variables to make your drawing scale with the window:
void setup(){
size(500, 500);
}
void draw(){
ellipse(width / 2, height / 2, width * .8, height * .8);
}
Now the drawing will scale with the size of the window.
However, now you have a different problem: the drawing can get stretched out depending on the size of your monitor. You need to fix the aspect ratio. I'd recommend Googling that to see a ton of approaches, but for this simple example, you could just take the minimum of width and height and use that:
void setup() {
fullScreen();
}
void draw() {
float minDimension = min(width, height);
ellipse(width / 2, height / 2, minDimension * .8, minDimension * .8);
}
There are a ton of other ways to approach this. You could also look into using a PGraphics that's always the same size, and then scaling that depending on the screen size.
Shameless self-promotion: here and here are tutorials on using the width and height variables to scale your drawings.

Resizable window with resizable circle

I have to make a resizable window with circle inside it,which should also be resizable i.e. if the window is made larger it should become larger and if it is made smaller then it becomes smaller.
I made a simple window using windows.h in c++ which is resizable.Now how should I resize the circle with respect to the window?
One approach I thought that if I get the current size of the window I will be able to adjust the radius accordingly.But....
For getting the current size of window I used GetWindowRect() but it only works when I stretch the window.When I contract it it does not work.
So please figure it out.
Thanks in advance!
You can play around with the Width, Height is not important since we have Width as the radius of the circle.
// Use SetWindows(Width 1~100, Height 1~100) in main()
void SetWindows(int Width,int Height){
_COORD coord;
coord.X=Width;
coord.Y=Height;
_SMALL_RECT Rect;
Rect.Top=0;
Rect.Left=0;
Rect.Bottom=Height-1;
Rect.Right=Width-1; //18
HANDLE Handle=GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle
SetConsoleScreenBufferSize(Handle,coord); // Set Buffer Size
SetConsoleWindowInfo(Handle,TRUE,&Rect); // Set Window Size
DrawCircle(Width);
}
void DrawCircle(int Radius){
// Draw Circle here, radius provided
}

SDL Resetting Surfaces

I draw some text to a surface (using SDL_ttf) and then I want to change the text on the surface. If I just redraw the surface the text does not go away. I have looked at several forum posts on how to fix the problem but I just cannot seem to figure it out. In particular I cannot understand why this solution does not work: (code is long so this just gives the essentials)
In Class file declared:
SDL_Surface* box; // These two are initialised to the
SDL_Surface* boxCopy; // same image
At the start of my render function:
*box = *boxCopy; \\Reset box surface
My understanding of pointers and C++ (which is admittedly limited) suggests that this should make the surface pointed at by box equal to the surface pointed at by boxCopy. Instead the boxCopy surface becomes a copy of box. I have no idea how boxCopy can be changed by this line of code but it seems like that is what is happening.
I'm not sure i completely understand your problem but hopefully this can help.. It's easier to update the text whenever the surface it's drawn on is to be updated rather than updating it whenever the actual text is updated. It might not be as optimized performance wise but i would say it's easier in most cases.
A typical program loop would include a re-rendering of a surface representing the screen followed by an SDL_Flip of this surface. You can of course optimize your re-rendering so you only render what has actually been updated since last frame. Is that what you're working on perhaps? If so, and if you use the method below you should be aware that the new text only covers the size of the new text and not the entire old text. I usually solve this by first drawing a filled rectangle and then the new text.
Here is a TTF example showing how text can be drawn on a surface (here called m_Screen, which is the surface flipped to screen every frame) in the simple case where i have one background color only:
void drawText(const char* string, int x, int y,
int fR, int fG, int fB, int bR, int bG, int bB)
{
SDL_Color foregroundColor = { fR, fG, fB };
SDL_Color backgroundColor = { bR, bG, bB };
SDL_Surface* textSurface = TTF_RenderText_Shaded(m_Font, string,
foregroundColor,
backgroundColor);
SDL_Rect textLocation = { x, y, 0, 0 };
SDL_BlitSurface(textSurface, NULL, m_Screen, &textLocation);
SDL_FreeSurface(textSurface);
}
Notice that this has been done before calling drawText (with some suitable font size):
m_Font = TTF_OpenFont("arial.ttf", size);
And this is done at cleanup:
TTF_CloseFont(m_Font);

Getting center of specific screen in virtual desktop qt

Hey guys i need to get resolution of specific screen in virtual desktop under qt and move window around given screen. I tried
QRect screenSize = desktopWidget.availableGeometry(desktopWidget.screen(ui.monitorNumberComboBox->currentIndex()));
and now when i execute
void MyWindow::setCoordinates(int x, int y)
{
this->move((x-(this->width())/2),(y-(this->height()/2)));
//sets center of window on given coordinates
}
window->setCoordinates(screenSize.width()/2, screenSize.height()/2);
it works great but only for primary screen. Is there a possibility to use it for different screen, selected by index?
I think you need to call:
[..]
QPoint center = screenSize.center(); // Get the center of the screen rect.
window->setCoordinates(center.x(), center.y());

Usage of CScrollView in MFC application

I am using CSCrollView window for our Application in which i have table drawn in View.
I have derived the CMYclass from CSCrollView, But whenevr i am scrolling the window up and down whatever i have drwan is getting erased. How i can acheive it this... i need to perform same actvity like a Word Pad is doing with images and Text. I want to keep scroll the View Vertically. Till the page ends.
Here is the code snippet:-
void CMyView::OnInitialUpdate()
{
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = 450;
sizeTotal.cy = 700;
SetScrollSizes(MM_TEXT, sizeTotal);
}
void CMyView::OnDraw(CDC* pDC)
{
for(int i = 1;i<50;++i)
{
AddRow(pDC);
TopPos = Height+TopPos;// ![Outpu Window Image][1]
nCountRow++;
}
}
it is only drawing 18 rows, but when iam scrolling down above drawn content is no more and also there is nothing coming up in scrolled area.
Is there anything more need to add?
Thanking for help
Regards,
Mukesh
Long time I have used CScrollView. Here is my hint: Try mapping modes other than MM_TEXT. Also look up other functions in CScrollView. I suggest to draw simple stuff first than some complicated rows.