How to programmatically click Enter Key in Powerbuilder - powerbuilder

I Have an ole object browser where there is a popup message box to the website I open, I want to programmatically trigger the enterkey to automatically close the message box.

If it is a messagebox you might try Send(32770, 16, 0, 0) with '16' being the decimal value for WM_CLOSE and '32770' being the handle for a Messagebox.

Since it's functionality in an OLE I think you either need control that dialog via the OLE API and if that doesn't have the tooling you need- the only other way is go "around" the OLE directly to Windows.
So the answer would likely be the same for most Windows programming languages- in case you don't get big enough audience being tied to the PowerBuilder tag alone. Big question is do you know title of that dialog?
The idea isn't much different than clicking a button on any web based dialog window which opens up a can of "right or wrong dilemma" and sure you're doing it for the right reasons.
The PB specific parts would be define External Function something like:
PUBLIC FUNCTION unsignedlong FindWindow (long &
classname, string windowname) LIBRARY "user32.dll" &
ALIAS FOR FindWindowW
Then add code to use it:
unsignedlong hwnd
hwnd = FindWindow( 0, "Window Title if known if not hahaha" )
if hwnd = 0 then
// fml^2 ("fml squared" originated here now- shared conscious test #1 )
else
// Yes! Now we have the Windows API, or Post, Send might work fine.
// Send(hwnd, 273, 0, Handle(cb_OK)) or similar
end if

Related

Empty Win32 Popup Menu

I'm trying to create a dynamic popup menu within my application, the generation code I use is something like that :
HMENU menu;
POINT pt;
menu = CreatePopupMenu();
SetForegroundWindow( receivingWindow );
GetCursorPos( &pt );
int i = 19;
AppendMenu( menu, MF_STRING, i++, _TEXT("meh meh") );
AppendMenu( menu, MF_STRING, i++, _TEXT("testo") );
AppendMenu( menu, MF_STRING, i++, _TEXT("foobar foobar") );
TrackPopupMenuEx( menu
, 0
, pt.x, pt.y
, receivingWindow
, NULL );
DestroyMenu( menu );
_TEXT is used to ensure text is in Unicode and receivingWindow is a Layered window created before and working well.
When calling TrackPopupMenuEx the menu is displayed with the good size and at the good position, but absolutely no text appear in the popup menu. Did someone got an idea why, and how to fix this problem?
EDIT: more information regarding my environment :
Windows 7 x64
x86 build in Visual Studio 2008
EDIT2: I've tested the same on Windows XP x86, and it works like a charm, and after further test, the menu is well displayed in Windows 7 x64 with the classic look.
Make sure ::DefWindowProc() is called for messages that aren't handled otherwise. I had a similar issue with TrackPopupMenu() where the menu would popup with the correct size and allow items to be selected, but there was no text on the items because the code was ignoring some messages without calling ::DefWindowProc().
If your compiler is not set to compile for unicode (i.e #ifndef UNICODE) then winuser.h will map AppendMenu to AppendMenuA which is a non-unicode version and would interpret your strings as multi-byte. Perhaps this explains your issue?
You could explicitly call AppendMenuW() (the unicode version) to check if this is your issue.
I believe the problem is that the function TrackPopUpMenuEx doesn't return immediately; thus after it initiates (and presumably chooses it's size and position) but before it first displays you are destroying the menu.
As I understand it you need to destroy the menu after your window has received the command message from a menu selection. Alternately use the TPM_RETURNCMD flag in TrackPopUpMenuEx as this forces trackpopupex to only return after a menu item has been selected (as opposed to immediately).
I found a workaround for this problem. Instead of using my main window (receivingWindow), I'm using a message only window to receive the event. For a reason that I don't understand, the text is displayed normally this way.
Try:
call GetSubMenu,mnu,0
call TrackPopupMenu,eax,TPM_CENTERALIGN or TPM_BOTTOMALIGN,cposx,cposy,0,MainhWnd,0
In my case i created the menu with "CreateMenu" but i must use "CreatePopupMenu" to use it with TrackPopupMenuEx.
If the menu is created in the resource editor check if the flag is set.

How to autodetect urls in RichEdit 2.0?

When we have a RichEdit control and send it an EM_AUTOURLDETECT message with WPARAM set to TRUE, it nicely hightlights the detected URLs and sends the EN_LINK notifications.
But it does this only for text that is entered into the control. I haven't found the way to do it for text that's loaded into the control with SetWindowText or EM_STREAMIN.
Please help! Thanks
Upd:
I've created a test application from scratch and it works fine there. I think the problem might be that I have superclassed the control, that is, created a new window class and just use the window procedure of the original class. I'm gonna try subclassing the control instead..
I just knocked up a basic WTL dialog based app containing a riched20 control and the following works fine:
CRichEditCtrl richedit = GetDlgItem(IDC_RICHEDIT);
richedit.SetAutoURLDetect(TRUE);
richedit.SetWindowText(_T("http://www.stackoverflow.com"));
I have some old MFC code that does something similar, albeit with ES_STREAM, and it works OK too.
FWIW the WTL CRichEditCtrl wrapper is pretty thin. SetAutoURLDetect simply calls SendMessage passing it EM_AUTOURLDETECT.
I am compiling with _RICHEDIT_VER set to 0x0200 FWIW.
Without knowing the format of the text you are trying to add to the control with SetWindowText and EM_STREAMIN I'm going to take a guess and say this might have something to do with the control's text mode. After setting the contents of the control try sending it a EM_GETTEXTMODE message and see if the TM_PLAINTEXT bit is set. If this is the case then try sending a EM_SETTEXTMODE message followed by EM_AUTOURLDETECT. Your code should look something like this:
UINT textmode = (UINT)::SendMessage(handle_to_control, EM_GETTEXTMODE, 0, 0);
if(textmode & TM_PLAINTEXT) {
textmode &= ~TM_PLAINTEXT; // Clear the TM_PLAINTEXT bit
textmode |= TM_RICHTEXT; // Set the TM_RICHTEXT bit
if(::SendMessage(handle_to_control, EM_SETTEXTMODE, textmode, 0) != 0) {
// Failed to set the text mode
}
}
::SendMessage(handle_to_control, EM_AUTOURLDETECT, TRUE, 0);
You might just have to rewrite the text to the control to get it to re-parse.

How to add spin control to the dialog box using win32 C?

just want to know how to add an spin control ( in another name, up/down control ) in the dialog box using C program (win32 / code::block / mingw compiler)
Simplest way is by using a resource editor to design your dialog. Code::Blocks doesn't come with one, but ResEdit is one I've used.
If you are editing an .rc file by hand, you'd add a line similar to the following within the dialog definition section:
CONTROL "", IDC_SPIN1, UPDOWN_CLASS, UDS_ARROWKEYS, 7, 22, 11, 14
If you want to add it programatically, you can do so through the CreateWindow API function, e.g.
HWND hwndUpDown = CreateWindow(UPDOWN_CLASS, NULL,
WS_CHILD | WS_VISIBLE | UDS_ARROWKEYS,
7, 22, 11, 14,
hwndDlg, NULL, hInst, NULL);
where the hwndDlg parameter is the HWND of your dialog window. A good place to call this is when you handle the WM_INITDIALOG message for the dialog.
It depends. There are two ways to create a dialog. Programmatically, or via a dialog resource. In the first case, you call CreateDialogIndirect, in the second case CreateDialog. I assume you call CreateDialogIndirect since you mention "in C". In the dialog box template you use, simply add the spin control. You will need to identify it by name in DLGTEMPLATEEX.windowClass.

c++ mfc: update HWND through a control box

Can anyone help me how I update a HWND value through an edit control or any other kind of text box? I tried the following
void ChwndtestDlg::OnBnClickedButton1()
{
TCHAR thebuffer[100];
HWND thetext = (HWND)thebuffer;
GetDlgItemText(IDC_EDITWINDOW, thebuffer, 100);
::SendMessage(thetext,WM_SETTEXT,0,(LPARAM)L"hello");
}
But, that does not work! I'm new to all of this, and I'll be grateful for any help. Please bear in mind that I already know about enumwindows, and I have already successfully changed text in another program from my program, but I want to actually update the HWND in my edit control. It is supposed to work like so...
program 2's current hwnd = 0x00000:
open my program -> open program #2 -> input 0x00000 into textbox in my program -> click button in my program to update the hwnd value -> input text in my programs text editor -> text goes to program 2's text editor. I'm aware that there are other ways of doing this, but I would really like to do it the way I have described. Thanks in advance. I found a similar question, but the answer was to use enumwindows, but I don't want to use that.
You are typecasting the TCHAR[] itself to an HWND. That will never work. You need to extract the TCHAR[] text from the edit control, then use _stscanf() or similar parsing function to convert that text value to an integer which can be typecasted to an HWND, then assign your other text to that HWND as needed.
For example:
void ChwndtestDlg::OnBnClickedButton1()
{
CString sWnd;
if (GetDlgItemText(IDC_HWNDEDIT, sWnd) > 0)
{
HWND hWnd;
if (_stscanf((LPTSTR)sWnd, _T("%p"), &hWnd) == 1)
{
CString sText;
GetDlgItemText(IDC_TEXTEDIT, sText);
::SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)(LPTSTR)sText);
}
}
}

WinAPI - How to print text to edit control?

REVISION 1:
I can't get a good grip on WinAPI's AllocConsole(); function. I either need option one, or option 2. It's better to have both though :). Thanks in advance. Here's option 1:
Make a console window from a WinAPI program when BUTTON_1 is clicked. The console needs to be able to output text (like cout), and sleep/delay text from being printed. (Something like Sleep())
Option 2:
Instead of printing text to the console window like in option 1, the program prints text to a user-inaccessible editbox. This also needs to be able to use something like Sleep() to delay text from being printed.
Assuming you're appending strings to an edit control, perhaps to record TTY-style output from some lengthy process:
Use the EM_SETSEL message to select the range just after the last character, then use EM_REPLACESEL to replace it with the string to append. The edit control scrolls when this happens. For example, if hEdit is the handle for the edit control, and str the 0-terminated string to append:
int idx=GetWindowTextLength(hEdit);
SendMessage(hEdit,EM_SETSEL,idx,idx);
SendMessage(hEdit,EM_REPLACESEL,0,(LPARAM)str);
This is the process recommended by this Knowledge Base article: http://support.microsoft.com/kb/109550
My suggestions is to use
SendMessage(hEditBox,EM_SETSEL,-2,-2);
to move the caret to the end of text in the edit control. It saves you one function call and it works.
About the Sleep related part, I think it would be better to use timers, that should work without any problems.
Ex.
// somewhere in the code where you decide to make changes to the edit control
...
SetTimer(hWnd,TIMER_ID,1000,0); // TIMER_ID is arbitrary, the delay is set to 1000 ms
...
// main procedure message
switch (uMsg) {
...
case WM_TIMER:
SendMessage(hEditBox,EM_SETSEL,-2,-2);
SendMessage(hEditBox,EM_REPLACESEL,0,buffer);
KillTimer(hWnds,TIMER_ID);
...
}
You may want to put a bit more work into this, for instance to reuse the SetTimer call.

Resources