473,568 Members | 2,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scale form in C#

4 New Member
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 5305
balabaster
797 Recognized Expert Contributor
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
sentra50
4 New Member
excellent! thank you very much for this, I am trying it now.
Nov 5 '07 #3
sentra50
4 New Member
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 Recognized Expert Contributor
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 Recognized Expert Top Contributor
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
sentra50
4 New Member
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 Recognized Expert Contributor
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...ma ybe 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
11783
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 should have the same size regardless of screen resolution... can anyone tell me why? thanks :>
36
6341
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 something I'll need in this case is some experience-based set of rules about how to use python in this context. For example... is defining...
1
1794
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 the axis has to be changed manually by double click the axis.
0
1285
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 screendimensions. It accounts for that, the window does not grow bigger but the controls do, although the maximum window dimensions it uses is...
2
7398
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
2020
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 it. I have managed to implement it as follows: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
0
1473
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 command I also receive an answer from the scale. I want to communicate with the scale using TCP or UDP. The commands that can be sent to the scale are...
7
4193
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 called parsing? i'll try to explain: currently i can have the scale sending continuous weight data, but i can also set it to print every secon or so,...
1
5755
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 response (1, 2 or 3) varies- such that for some questions "I agree" is = to 3 points, and other times "I agree" is = to 1. I created an "answer key"...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7916
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.