473,378 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Dynamic GUI

compman9902
105 100+
People, first of all thank you for reading my post.
I would like to start off by saying that I have read Prorgramming Windows and I have found no information that contributes to what I need to do.
Here is what I need help with. I need to make a windown with text in the middle of it that displays "Percent: %X" X being a intiger that I submit to the function that makes thew window.
Basicaly, a funtion that when I send a intiger to it, it opens up and then displays what is above. Then, it just dosn't close after it initially opens.
I really need help on this.
Thank you.
Sep 10 '07 #1
22 3000
weaknessforcats
9,208 Expert Mod 8TB
And you can't build a string containing your display and just use TextOut() ??
Sep 10 '07 #2
Studlyami
464 Expert 256MB
I'm not quite sure i fully understand the question. You need help closing the winodw? or creating the window? or centering the text?
Sep 10 '07 #3
compman9902
105 100+
I'm not quite sure i fully understand the question. You need help closing the winodw? or creating the window? or centering the text?
All of the above
And then some
Sep 10 '07 #4
kreagan
153 100+
All of the above
And then some
Would message pop up work for you?
MessageBox
Sep 10 '07 #5
Studlyami
464 Expert 256MB
Message box is nice and it provides and OK which will close it when clicked.

As for centering text here are the basic steps needed.


Get total window width and height (windowrect.right - windowrect.left; windowrect.bottom - windowrect.top)

divided total height and width by 2 (you want the center of the window).

Now we have the center of the window we need to subtract half the size of the string width and half of the string height.

Use GetTextExtentPoint32 and divide that number by 2 that will be your x,y value for drawing the font. You'll need to look up the parameters for GetTextExtentPoint32 i can never remember exactly how it works
Sep 10 '07 #6
compman9902
105 100+
Message box is nice and it provides and OK which will close it when clicked.

As for centering text here are the basic steps needed.


Get total window width and height (windowrect.right - windowrect.left; windowrect.bottom - windowrect.top)

divided total height and width by 2 (you want the center of the window).

Now we have the center of the window we need to subtract half the size of the string width and half of the string height.

Use GetTextExtentPoint32 and divide that number by 2 that will be your x,y value for drawing the font. You'll need to look up the parameters for GetTextExtentPoint32 i can never remember exactly how it works
It would only work if the window would close automatically when a new number was submitted to the function and then opened up another with the submitted number.
Actually.
The above sounds perfect.
If you can, please post code.
Sep 10 '07 #7
Studlyami
464 Expert 256MB
Here is the basic way of centering the text on a given window. I was unable to get the dynamic window to work, but im sure a couple of the other people can help you out with that.

Expand|Select|Wrap|Line Numbers
  1. RECT TempRect;
  2. GetWindowRect(hWnd, &TempRect);
  3. LPCSTR myString = "This is the string to output";
  4. SIZE StringSize;
  5. GetTextExtentPoint32(GetDC(hWnd), (LPCTSTR) myString, 28, &StringSize);
  6. int Textx = (TempRect.right - TempRect.left)/2 - (StringSize.cx/2);
  7. int Texty =    (TempRect.bottom - TempRect.top)/2 - (StringSize.cy/2);
  8. TextOut(GetDC(hWnd),Textx,Texty,myString,28);
  9.  
That 28 is the number of chars in the string. I'm sure there is a way to get that dynamically, but im tired.
Sep 11 '07 #8
compman9902
105 100+
Here is the basic way of centering the text on a given window. I was unable to get the dynamic window to work, but im sure a couple of the other people can help you out with that.

Expand|Select|Wrap|Line Numbers
  1. RECT TempRect;
  2. GetWindowRect(hWnd, &TempRect);
  3. LPCSTR myString = "This is the string to output";
  4. SIZE StringSize;
  5. GetTextExtentPoint32(GetDC(hWnd), (LPCTSTR) myString, 28, &StringSize);
  6. int Textx = (TempRect.right - TempRect.left)/2 - (StringSize.cx/2);
  7. int Texty =    (TempRect.bottom - TempRect.top)/2 - (StringSize.cy/2);
  8. TextOut(GetDC(hWnd),Textx,Texty,myString,28);
  9.  
That 28 is the number of chars in the string. I'm sure there is a way to get that dynamically, but im tired.
Thank yoyu very much..But, where do I put this to make the code work?
Sep 11 '07 #9
Studlyami
464 Expert 256MB
That would go after the new window is created. Instead of grabbing the rect for hWnd you would use the new window handle. If someone could help out with the dynamic window creation applying that code would make a lot more sense.
Sep 11 '07 #10
compman9902
105 100+
That would go after the new window is created. Instead of grabbing the rect for hWnd you would use the new window handle. If someone could help out with the dynamic window creation applying that code would make a lot more sense.
Then it is agreed, I need help.
Anyone?
Sep 13 '07 #11
compman9902
105 100+
And you can't build a string containing your display and just use TextOut() ??
Could you maybe write an example of how to use it? Becasue I have next to no experience with GUI in C++, so that would be great.
Sep 13 '07 #12
compman9902
105 100+
Actually, I just thought of something. All I need is what goes under the "WM_PAINT" case and then I'll be golden.
Sep 13 '07 #13
Studlyami
464 Expert 256MB
So, you don't want a new window to be created then? If so, you don't want that code under the WM_PAINT.
Sep 13 '07 #14
compman9902
105 100+
So, you don't want a new window to be created then? If so, you don't want that code under the WM_PAINT.
Oh, then I'm stuck. Any Tips? Anybody?
Also, just for future refrence, I was wondering what include to put in to the textout() function works. I'm using Dev-C++. Thanks.
Sep 14 '07 #15
Studlyami
464 Expert 256MB
WM_PAINT message gets sent on a lot of different occasions so having a create within that message would be bad. the textout function takes textout(<window handle>, <X location to draw>, <Y location to draw>, <String to be drawn>, <number of characters in string>). That code up above is all you need to center text on a given window. The only thing that needs to change is the handle to the window. The problem you are running into is the dynamic window creation in win32. After you create the dynamic window you will have a handle to that window.
Sep 14 '07 #16
compman9902
105 100+
WM_PAINT message gets sent on a lot of different occasions so having a create within that message would be bad. the textout function takes textout(<window handle>, <X location to draw>, <Y location to draw>, <String to be drawn>, <number of characters in string>). That code up above is all you need to center text on a given window. The only thing that needs to change is the handle to the window. The problem you are running into is the dynamic window creation in win32. After you create the dynamic window you will have a handle to that window.
I wanted to try out the textout() function, so I created the following program:
Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. /*  Declare Windows procedure  */
  7. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  8.  
  9. /*  Make the class name into a global variable  */
  10. char szClassName[ ] = "WindowsApp";
  11.  
  12. int WINAPI WinMain (HINSTANCE hThisInstance,
  13.                     HINSTANCE hPrevInstance,
  14.                     LPSTR lpszArgument,
  15.                     int nFunsterStil)
  16.  
  17. {
  18.     HWND hwnd;               /* This is the handle for our window */
  19.     MSG messages;            /* Here messages to the application are saved */
  20.     WNDCLASSEX wincl;        /* Data structure for the windowclass */
  21.  
  22.     /* The Window structure */
  23.     wincl.hInstance = hThisInstance;
  24.     wincl.lpszClassName = szClassName;
  25.     wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
  26.     wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
  27.     wincl.cbSize = sizeof (WNDCLASSEX);
  28.  
  29.     /* Use default icon and mouse-pointer */
  30.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  31.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  32.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  33.     wincl.lpszMenuName = NULL;                 /* No menu */
  34.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  35.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  36.     /* Use Windows's default color as the background of the window */
  37.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  38.  
  39.     /* Register the window class, and if it fails quit the program */
  40.     if (!RegisterClassEx (&wincl))
  41.         return 0;
  42.  
  43.     /* The class is registered, let's create the program*/
  44.     hwnd = CreateWindowEx (
  45.            0,                   /* Extended possibilites for variation */
  46.            szClassName,         /* Classname */
  47.            "Windows App",       /* Title Text */
  48.            WS_OVERLAPPEDWINDOW, /* default window */
  49.            CW_USEDEFAULT,       /* Windows decides the position */
  50.            CW_USEDEFAULT,       /* where the window ends up on the screen */
  51.            544,                 /* The programs width */
  52.            375,                 /* and height in pixels */
  53.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  54.            NULL,                /* No menu */
  55.            hThisInstance,       /* Program Instance handler */
  56.            NULL                 /* No Window Creation data */
  57.            );
  58.  
  59.     /* Make the window visible on the screen */
  60.     ShowWindow (hwnd, nFunsterStil);
  61.  
  62.     /* Run the message loop. It will run until GetMessage() returns 0 */
  63.     while (GetMessage (&messages, NULL, 0, 0))
  64.     {
  65.         /* Translate virtual-key messages into character messages */
  66.         TranslateMessage(&messages);
  67.         /* Send message to WindowProcedure */
  68.         DispatchMessage(&messages);
  69.     }
  70.  
  71.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  72.     return messages.wParam;
  73. }
  74.  
  75.  
  76. /*  This function is called by the Windows function DispatchMessage()  */
  77.  
  78. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  79. {
  80.     switch (message)                  /* handle the messages */
  81.     {
  82.         case WM_PAINT:
  83.             PAINTSTRUCT paintStruct;
  84.             HDC hdc;
  85.             hdc = GetDC (hwnd);
  86.             BeginPaint (hwnd, &paintStruct);
  87.             textout(hdc, 500, 500, "yo", 2);
  88.             EndPaint(hwnd, &paintStruct);
  89.             ReleaseDC (hwnd, hdc);
  90.             return 0;
  91.         break;
  92.         case WM_DESTROY:
  93.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  94.             break;
  95.         default:                      /* for messages that we don't deal with */
  96.             return DefWindowProc (hwnd, message, wParam, lParam);
  97.     }
  98.  
  99.     return 0;
  100. }
When I try to compile it, I get the following error:
87 C:\Documents and Settings\DBE\Desktop\main.cpp `textout' undeclared (first use this function)
That is what I was asking about in my last post. The dynamic text thing is a different matter, but still needs attending to.
Sep 14 '07 #17
Studlyami
464 Expert 256MB
You textout needs to be TextOut(...)
capital T capital O
Sep 14 '07 #18
compman9902
105 100+
You textout needs to be TextOut(...)
capital T capital O
That fixes that error.
Now I get tge error message:
[Linker error] undefined reference to `TextOutA@20'
ld returned 1 exit status
Sep 14 '07 #19
Studlyami
464 Expert 256MB
Did you try creating a new project and pasting the code in or was this a code file downloaded from somewhere? If it was a code file downloaded from somewhere try creating a new win32 project and copy that code and paste it in the new project. I copied your code made the change and it works fine. I'm using Visual studios 2003 to build it.
Sep 15 '07 #20
compman9902
105 100+
Did you try creating a new project and pasting the code in or was this a code file downloaded from somewhere? If it was a code file downloaded from somewhere try creating a new win32 project and copy that code and paste it in the new project. I copied your code made the change and it works fine. I'm using Visual studios 2003 to build it.
Neat, it works.
Well, now all I need is how to get it to be dynamic whenever I pass an intiger into the function that creates the window, and then it displays that.
Sep 15 '07 #21
compman9902
105 100+
I would just like to remind everyone involved with this post that the issue has not been resolved.
It seems like everyone has forgotten since the last post was 29 days ago
Oct 13 '07 #22
weaknessforcats
9,208 Expert Mod 8TB
I would just like to remind everyone involved with this post that the issue has not been resolved.
It seems like everyone has forgotten since the last post was 29 days ago
The code sample doesn't compile.

There are various errors. Mainly attempting to use a char array as an LPWSTR. For that I added the include of tchar.h and used the standard TCHAR mappings you are supposed to use in Windows code.

All I did was take you code, create a Win32 Project in Visual Studio.NET 2005 and fix errors. I did rework the WM_PAINT message handling.

The code below works and displays the yo in the center of the window. Have a look and you will see where my changes are.

I had no idea that the code was never compilng.


Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. #include <tchar.h>
  6.  
  7. /*  Declare Windows procedure  */
  8. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  9.  
  10. /*  Make the class name into a global variable  */
  11. TCHAR szClassName[ ] = TEXT("WindowsApp");
  12.  
  13. int WINAPI WinMain (HINSTANCE hThisInstance,
  14.                     HINSTANCE hPrevInstance,
  15.                     LPSTR lpszArgument,
  16.                     int nFunsterStil)
  17.  
  18. {
  19.     HWND hwnd;               /* This is the handle for our window */
  20.     MSG messages;            /* Here messages to the application are saved */
  21.     WNDCLASSEX wincl;        /* Data structure for the windowclass */
  22.  
  23.     /* The Window structure */
  24.     wincl.hInstance = hThisInstance;
  25.     wincl.lpszClassName = szClassName;
  26.     wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
  27.     wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
  28.     wincl.cbSize = sizeof (WNDCLASSEX);
  29.  
  30.     /* Use default icon and mouse-pointer */
  31.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  32.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  33.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  34.     wincl.lpszMenuName = NULL;                 /* No menu */
  35.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  36.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  37.     /* Use Windows's default color as the background of the window */
  38.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  39.  
  40.     /* Register the window class, and if it fails quit the program */
  41.     if (!RegisterClassEx (&wincl))
  42.         return 0;
  43.  
  44.     /* The class is registered, let's create the program*/
  45.     hwnd = CreateWindowEx (
  46.            0,                   /* Extended possibilites for variation */
  47.            szClassName,         /* Classname */
  48.            TEXT("Windows App"),       /* Title Text */
  49.            WS_OVERLAPPEDWINDOW, /* default window */
  50.            CW_USEDEFAULT,       /* Windows decides the position */
  51.            CW_USEDEFAULT,       /* where the window ends up on the screen */
  52.            CW_USEDEFAULT,        /* The programs width */
  53.            CW_USEDEFAULT,        /* and height in pixels */
  54.            //544,                 /* The programs width */
  55.            //375,                 /* and height in pixels */
  56.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  57.            NULL,                /* No menu */
  58.            hThisInstance,       /* Program Instance handler */
  59.            NULL                 /* No Window Creation data */
  60.            );
  61.  
  62.     /* Make the window visible on the screen */
  63.     ShowWindow (hwnd, nFunsterStil);
  64.  
  65.     /* Run the message loop. It will run until GetMessage() returns 0 */
  66.     while (GetMessage (&messages, NULL, 0, 0))
  67.     {
  68.         /* Translate virtual-key messages into character messages */
  69.         TranslateMessage(&messages);
  70.         /* Send message to WindowProcedure */
  71.         DispatchMessage(&messages);
  72.     }
  73.  
  74.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  75.     return messages.wParam;
  76. }
  77.  
  78.  
  79. /*  This function is called by the Windows function DispatchMessage()  */
  80.  
  81. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  82. {
  83.     HDC hdc;
  84.     PAINTSTRUCT ps;
  85.     RECT rect;
  86.     switch (message)                  /* handle the messages */
  87.     {
  88.         case WM_PAINT:
  89.             hdc = BeginPaint(hwnd, &ps);
  90.             GetClientRect(hwnd, &rect);
  91.             DrawText(hdc, TEXT("yo"), -1, &rect,
  92.                   DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  93.             EndPaint(hwnd, &ps);
  94.             return 0;
  95.         break;
  96.         case WM_DESTROY:
  97.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  98.             break;
  99.         default:                      /* for messages that we don't deal with */
  100.             return DefWindowProc (hwnd, message, wParam, lParam);
  101.     }
  102.  
  103.     return 0;
  104. }
  105.  
  106.  
Oct 14 '07 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Roel Wuyts | last post by:
CALL FOR CONTRIBUTIONS International Workshop on Revival of Dynamic Languages http://pico.vub.ac.be/~wdmeuter/RDL04/index.html (at OOPSLA2004, Vancouver, British Columbia, Canada, October...
1
by: Guinness Mann | last post by:
When you guys talk about "dynamic SQL," to what exactly are you referring? Is dynamic SQL anything that isn't a stored procedure? Specifically, I use ASP.NET to communicate with my SQL Server...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
5
by: bearophileHUGS | last post by:
I often use Python to write small programs, in the range of 50-500 lines of code. For example to process some bioinformatics data, perform some data munging, to apply a randomized optimization...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.