No offense, but I am not familiar with your posts enough to trust the zip download. Can you post the actions in your code where 'the picturebox is dropped down' (I don't know what that means.)? And maybe a screenshot of your form?
The project consists of a single form with no code.
There is a single control on the form, which is a UC(user control).
There are no other dependencies or References
That is the good news
The bad news is I have never worked with the code within a UC
(Except once to solve a UC message box, that ran on everything except W10. Still patting myself on the back for solving that one)
So it probably pays for the OP to mention - some one familiar with UC's should have a look at this.
Here is the code in the UC (actually this is all of the code in the project) -
Code:
Const SWP_NOSIZE As Long = &H1
Const SWP_NOMOVE As Long = &H2
Const SWP_NOZORDER As Long = &H4
Const SWP_NOREDRAW As Long = &H8
Const SWP_NOACTIVATE As Long = &H10
Const SWP_NOOWNERZORDER As Long = &H200
Const HWND_TOPMOST As Long = -1
Const GWL_STYLE As Long = (-16)
Const GWL_EXSTYLE As Long = (-20)
Const HWND_DESKTOP As Long = &H0
Const WS_CHILD As Long = &H40000000
Const WS_VISIBLE As Long = &H10000000
Const SBS_SIZEBOXTOPLEFTALIGN As Long = &H2&
Const SBS_SIZEBOXBOTTOMRIGHTALIGN As Long = &H4&
Const SBS_SIZEBOX As Long = &H8&
Const SBS_SIZEGRIP As Long = &H10&
Dim SizeGripHandle As Long
Const WS_EX_APPWINDOW As Long = &H40000
Const WS_EX_TOOLWINDOW As Long = &H80
Const HWND_NOTOPMOST = -2
Const HWND_TOP = 0
Private Declare Function SetCapture Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal CX As Long, ByVal CY As Long, ByVal wFlags As Long) As Long
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExW" (ByVal dwExStyle As Long, ByVal lpClassName As Long, ByVal lpWindowName As Long, ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, ByRef lpParam As Any) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function BringWindowToTop Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function ShowCaret Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
Public Sub TestDropDownClient(DropDownClient As PictureBox)
DropDownClient.Visible = False
DropDownClient.BorderStyle = 0
SetWindowLong DropDownClient.hwnd, GWL_EXSTYLE, GetWindowLong(DropDownClient.hwnd, GWL_EXSTYLE) Or WS_EX_TOOLWINDOW And Not WS_EX_APPWINDOW
SetWindowLong DropDownClient.hwnd, GWL_STYLE, GetWindowLong(DropDownClient.hwnd, GWL_STYLE) Or &H80000
Dim P As POINTAPI
ClientToScreen UserControl.hwnd, P
SetParent DropDownClient.hwnd, HWND_DESKTOP
MoveWindow DropDownClient.hwnd, P.X, P.Y + UserControl.ScaleHeight, UserControl.ScaleWidth, 100, 1
SetWindowPos DropDownClient.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE Or SWP_NOACTIVATE Or SWP_NOOWNERZORDER
DropDownClient.Visible = True
Dim SizeGripHandle As Long
SizeGripHandle = CreateWindowEx(0, StrPtr("ScrollBar"), StrPtr(vbNullString), WS_CHILD Or WS_VISIBLE Or SBS_SIZEBOX Or SBS_SIZEBOXBOTTOMRIGHTALIGN Or SBS_SIZEGRIP, 0, 0, 0, 0, DropDownClient.hwnd, 0, App.hInstance, ByVal 0&)
End Sub
Private Sub Command1_Click()
TestDropDownClient Picture1
End Sub
Last edited by Bobbles; Sep 8th, 2017 at 07:25 AM.
Reason: added image
I uploaded it in zip extension because the uploader never let me upload rar files and I dont know why .
simply it is a user control which has a picturebox which I need it to float outside the usercontrol like a pop-up window . I set the parent of the picturebox to HWND-DESKTOP and this is the problem. once set , the textbox in the picturebox never gets the focus even by mouse .
I will simplify the code now , just add a usercontrol with command1 and picture1 . Add new textbox to the picturebox . consider changing picturebox color or user control color to distinguish between them because the will be layered one over one . Before clicking the command, the textbox behaves so normally . Once the command is clicked , the textbox will no longer receive any manual focus by mouse or by code.
here is all the code
this code makes the picturebox as a new window
Code:
Option Explicit
Private Const WM_SETFOCUS = &H7
Const GWL_STYLE As Long = (-16)
Const GWL_EXSTYLE As Long = (-20)
Const WS_EX_TOOLWINDOW As Long = &H80
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Sub Command1_Click()
SetParent Picture1.hwnd, 0
SetWindowLong Picture1.hwnd, GWL_EXSTYLE, (GetWindowLong(Picture1.hwnd, GWL_EXSTYLE) Or WS_EX_TOOLWINDOW)
Picture1.ZOrder 0
UserControl.ScaleMode = 3
Picture1.Visible = True
SendMessage Picture1.hwnd, WM_SETFOCUS, 0, 0
End Sub
Can I ask why you are trying to remove a control (picturebox) from your project and pinning it to the desktop? Maybe better understanding the purpose will help us provide better suggestions?
Insomnia is just a byproduct of, "It can't be done"
Can I ask why you are trying to remove a control (picturebox) from your project and pinning it to the desktop? Maybe better understanding the purpose will help us provide better suggestions?
I have a usercontrol , its function is to read some recordset and display it in a lsit view . and this pop up will help me to add the search function for the fields of the recordset . it may have more than 30 textboxes .so it is an area problem
the form concept will require more performance load beacuase it will have to recall some usercontrl methods again . anyway . I just first want to make sure that using the picturebox is a closed way and then we will discuss the form method if you dont mind lavolpe . I hope this problem is solved . it is a headache
As I mentioned, I know diddly-squat about UC's, but I created the following in the IDE
1. Command1
2. PB1
3. TextBox manually placed inside of PB1 .. named Text3(2) .. but don't ask why ..
When I click the TextBox, I get this ..
... and focus moves from the CB to the TextBox and the cursor "flashes"
Of course, this is NOT what you are doing, but I just wanted to establish a starting point .. this is what you are expecting, right?
EDIT-1
Oops .. I didn't expect the cursor to be captured by Paint.
I manually changed the text and added a "|", but now I see that the "real" cursor appears
on the second line of the TextBox. Sorry for any confusion.
Spoo
Last edited by Spooman; Sep 8th, 2017 at 04:38 PM.
This will enable the textbox, but it prevents being able to access the parent form. If the button, picbox & textbox were part of the form instead and the code was called, the same happens, but you could tab from the textbox back to the form (command button) which enables the form to be accessed normally. Guess what I'm trying to say, is to use the code below, you'll need a way to move the picbox back to the usercontrol from the picbox. If stuck, you'll want to close the form via the IDE toolbar STOP button.
Code:
SetWindowLong Picture1.hwnd, GWL_STYLE, (GetWindowLong(Picture1.hwnd, GWL_STYLE) Or WS_POPUP)
SetWindowLong Picture1.hwnd, GWL_EXSTYLE, (GetWindowLong(Picture1.hwnd, GWL_EXSTYLE) Or WS_EX_TOOLWINDOW)
SetParent Picture1.hwnd, 0
Personally, I think a different approach may be worth pursuing if you are having this much trouble early on.
FYI: Const WS_POPUP As Long = &H80000000
Insomnia is just a byproduct of, "It can't be done"
As I mentioned, I know diddly-squat about UC's, but I created the following in the IDE
thanks so much spoo for trying to help , but as you said it is not exactly the problem . thanks again
This will enable the textbox, but it prevents being able to access the parent form. If the button, picbox & textbox were part of the form instead and the code was called, the same happens, but you could tab from the textbox back to the form (command button) which enables the form to be accessed normally. Guess what I'm trying to say, is to use the code below, you'll need a way to move the picbox back to the usercontrol from the picbox. If stuck, you'll want to close the form via the IDE toolbar STOP button.
Code:
SetWindowLong Picture1.hwnd, GWL_STYLE, (GetWindowLong(Picture1.hwnd, GWL_STYLE) Or WS_POPUP)
SetWindowLong Picture1.hwnd, GWL_EXSTYLE, (GetWindowLong(Picture1.hwnd, GWL_EXSTYLE) Or WS_EX_TOOLWINDOW)
SetParent Picture1.hwnd, 0
Personally, I think a different approach may be worth pursuing if you are having this much trouble early on.
FYI: Const WS_POPUP As Long = &H80000000
thank you lavolpe , it works fine for now. but I am awfully sorry , I think I could not get your point clearly about the considerations with this code .
When I clicked the command button, I could no longer access the form. It was as if it was disabled. I'd think you would have the same problem. The only way I could close the form was to click the STOP button on the IDE toolbar.
Insomnia is just a byproduct of, "It can't be done"
yes , I have just noticed this issue . so , is it a dead end for this approach now ? cant this be done in any other way lavolpe ? not including a form .
I think a form is the best solution for several reasons:
1 - makes sense to me
2 - you should be able to show it as "owned" by the parent form. This will make it minimize when parent is minimized; prevents it from going behind the parent form
3 - if user closes the parent form, all owned forms are closed as well
4 - lots of real estate for what you need
Above being said, I've created dozens of usercontrols, but never one that contained a form.
Insomnia is just a byproduct of, "It can't be done"
ok , lets say
1- I have a form . say form1
2- this form has 2 UCs . UC1 and UC2
3- both UCs popped-up another form (replacement for the picturebox)
I want a method to the replacement form is that now it deals with UC1 which is placed in form1. I mean returning them as objects to the form to be able to put this code in the replacement form :: say ::
Code:
form1.uc1.backcolor=vbred
or
Code:
call form1.uc1.startanything
or any other method
Last edited by Hosam AL Dein; Sep 8th, 2017 at 05:51 PM.
Am not sure I fully understand.
The command button in the UC may have code like this:
Code:
dim frm as new replacementform
frm.OwnerUC = Me
frm.show , UserControl.Parent ' << owned by parent form.
' If UC is in another UC, above may error, so may need to code around that if applicable
The form can have a public property where you can identify which UC called it...
Code:
' in the form declarations section
Private m_MyUC As Object
' then this property added to your form
Friend Property Let OwnerUC(Value As Object)
Set m_MyUC = Value
End Property
' in your form's unload event: Set m_MyUC = Nothing
' now somewhere in your code: m_MyUC.BackColor = vbRed
The above is just one possible solution if I understood your question correctly
Insomnia is just a byproduct of, "It can't be done"
Change: OwnerFRM.OwnerUC.BackColor = vbRed
To: m_MyUC.BackColor = vbRed
OwnerFRM is the parent form. To reference a control from a form, you need to use the format: OwnerFRM.Control. For example a textbox: OwnerFRM.Text1. Alternate method is to reference a form's control by its name via: OwnerFRM.Controls("name")
Not sure why you would need to cache a reference. It can be retrieved from:
m_MyUC.Parent
Insomnia is just a byproduct of, "It can't be done"
maybe the complete project will make it more clear .
now I have three UCs on the form . each one is calling its form . the button on the form should throw its effect on only the control which called it and this does not happen replacement.zip
I need every form to affect the control which called it to load
the getcontrolfrom hwnd needs more improvements I know , but it is jst because I wanted to pass the control as an object to the form . if there is any better solution , it would be nice
and the reason why I want the form and the control to be passed is because there might be two forms with many UCs , so , I want to access the control like this
Code:
someform.somecontrol.backcolor
Last edited by Hosam AL Dein; Sep 8th, 2017 at 08:12 PM.
Replace: frm.OwnerUC = GetControlFromhWnd(UserControl.hWnd)
with: frm.OwnerUC = Me
^^ Your are setting the form's property from within the usercontrol. Just pass the usercontrol's own reference. Don't make it harder than it needs to be
You also had your calls reversed in the popup form. Swap OwnerUC with OwnerFRM in command1_click.
Note: If your intent is to change the color of the popup form, then just use Me in the popup form's code, not OwnerFRM
Insomnia is just a byproduct of, "It can't be done"
no , lavolpe . you dont get what I mean . I find it hard to communicate due to many things related to my language . ok . forget about all of this lavolpe ,
I need each form to paint the control which initiated it . load the 3 forms and you will find that all paint the same UC.