Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image and Form Size 1

Status
Not open for further replies.

allyeric

Programmer
Mar 14, 2000
104
CA
On my form, I have an image that I need as a background pic.&nbsp;&nbsp;I have the form maximize when running, but the image does not maximize.&nbsp;&nbsp;How can I fix this?&nbsp;&nbsp;I would like it to fill the screen no matter what setting the screen is at.&nbsp;&nbsp;Any help is greatly appreciated.<br><br>Thanks<br><br>
 
Form.Picture property draws the image at screen resolution with top-left at 0, 0.<br>use API call BitBlt() to draw directly on the form.<br><br>Public Declare Function BitBlt Lib &quot;gdi32&quot; Alias &quot;BitBlt&quot; (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long<br><br>hDestDC = Form.hDC<br>x = 0<br>y = 0<br>nWidth = Form.ScaleWidth (in PIXELS)<br>nHeight = Form.ScaleHeight (in PIXELS)<br>hSrcDC = ContainerObject.hDC (some object with its Picture property set to the image you want to use)<br>xSrc = 0<br>ySrc = 0<br>dwRop = &HCC0020 (copy)<br><br>see MSKB article Q147810 &lt;<A HREF=" TARGET="_new"> for more info.<br><br>mr s. &lt;;)
 
thank you for responding.&nbsp;&nbsp;But, I am pretty new to this, I did read the article you referred to, but still a little confused.&nbsp;&nbsp;Where do I put this code?&nbsp;&nbsp;I added it as a separate function, but I am getting an error.&nbsp;&nbsp;Is there something else I need to do?
 
ok, I figured out where to put the code, but still getting an error - with the hDestDC&nbsp;&nbsp;line ...&nbsp;&nbsp;do I put my form name in there?&nbsp;&nbsp;Or my image object name?&nbsp;&nbsp;
 
the hDestDC (DC = device context, the handle of the object windows draws onto) is the DC of the destination, so pass &lt;yourformname&gt;.hDC as the first parameter. don't store this value in a variable since it may change.<br><br>stephen. &lt;;)
 
I did put my form name as hDC - but still get an error, invalid outside procedure ??&nbsp;&nbsp;why is this?
 
i'm sorrry, i should have made this clearer. you can only ever run code from inside subs or functions.<br>if you use <b><FONT FACE=monospace>Public Declare...</font></b> it should go in a <b><FONT FACE=monospace>.bas</font></b> module.<br>if on the other hand you use <b><FONT FACE=monospace>Private Declare...</font></b>, it goes in the declarations section of the form (before the first sub or function).<br>call <b><FONT FACE=monospace>BitBlt()</font></b> inside the <b><FONT FACE=monospace>Form_Paint()</font></b> or <b><FONT FACE=monospace>Form_Resize()</font></b> procedures.<br><br>so something like:<br><br><b><FONT FACE=monospace>Option Explicit<br><br>Public Declare Function BitBlt Lib...<br><br>Private Sub Form_Paint()<br>If BitBlt(Form.hDC, ...) = 0 Then<br>&nbsp;&nbsp;' Error checking<br>End If<br>End Sub</font></b><br><br>remember that <b><FONT FACE=monospace>BitBlt()</font></b> returns non-zero when it succeeds, but not necessarily (well, almost definitely) not -1, so you can't use <b><FONT FACE=monospace>Not</font></b> to check for this. <b><FONT FACE=monospace>Not 1</font></b> is -2, which is also equivalent to <b><FONT FACE=monospace>True</font></b>.<br><br>mr s. &lt;;)<br>
 
I am sorry - I am just not getting this.&nbsp;&nbsp;I am still getting the same error in the hDestDC = frmResults.hDC line.&nbsp;&nbsp;Here is what I have in my code ...<br><br>Option Explicit<br>Private Declare Function BitBlt Lib &quot;gdi32&quot; (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) As Long<br><br>hDestDC = frmResults.hDC<br>X = 0<br>Y = 0<br>nWidth = frmResults.ScaleWidth '(in PIXELS)<br>nHeight = frmResults.ScaleHeight '(in PIXELS)<br>hSrcDC = frmResults.hDC '(some object with its Picture property set to the image you want to use)<br>XSrc = 0<br>YSrc = 0<br>dwRop = &HCC0020 '(copy)<br><br>End Sub<br><br>-----------------------------------------<br><br>Private Sub Form_Paint()<br><br>If BitBlt(Form.hDC, 0, 0, frmResults.ScaleWidth, frmResults.ScaleHeight, frmResults.hDC, 0, 0, &HCC0020) = 0 Then<br>&nbsp;&nbsp;' Error checking<br>End If<br><br>End Sub<br><br><br>I really do appreciate you taking the time to help me with this.
 
sorry, i didn't mean to confuse you. the <b><FONT FACE=monospace>variable = value</font></b> lines were to show you what to put in the parameters.<br><br>so, set the ScaleMode property of your form to 3 - Pixel in the form designer.<br><br>it goes like this:<br><br><b><FONT FACE=monospace>Option Explicit<br><br>Private Declare Function BitBlt Lib &quot;gdi32&quot; _<br>&nbsp;&nbsp;(ByVal hDestDC As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal X As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal Y As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal nWidth As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal nHeight As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal hSrcDC As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal XSrc As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal YSrc As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal dwRop As Long) _<br>&nbsp;&nbsp;&nbsp;As Long<br><br>Private Sub Form_Paint()<br>If BitBlt(frmResults.hDC, 0, 0, frmResults.ScaleWidth, frmResults.ScaleHeight, picStore.hDC, 0, 0, &HCC0020) = 0 Then<br>&nbsp;&nbsp;' Error checking<br>End If<br>End Sub</font></b><br><br>use Err.LastDLLError to check the error as API call GetLastError has (apparently) a serious memory leak.<br><br>hope this helps.<br><br>mr s. &lt;;)
 
ok, I changed my code.&nbsp;&nbsp;I don't get any more errors, but its still not working.&nbsp;&nbsp;Doesn't Form_Paint() only get called when user resizes the form?&nbsp;&nbsp;In my case, the user cannot resize the form, it opens up maximized.&nbsp;&nbsp;Therefore, the paint function is never called.&nbsp;&nbsp;And, I created a picStore object and put it on the form (?) but should this object be visible or not?&nbsp;&nbsp;
 
hmm. a sudden rush of self-doubt sent me rushing to vb to test if my memory was right or not. doesn't work for me. copies the screen background fine. never trust what you read in the manual.<br><br>so, how about using an Image control with the Stretch property set to True, Top and Left to 0, and resizing in the Form_Resize event.<br><br><b><FONT FACE=monospace>Private Sub Form_Load()<br>Image1.Stretch = True<br>Image1.Top = 0<br>Image1.Left = 0<br>Image1.zOrder 0<br>End Sub<br><br>Private Sub Form_Resize()<br>If Me.WindowState = vbMinimized Then Exit Sub<br>Image1.Width = Me.ScaleWidth<br>Image1.Height = Me.ScaleHeight<br>End Sub</font></b><br><br>this is probably simpler too if you don't mind the overhead.<br><br>you can also use the PaintPicture method of the Form object:<br><br><b><FONT FACE=monospace>Private Sub Form_Resize()<br>Me.PaintPicture picStore.Picture, 0, 0, Me.ScaleWidth, Me.ScaleHeight<br>End Sub</font></b><br>
 
that works great!&nbsp;&nbsp;Well, except for one small problem - I have some labels on the form, and they stay behind the background when I run it - I did set the background to Send to Back - but makes no difference - the text boxes are fine, its just the labels that don't show up.
 
the Paint event:<br><br><b><FONT FACE=monospace>Occurs when part or all of an object is exposed after being moved or enlarged, or after a window that was covering the object has been moved. (MSDN)</font></b><br><br>so pretty much whenever the object needs to be repainted.<br><br>the nWidth and nHeight parameters of BitBlt seem to be ignored, so use StretchBlt instead:<br><br><b><FONT FACE=monospace>Option Explicit<br><br>Private Declare Function StretchBlt Lib &quot;gdi32&quot; _<br>&nbsp;&nbsp;(ByVal hdc As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal x As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal y As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal nWidth As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal nHeight As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal hSrcDC As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal xSrc As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal ySrc As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal nSrcWidth As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal nSrcHeight As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal dwRop As Long) _<br>&nbsp;&nbsp;&nbsp;As Long<br><br>Private Sub Form_Paint()<br>If StretchBlt(frmResults.hdc, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0, 0, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmResults.ScaleWidth, frmResults.ScaleHeight, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;picStore.hdc, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0, 0, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;picStore.ScaleWidth, picStore.ScaleHeight, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&HCC0020) = 0 Then Stop<br>End Sub</font></b><br><br>only problem is Paint doesn't seem to be called when the form is made smaller (huh?) but since yours is maximised that doesn't matter.<br><br>mr s. &lt;;)
 
I used the other way - with the image - and it works perfectly - as for the label question I had, its ok now too, I changed the z-order of the image.<br><br>Thank you very much for your time and assistance!!<br>
 
uh oh - I have another problem with that now! (And just when you thought you were done with me!) When I change my screen size from 1024 x 768 to 800 x 600 the background no longer fits - its too big.&nbsp;&nbsp;And, while we are on the subject, is there any way I can have all my controls on that form to move as form resizes?&nbsp;&nbsp;What I mean is, when I created the controls on the form, they were in the right place that I wanted them to be, but when the form maximizes, they are no longer on the same place on the form, no longer centered so to speak.&nbsp;&nbsp;
 
so, you've use the resizing Image control with stretch set to True to display the image. if you set the Width to Me.ScaleWidth and the Height to Me.ScaleHeight, i don't see how it can be too big. could you post the code?<br><br>if you want to set the positions of the controls on the form when it is resized, there's no easy way to do it. you have to work out where you want them (relatively), and then set the Top, Left, Width and Height properties for each control seperately.<br><br>example:<br><br>form has a label and a textbox at the top, then a full width and height multiline text box, then another label/textbox pair at the bottom of the form<br><br><b><FONT FACE=monospace><br>' X = border;- = label;. = textbox<br>'<br>' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>' X-Label1---------------X.Text1................X<br>' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>' X.Text2.......................................X<br>' X.............................................X<br>' X.............................................X<br>' X.............................................X<br>' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>' X-Label2---------------X.Text3................X<br>' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br><br>Private Sub Form_Resize()<br>Label1.Width = (Me.ScaleWidth - 3 * Label1.Top) / 2<br>Text1.Width = Label1.Width<br>Text1.Left = (Me.ScaleWidth + Label1.Top) / 2<br>Text2.Top = Label1.Height + 2 * Label1.Top<br>Text2.Width = Me.ScaleWidth - 2 * Label1.Top<br>Text2.Height = Me.ScaleHeight - 2 * Label1.Height - 3 * Label1.Top<br>Label2.Top = Me.ScaleHeight - Label2.Height - Label1.Top<br>Label2.Width = Label1.Width<br>Text3.Top = Label2.Top<br>Text3.Width = Label1.Width<br>End Sub<br></font></b><br><br>you'll get errors if the form is too small or minimised...<br><br>mr s. &lt;;)
 
I added that code to two separate forms, and on one it works perfectly, but on the other doesn't get smaller when I change screen size.&nbsp;&nbsp;The only difference between the two forms, is that on the form that doesn't work properly, I have other code in the form load as well as your code.&nbsp;&nbsp;On the other form, only code I have in form load is yours.&nbsp;&nbsp;I tried changing where I put your code in the function, first or last, but didn't make a difference either.
 
when you say it doesn't work, what do you mean? doesn't compile, gives a runtime error, or doesn't resize the image on the form?<br><br>some code would really help here.<br> <p>mr s. <;)<br><a href=mailto: > </a><br><a href= > </a><br>why does it never do what is says it does in the manual?
 
sorry - what I meant is it doesn't resize the image on the form.&nbsp;&nbsp;As for code, if I copied it, I am sure it would be alot of useless information, as most of it is just queries and putting the query results on the form. But, here it is anyways. <br><br><b><br>Private Sub Form_Resize()<br><br>If Me.WindowState = vbMinimized Then Exit Sub<br>imgBackground.Width = Me.ScaleWidth<br>imgBackground.Height = Me.ScaleHeight<br><br>End Sub<br><br>----------------------------<br><br>Private Sub Form_Load()<br><br>X = 15<br>Dim FrmPass As String<br>Dim MCIndex As Integer<br>Dim Index2 As Integer<br>Dim NewIndex As Integer<br>Dim C, R As Integer<br>Dim Pass As String<br><br>'Open the database<br>For lQuestionNumber = 1 To 24<br><br>&nbsp;&nbsp;&nbsp;&nbsp;strSQL = &quot;SELECT * FROM Exam_SRHEXA16 where Question_No =&quot; & lQuestionNumber<br>&nbsp;&nbsp;&nbsp;&nbsp;Set con = New ADODB.Connection<br>&nbsp;&nbsp;&nbsp;&nbsp;con.Open &quot;DSN=Training;UID=;PWD=;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set rs = con.Execute(strSQL)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;While Not rs.EOF<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sAnswers(lQuestionNumber - 1) = rs(&quot;Answer_Text&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sCorrectAns(lQuestionNumber - 1) = rs(&quot;Correct_Answer&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sCorrectAns2(lQuestionNumber - 1) = rs(&quot;Correct_Answer&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sQuestion(lQuestionNumber - 1) = rs(&quot;Question_Text&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sAnswers2(lQuestionNumber - 1) = rs(&quot;Answer_Text&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rs.MoveNext<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Wend<br>Next<br><br>Pass = frmPassword.txtPassword.Text<br>For C = 1 To Len(Pass)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R = R + Asc(Mid$(Pass, C))<br>Next<br><br>'randomize answers from database<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For lJumble = 1 To (R \ 5)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lrandom = Int(Rnd * 23)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sTempQues = sQuestion(lrandom)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sQuestion(lrandom) = sQuestion(lJumble Mod 3)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sQuestion(lJumble Mod 3) = sTempQues<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next<br><br>For lQuestionNumber = 0 To 23<br>&nbsp;&nbsp;&nbsp;&nbsp;If sQuestion(lQuestionNumber) &lt;&gt; &quot;XXX&quot; Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lblQuestion(Index).Caption = sQuestion(lQuestionNumber)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Index = Index + 1<br>&nbsp;&nbsp;&nbsp;End If<br>Next<br><br>'print questions on form<br>For Index = 0 To 23<br>&nbsp;&nbsp;&nbsp;&nbsp;lblAnswers(Index).Caption = sAnswers(Index)<br>Next<br><br>'print employee's answers to form<br>&nbsp;&nbsp;&nbsp;&nbsp;For Index2 = 0 To 19<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lblAnswers(Index2).Caption = (sAnswers(Index2))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strSQL = &quot;UPDATE Emp_Questions set Ques_&quot; & (Index2 + 1) & &quot;= '&quot; & lblQuestion(Index2).Caption & &quot;' where No_Employe = &quot; & frmPassword.txtEmpNo.Text<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set rs = con.Execute(strSQL)<br>&nbsp;&nbsp;&nbsp;&nbsp;Next<br><br>For Index = 0 To 19<br>&nbsp;&nbsp;&nbsp;&nbsp;strSQL = &quot;SELECT * From Exam_SRHEXA16 WHERE Question_Text = '&quot; & lblQuestion(Index) & &quot;'&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set rs = con.Execute(strSQL)<br>&nbsp;&nbsp;&nbsp;&nbsp;With frmResults<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.txtQuestion(Index).Text = lblQuestion(Index)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If rs(&quot;Correct_Answer&quot;) = &quot;15&quot; Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.txtCorrAns(Index).Text = &quot;13&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.txtAnswerText(Index).Text = &quot;Micro Circuit&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.txtCorrAns(Index).Text = rs(&quot;Correct_Answer&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.txtAnswerText(Index).Text = rs(&quot;Answer_Text&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End With<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rs.MoveNext<br>Next<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>imgBackground.Stretch = True<br>imgBackground.Top = 0<br>imgBackground.Left = 0<br>imgBackground.ZOrder 1<br><br></b><br><br>End Sub<br><br>
 
it looks like your code isn't reaching the imgBackground lines. your code is like this:<br><br><b><FONT FACE=monospace>Private Sub Form_Load()<br><br>'Code Block One<br>X =...<br><br>'Code Block Two<br>imgBackground....<br><br>End Sub</font></b><br><br>if code block two only runs if code block one isn't present, it suggests that code block one doesn't finish. <br>some errors aren't caught by vb and dump you out of the procedure with no notification (the API is especially terrible for this).<br>you could try putting the image initialisation at the beginning of the procedure instead. if this works and the image resizes you should step debug the rest of the code.<br><br>if moving the code doesn't work you could always set the properties directly at design time using the properties window.<br> <p>mr s. <;)<br><a href=mailto: > </a><br><a href= > </a><br>why does it never do what is says it does in the manual?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top