As the title says, I can't get it working. The button works and looks like normal...
HWND hWndButtonGetRes = CreateWindowEx(NULL,
"BUTTON",
"Get Results",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
50,
220,
100,
24,
hwnd,
(HMENU)IDC_MAIN_BUTTON,
GetModuleHandle(NULL),
NULL);
SendMessage(hWndButtonGetRes, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)myBitmap);
Edit:
The bitmap is being loaded this way:
myBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(BM))
where BM is
BM BITMAP DISCARDABLE "bitmap.bmp"
#define BM 1
I am running Windows 7 64.
Related
I want make a hidden window visible.
HWND hWnd = FindWindow(NULL, "MyWindowName");
ShowWindow(hWnd, SW_SHOW);
The window is found, but nothing happens. It remains hidden.
What am I doing wrong?
If it matters, the application is made using MFC and it has the following method overwritten:
void CMyClass::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
lpwndpos->flags &= ~SWP_SHOWWINDOW;
CDialog::OnWindowPosChanging(lpwndpos);
}
I did it.
Apparently you need to modify some flags.
long style= GetWindowLong(hWnd, GWL_STYLE);
style |= WS_VISIBLE;
SetWindowLong(hWnd, GWL_STYLE, style);
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
And it works.
I am making a win32 api window application project on visual studio 2012.I use a LPWSTR type variable to store my strings described as follow.
LPWSTR MyStringList[3]={L"apple",L"orange",L"watermelon"};
I expect to copy the text from an editbox to one of the strings in MyStringList. Therefore,I made a simple editbox and button.
Here is the definition of editbox and button.
case WM_CREATE:
hEdit = CreateWindow(L"EDIT",
L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
10, 10, 200, 25,
hWnd,
(HMENU)ID_EDIT,
GetModuleHandle(NULL),
NULL);
hBtn = CreateWindow(L"BUTTON",
L"",
WS_CHILD | WS_VISIBLE,
250, 10, 50, 30,
hWnd,
(HMENU)ID_BUTTON,
GetModuleHandle(NULL),
NULL);
Here is the action when the button is pushed.The two messagebox are used to see if the string has been changed after function GetWindowText is called.
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case ID_BUTTON:
MessageBox(hWnd,MystringList[1],L"Before_Pusing",MB_OK);
GetWindowText(hEdit,MystringList[1],sizeof(MystringList[1]));
MessageBox(hWnd,MystringList[1],L"After_Pushing",MB_OK);
InvalidateRect(hWnd,NULL,TRUE);
break;
Now I tried to push the button after typing "banana" into the editbox. The second string "orange" should be replaced with "banana". However,it turned out that nothing changed .The second messagebox displayed "orange" as the first messagebox did. What's wrong with my code? Please help! Thanks a lot!
You could try this, adding whatever programming quirks that Windows needs.
#define MYLEN 20
...
char MyStringList[3][MYLEN+1] = {"apple", "orange", "watermelon"};
Then you can fetch the string from the edit box with
GetWindowText(hEdit,MystringList[1],MYLEN);
Although the GetWindowText() documentation says
"Copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application."
I have Created an Edit window.
I want one string to be displayed in one line and the other string to be displayed on the other line, but the code I am executing only displays the second string. below is my code snippet:
hWndEdit = CreateWindow("EDIT", // We are creating an Edit control
NULL, // Leave the control empty
WS_CHILD | WS_VISIBLE | WS_HSCROLL |
WS_VSCROLL | ES_LEFT | ES_MULTILINE |
ES_AUTOHSCROLL | ES_AUTOVSCROLL,
10, 10,1000, 1000,
hWnd,
0,
hInst,NULL);
SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n"));
SetWindowText(hWndEdit, TEXT("\r\nSecond string"));
OUTPUT:
You are only seeing the last line because SetWindowText() replaces the entire contents of the window in one go.
If you want to display both lines at one time, simply concatenate them together in a single call to SetWindowText():
SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n\r\nSecond string"));
On the other hand, if you want to insert them at different times, you have to use the EM_SETSEL message to place the edit caret at the end of the window and then use the EM_REPLACESEL message to insert text at the current caret position, as described in this article:
How To Programatically Append Text to an Edit Control
For example:
void AppendText(HWND hEditWnd, LPCTSTR Text)
{
int idx = GetWindowTextLength(hEditWnd);
SendMessage(hEditWnd, EM_SETSEL, (WPARAM)idx, (LPARAM)idx);
SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM)Text);
}
.
AppendText(hWndEdit, TEXT("\r\nFirst string\r\n"));
AppendText(hWndEdit, TEXT("\r\nSecond string"));
hWndEdit = CreateWindow("EDIT", // We are creating an Edit control
NULL, // Leave the control empty
WS_CHILD | WS_VISIBLE | WS_HSCROLL |
WS_VSCROLL | ES_LEFT | ES_MULTILINE |
ES_AUTOHSCROLL | ES_AUTOVSCROLL,
10, 10,1000, 1000,
hWnd,
0,
hInst,NULL);
SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n\r\nSecond string"));
or
SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n"));
char* buf = malloc(100);
memset(buf, '\0', 100);
GetWindowText(hWndEdit, (LPTSTR)buf, 100);
strcat(buf, "\r\nSecond string");
SetWindowText(hWndEdit, (LPTSTR)buf);
Any ideas how to use SetParent()?
The following doesn't seem to work:
fhWnd = FindWindow(_T("Notepad"), NULL);
hWnd = CreateWindowEx(WS_EX_TOPMOST , _T("test"), _T("test"),
WS_POPUP,
20, 20, 400, 400, NULL, NULL, hInst, NULL);
SetParent(hWnd, fhWnd);
DWORD style = GetWindowLong(hWnd, GWL_STYLE);
style = style & ~(WS_POPUP);
style = style | WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
SetWindowLong(hWnd, GWL_STYLE, style);
SetWindowLong(fhWnd, GWL_STYLE, GetWindowLong(fhWnd, GWL_STYLE) | WS_CLIPCHILDREN);
With the above code I have to move the parent window out of the screen and back in again in order to see what's draw on the child window.
Any idea what I am doing wrong?
I have a richedit control on a dialog. I want to populate the richedit control with a string. The richedit control itself displays properly. But I don't know how to populate the richedit control with a string. I took a guess at it below, in the resource.rc code.
In main.cpp, I have:
case IDC_BUT_ABOUT:
{
HINSTANCE hRichEdit;
hRichEdit = LoadLibrary("RICHED32.DLL");
DialogBox( hInst, MAKEINTRESOURCE(IDD_ABOUT), hDlgWnd,(DLGPROC)InitAbout );
//ShowWindow(hDlgWnd, SW_SHOWNORMAL);
return TRUE;
}
break;
In resource.rc, I have:
IDD_ABOUT DIALOGEX 0, 0, 206, 136
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
PUSHBUTTON "Close",IDCANCEL,77,115,50,14
CONTROL "",IDC_RICHEDIT21,"RichEdit20W",ES_AUTOHSCROLL | ES_READONLY | WS_BORDER | WS_TABSTOP,36,53,130,46
END
string rt = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fprq2\fcharset0 Biondi;}}{\colortbl ;\red255\green0\blue0;}\viewkind4\uc1\pard\f0\fs20\par\cf1\f1 hello\cf0\f0 \ul some text here\par}";
this.IDC_RICHEDIT21.Rtf = rt;
But this apparently isn't even close to how I should put text from a string into a rich edit control?
Try EM_SETTEXTEX or EM_STREAMIN.