473,406 Members | 2,745 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,406 software developers and data experts.

scale form in C#

hello,


I am trying to do the following:
On load, a form must scale itself with a number of pixels on the X axis, lets say 100 pixels for example. Also it must do this on a timer, to ensure a somehow smooth animation.

How would I do this?> Here is what I wrote so far but it does not work.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Drawing.Drawing2D;
  9.  
  10. namespace drawingStuff
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             this.DoubleBuffered = true;
  18.             tmRefresh.Interval = 1;
  19.         }
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.             tmRefresh.Start();
  23.         }
  24.  
  25.         private void tmRefresh_Tick(object sender, EventArgs e)
  26.         {
  27.         this.Invalidate();
  28.         }
  29.  
  30.  
  31.         private void Form1_Paint(object sender, PaintEventArgs e)
  32.         {
  33.             Graphics g;
  34.             g = e.Graphics;
  35.             g.SmoothingMode = SmoothingMode.HighQuality;
  36.             g.ScaleTransform(this.Width - 50, this.Height - 50);
  37.  
  38.             Rectangle recForm = new Rectangle(0, 0, this.Width, this.Height);
  39.             Pen pen = new Pen(Color.Black);
  40.             g.DrawRectangle(pen, recForm);
  41.         }
  42.  
  43.     }
  44. }
  45.  
Ok so what this does , it draws a rectangle inside my form. That one is scaled. I dont need rectangles inside my form, I want to scale the form itself and I don't know how to do this.

Please help with suggestions.

Regards,
Nov 2 '07 #1
7 5294
balabaster
797 Expert 512MB
hello,


I am trying to do the following:
On load, a form must scale itself with a number of pixels on the X axis, lets say 100 pixels for example. Also it must do this on a timer, to ensure a somehow smooth animation.

How would I do this?> Here is what I wrote so far but it does not work.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Drawing.Drawing2D;
  9.  
  10. namespace drawingStuff
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. this.DoubleBuffered = true;
  18. tmRefresh.Interval = 1;
  19. }
  20. private void Form1_Load(object sender, EventArgs e)
  21. {
  22. tmRefresh.Start();
  23. }
  24.  
  25. private void tmRefresh_Tick(object sender, EventArgs e)
  26. {
  27. this.Invalidate();
  28. }
  29.  
  30.  
  31. private void Form1_Paint(object sender, PaintEventArgs e)
  32. {
  33. Graphics g;
  34. g = e.Graphics;
  35. g.SmoothingMode = SmoothingMode.HighQuality;
  36. g.ScaleTransform(this.Width - 50, this.Height - 50);
  37.  
  38. Rectangle recForm = new Rectangle(0, 0, this.Width, this.Height);
  39. Pen pen = new Pen(Color.Black);
  40. g.DrawRectangle(pen, recForm);
  41. }
  42.  
  43. }
  44. }
  45.  
Ok so what this does , it draws a rectangle inside my form. That one is scaled. I dont need rectangles inside my form, I want to scale the form itself and I don't know how to do this.

Please help with suggestions.

Regards,
If you want to modify items such as the form's size by timer you'll have to do it through the threading model using delegates.
VB
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim FinalSize As New System.Drawing.Size(400, 400)
  3. Private WithEvents ChangeSizeTimer As New Timers.Timer(25)    
  4. Public Delegate Sub ChangeSizeDelegate(ByVal Size As System.Drawing.Size)
  5.     Public Sub ChangeSize(ByVal Size As System.Drawing.Size)
  6.         If Me.InvokeRequired Then
  7.             Me.Invoke(New ChangeSizeDelegate(AddressOf ChangeSize), Size)
  8.         Else
  9.             Me.Size = Size
  10.         End If
  11.     End Sub
  12.     Public Sub TimerEvt(ByVal Sender As Object, ByVal e As Timers.ElapsedEventArgs) _
  13.     Handles ChangeSizeTimer.Elapsed
  14.         Dim oSize As System.Drawing.Size
  15.         'Do whatever it is you have to do to calculate your next size
  16.         ChangeSize(oSize)
  17.     End Sub 
  18.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  19.         Me.Size = New System.Drawing.Size(200, 200)
  20.         ChangeSizeTimer.Start()
  21.     End Sub
  22.  
C#
Expand|Select|Wrap|Line Numbers
  1.  
  2. System.Drawing.Size FinalSize = new System.Drawing.Size(400, 400); 
  3. private Timers.Timer ChangeSizeTimer = new Timers.Timer(25); 
  4. public delegate void ChangeSizeDelegate(System.Drawing.Size Size); 
  5. public void ChangeSize(System.Drawing.Size Size){ 
  6.     if (this.InvokeRequired) { 
  7.         this.Invoke(new ChangeSizeDelegate(ChangeSize), Size); 
  8.     } 
  9.     else { 
  10.         this.Size = Size; 
  11.     } 
  12. public void TimerEvt(object Sender, Timers.ElapsedEventArgs e) { 
  13.     System.Drawing.Size oSize; 
  14.     //Do whatever it is you have to do to calculate your next size 
  15.     ChangeSize(oSize);    
  16. private void Form1_Load(object sender, System.EventArgs e) { 
  17.     this.Size = new System.Drawing.Size(200, 200); 
  18.     ChangeSizeTimer.Start(); 
  19. }
  20.  
I'll leave you to calculate the sizes...just use a basic calculation to determine if your form has reached the right size or not. When it does, kill the timer.
Nov 2 '07 #2
excellent! thank you very much for this, I am trying it now.
Nov 5 '07 #3
I dont understand this.
It never enters the TimerEvt() method, therefore it never enters ChangeSize() method.

What event do I need to set for it to work?

All it does is it enters Form_Load and thats it.

later edit:
ok I added an event now.

It does not work. Here is the code as it is now:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Diagnostics;
  9.  
  10.  
  11. namespace diagnostics
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.             changeSizeTimer.Elapsed += new System.Timers.ElapsedEventHandler(changeSizeTimer_Elapsed);
  19.         }
  20.  
  21.         void changeSizeTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  22.         {
  23.             System.Drawing.Size oSize;
  24.             oSize = this.Size;
  25.             if (finalSize == oSize)
  26.             {
  27.                 changeSizeTimer.Stop();
  28.             }
  29.             changeSize(oSize);
  30.         }
  31.  
  32.         Size finalSize = new Size(400, 400);
  33.         private System.Timers.Timer changeSizeTimer = new System.Timers.Timer(25);
  34.  
  35.         public delegate void changeSizeDelegate(Size Size);
  36.  
  37.         public void changeSize(Size Size)
  38.         {
  39.             if (this.InvokeRequired)
  40.             {
  41.                 this.Invoke(new changeSizeDelegate(changeSize), Size);
  42.             }
  43.             else
  44.             {
  45.                 this.Size = Size;
  46.             }
  47.         }
  48.  
  49.         private void Form1_Load(object sender, EventArgs e)
  50.         {
  51.             this.Size = new System.Drawing.Size(200, 200);
  52.             changeSizeTimer.Start();
  53.         }
  54.  
  55.     }
  56. }
  57.  
Nov 5 '07 #4
balabaster
797 Expert 512MB
I dont understand this.
It never enters the TimerEvt() method, therefore it never enters ChangeSize() method.

What event do I need to set for it to work?

All it does is it enters Form_Load and thats it.

later edit:
ok I added an event now.

It does not work. Here is the code as it is now:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Diagnostics;
  9.  
  10.  
  11. namespace diagnostics
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. changeSizeTimer.Elapsed += new System.Timers.ElapsedEventHandler(changeSizeTimer_Elapsed);
  19. }
  20.  
  21. void changeSizeTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  22. {
  23. System.Drawing.Size oSize;
  24. oSize = this.Size;
  25. if (finalSize == oSize)
  26. {
  27. changeSizeTimer.Stop();
  28. }
  29. changeSize(oSize);
  30. }
  31.  
  32. Size finalSize = new Size(400, 400);
  33. private System.Timers.Timer changeSizeTimer = new System.Timers.Timer(25);
  34.  
  35. public delegate void changeSizeDelegate(Size Size);
  36.  
  37. public void changeSize(Size Size)
  38. {
  39. if (this.InvokeRequired)
  40. {
  41. this.Invoke(new changeSizeDelegate(changeSize), Size);
  42. }
  43. else
  44. {
  45. this.Size = Size;
  46. }
  47. }
  48.  
  49. private void Form1_Load(object sender, EventArgs e)
  50. {
  51. this.Size = new System.Drawing.Size(200, 200);
  52. changeSizeTimer.Start();
  53. }
  54.  
  55. }
  56. }
  57.  
Did you get this working yet? The code you posted looks okay at first glance. Don't want to waste time trying to figure out something you already fixed. Cheers.
Nov 6 '07 #5
Shashi Sadasivan
1,435 Expert 1GB
Make sure that the timer is enabled (by default it is set as false ie not enabled) which can cause some headache
Nov 6 '07 #6
thank you for your input, both of you.

It is not working yet but I am trying to figure out why I m sure its something small that I am missing :)

Thank you again.
Nov 7 '07 #7
balabaster
797 Expert 512MB
thank you for your input, both of you.

It is not working yet but I am trying to figure out why I m sure its something small that I am missing :)

Thank you again.
Not that there's anything wrong with it that I can see...but try changing the names of your Size parameters...maybe that's causing a hiccup. I know that this functionality is supposed to be allowed, but I've had mixed success with calling objects by their typename, so now I try not to.
Nov 7 '07 #8

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

Similar topics

3
by: News Central | last post by:
I resize a control in a Form wth Scale mode set to vbInches. However, the control's size changes in different screen resolution, I thought that if Scale mode is set to vbInchs, then the control...
36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
1
by: S. van Beek | last post by:
Dear reader, A Microsoft Graph 2000 chart is as OLE class available in a form. If the form opens a diagram will be produced. Because of the variation in the figures for these diagrams...
0
by: Frank | last post by:
Hi, I use form.scale to scale up/down my form and controls, works great.But... when sizing in and the form and controls become bigger, at a certain moment the form will be as big/bigger than the...
2
by: placid | last post by:
Hi all, I was just wondering what is the equivalent of Form1.Scale (-160, -160)-(90, 95) in VB.NET? The Form1.Scale(...) in the VB.net is not the same as the old VB! Thanks in advance
3
by: Jerry Spence1 | last post by:
A very useful feature was added in 2.0 of .NET framework which was the scaling of a form and all the controls within it. This is really useful but I am finding very little information of how to use...
0
by: TonyJ | last post by:
Hello! We have a pc running a form application. If I push some buttons in this application I want to send some commands to a scale which is connected to a specific IP address. When I sent a...
7
Wagsy
by: Wagsy | last post by:
Hi All, I have a small form that allows scale weigh data to be diplayed in a textbox. i can communicate with the scale - tare, zero etc. how do i filter the weight string or i think it may be...
1
by: Maria DiGiano | last post by:
I am using Access to organize data from a survey which uses a Likert scale to measure response- the scale is 3 points- "I agree", "I don't know" and "I disagree". The numerical value of each...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.