Win32 API: transparency doesn't work - c++

I wrote a simple program; it's made of a main window and TextBox window above it. TextBox is 50% transparent. When TextBox gets a message, it draws a blue line on the main window.
The problem is that "transparent" actually is not transparent at all. If the blue line crosses a text in TextBox, the text just erased, despite the fact that the text is above. And vice versa: if I start typing, a part of the line in a row of a text just disappears instead of shine through.
Is this a bug? Or am I missing something?
#include <windows.h>
#include <stdio.h>
#define IDC_MAIN_EDIT 101
void DrawInWindow(HWND hWndToPaint){
HDC hdc = GetDC(hWndToPaint);
if(!hdc)printf("Invalid handle\n");
HPEN hPen = CreatePen(PS_SOLID,5,RGB(0, 0, 255));
SelectObject(hdc, hPen);
static float x=620, y=1, tg=0.5, ctg=2;
static int Xone = 1, Yone = 1;//depending on later recalculation this may become negative
MoveToEx(hdc,(int)x,(int)y,NULL);
if(tg<1){
y+=tg;
x+=Xone;
}else{
y+=Yone;
x+=ctg;
}
if(!LineTo(hdc, (int)x, (int)y) )printf("There are paint problem\n");
ReleaseDC(hWndToPaint,hdc);
//Now recalculate direction
RECT WndRect;
GetClientRect(hWndToPaint,&WndRect);
if(x>=WndRect.right){
if(ctg>0)ctg*=-1;//make negative
Xone=-1;
}
if(x<=WndRect.left){
if(ctg<0)ctg*=-1;//make positive
Xone=1;
}
if(y>=WndRect.bottom){
if(tg>0)tg*=-1;//make negative
Yone=-1;
}
if(y<=WndRect.top){
if(tg<0)tg*=-1;//make positive
Yone=1;
}
}
int CALLBACK EnumWindowsFunc(HWND hWnd, LPARAM lParam){
DrawInWindow(hWnd);
return false;
}
void PaintInMainWnd(){
EnumWindows(EnumWindowsFunc,0L);//Getting the handle of main window to draw
}
LRESULT __stdcall MyMainCallBckProcedure( HWND window, unsigned msg, WPARAM wp, LPARAM lp ){
switch(msg){
case WM_KEYDOWN:
if(wp == VK_ESCAPE)PostQuitMessage(0);
break;
case WM_DESTROY:
printf("\ndestroying window\n");
PostQuitMessage(0);
return 0;
case WM_SIZE:{
HWND hEdit;
RECT rcClient;
GetClientRect(window, &rcClient);
hEdit = GetDlgItem(window, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
break;
}
default:
return DefWindowProc( window, msg, wp, lp ) ;
}
}
WNDPROC lpEditWndProc;
LRESULT CALLBACK MyEditCallBckProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
if( (uMsg == WM_CHAR) && (wParam == VK_ESCAPE) )
{
PostQuitMessage(0);
return 0;
}
PaintInMainWnd();
lpEditWndProc(hWnd, uMsg, wParam, lParam);
}
bool CreateWindows(){
const char* const myclass = "myclass";
WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
0, myclass, LoadIcon(0,IDI_APPLICATION) };
if(RegisterClassEx(&wndclass)<0){
printf("ERR: in registering window class\n");
return false;
}
//Creating window
HWND window = CreateWindowEx( 0, myclass, "title",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
640, 480, 0, 0, GetModuleHandle(0), 0 );
if(!window){
printf("ERR: in creating window\n");
return false;
}
ShowWindow( window, SW_SHOWDEFAULT );
//creating TextBox on the window
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx(0, "edit", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
window, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL){
MessageBox(window, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
return false;
}
hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
//Now resize TextBox to fill whole parent window
RECT RectSize;
GetClientRect(window,&RectSize);
hEdit = GetDlgItem(window,IDC_MAIN_EDIT);
SetWindowPos(hEdit, 0,0,0,RectSize.right,RectSize.bottom,SWP_NOZORDER);
//Let's try to catch some messages in TextBox...
lpEditWndProc = (WNDPROC)SetWindowLongPtr(hEdit, GWL_WNDPROC, (LONG_PTR)&MyEditCallBckProcedure);
//Making hEdit transparent
SetWindowLongPtr(hEdit,GWL_EXSTYLE, WS_EX_LAYERED | GetWindowLongPtr(hEdit, GWL_EXSTYLE) );
SetLayeredWindowAttributes(hEdit, 0, (255*50)/100, LWA_ALPHA);
return true;
//###
}
int main(){
if(!CreateWindows() ){printf("Something gone wrong\n");return 1;}
MSG msg;
while(GetMessage(&msg,0,0,0) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Don't know is it important, but I should also mention, that I have tested only under Ubuntu with Wine, as my Windows is screwed up by this bug. Anyway, I hope the problem is not in Wine itself.
And sorry for amount of code, I really don't know what to remove to make it smaller.

I found the workaround. I created background top level window, and made foreground window 50% trasparent. I draw the lines in background window. If the front window moved or resized, the back window reacts accordingly with help of WM_WINDOWPOSCHANGED message, it sent on every little move/resize.
Anyway, this workaround is little dirty because of:
Linux/wine specific problems: 1) Display Manager do not decorate transparent window of wine(but this could be evaded by making second window 0% transparent) 2) Dragged window wobbling, but the second moving straight. All OS specific problem: second window is visible in the taskbar. Theoretically the last could be avoided by adding WS_EX_TOOLWINDOW to unowned window. The quote
To prevent the window button from being placed on the taskbar, create
the unowned window with the WS_EX_TOOLWINDOW extended style.
But, at least, in wine it doesn't work. Well, I hope this is a bug :)
#include <windows.h>
#include <stdio.h>
#define IDC_MAIN_EDIT 101
HWND hBackWnd;
void DrawInWindow(HWND hWndToPaint){
HDC hdc = GetDC(hWndToPaint);
if(!hdc)printf("Invalid handle\n");
HPEN hPen = CreatePen(PS_SOLID,5,RGB(0, 0, 255));
SelectObject(hdc, hPen);
static float x=620, y=1, tg=0.5, ctg=2;
static int Xone = 1, Yone = 1;//depending on later recalculation this may become negative
MoveToEx(hdc,(int)x,(int)y,NULL);
if(tg<1){
y+=tg;
x+=Xone;
}else{
y+=Yone;
x+=ctg;
}
if(!LineTo(hdc, (int)x, (int)y) )printf("There are paint problem\n");
ReleaseDC(hWndToPaint,hdc);
//Now recalculate direction
RECT WndRect;
GetClientRect(hWndToPaint,&WndRect);
if(x>=WndRect.right){
if(ctg>0)ctg*=-1;//make negative
Xone=-1;
}
if(x<=WndRect.left){
if(ctg<0)ctg*=-1;//make positive
Xone=1;
}
if(y>=WndRect.bottom){
if(tg>0)tg*=-1;//make negative
Yone=-1;
}
if(y<=WndRect.top){
if(tg<0)tg*=-1;//make positive
Yone=1;
}
}
LRESULT __stdcall MyMainCallBckProcedure( HWND window, unsigned msg, WPARAM wp, LPARAM lp ){
switch(msg){
case WM_KEYDOWN:
if(wp == VK_ESCAPE)PostQuitMessage(0);
break;
case WM_DESTROY:
printf("\ndestroying window\n");
PostQuitMessage(0);
return 0;
case WM_SIZE:
HWND hEdit;
RECT rcClient;
GetClientRect(window, &rcClient);
hEdit = GetDlgItem(window, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
break;
case WM_WINDOWPOSCHANGED:{//LPARAM is a ptr to WINDOWPOS
RECT BckWndRect;
if(!GetWindowRect(hBackWnd, &BckWndRect) )printf("ERR: getting backwnd rectangle\n");
bool IsRepaint;
WINDOWPOS* pNewPos = (WINDOWPOS*)lp;
if(BckWndRect.left+BckWndRect.right != pNewPos->cx
|| BckWndRect.top+BckWndRect.bottom != pNewPos->cy)IsRepaint = true;
else IsRepaint = false;
MoveWindow(hBackWnd, pNewPos->x, pNewPos->y, pNewPos->cx, pNewPos->cy, IsRepaint);
break;
}
default:
return DefWindowProc( window, msg, wp, lp ) ;
}
}
WNDPROC lpEditWndProc;
LRESULT CALLBACK MyEditCallBckProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
if( (uMsg == WM_CHAR) && (wParam == VK_ESCAPE) )
{
PostQuitMessage(0);
return 0;
}
DrawInWindow(hBackWnd);
lpEditWndProc(hWnd, uMsg, wParam, lParam);
}
bool CreateWindows(){
//creating back window
const char* backwnd = "backwnd";
WNDCLASSEX backwndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
0, backwnd, LoadIcon(0,IDI_APPLICATION) };
if(RegisterClassEx(&backwndclass)<0){
printf("ERR: in registering second window class\n");
return false;
}
hBackWnd = CreateWindowEx( 0, backwnd, "title", WS_EX_TOOLWINDOW |
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
640, 480, 0, 0, GetModuleHandle(0), 0 );
if(!hBackWnd){
printf("ERR: in creating background window\n");
return false;
}
ShowWindow( hBackWnd, SW_SHOWDEFAULT );
//Creating front window
const char* const frontwnd = "frontwnd";
WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
0, frontwnd, LoadIcon(0,IDI_APPLICATION) };
if(RegisterClassEx(&wndclass)<0){
printf("ERR: in registering foreground window class\n");
return false;
}
HWND window = CreateWindowEx( 0, frontwnd, "title",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
640, 480, 0, 0, GetModuleHandle(0), 0 );
if(!window){
printf("ERR: in creating foreground window\n");
return false;
}
ShowWindow( window, SW_SHOWDEFAULT );
//creating textbox
HWND hEdit = CreateWindowEx( 0, "edit", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL
| ES_MULTILINE | ES_AUTOVSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, 640,
480, window, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(0), 0 );
HFONT hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
//Let's try to catch some messages in TextBox...
lpEditWndProc = (WNDPROC)SetWindowLongPtr(hEdit, GWL_WNDPROC, (LONG_PTR)&MyEditCallBckProcedure);
//Making foreground window transparent
SetWindowLongPtr(window,GWL_EXSTYLE, WS_EX_LAYERED | GetWindowLongPtr(window, GWL_EXSTYLE) );
SetLayeredWindowAttributes(window, 0, (255*50)/100, LWA_ALPHA);
return true;
//###
}
int main(){
if(!CreateWindows() ){printf("Something gone wrong\n");return 1;}
MSG msg;
while(GetMessage(&msg,0,0,0) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

Related

C++ resizing issue

I have successfully opened a win32 window(without OpenGL context). Opening the window, then resizing it causes a lot of issues. How do we properly handle resizing inside the win32 api? I create a window with WS_OVERLAPPEDWINDOW and making the window resizable. Mainly, I attempt to handle the resizing with the WM_SIZE event handler.
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <iostream>
using namespace std;
HINSTANCE hInstanceGlobal;
LRESULT CALLBACK wndproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
switch (message) {
default:
return DefWindowProcA(hWnd, message, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
return 0;
case WM_SIZE:
SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
PostMessage(hWnd, WM_PAINT, 0, 0);
return 0;
case WM_KEYDOWN:
break;
case WM_SETCURSOR:
if (LOWORD(lParam) == HTCLIENT) {
SetCursor(LoadCursor(NULL, IDC_ARROW));
return TRUE;
}
break;
}
return DefWindowProcA(hWnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd) {
#ifdef _DEBUG
AllocConsole();
//SetWindowPos(GetConsoleWindow(), 0, 1920, 200, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
AttachConsole(GetCurrentProcessId());
freopen("CON", "w", stdout);
#endif
hInstanceGlobal = hInstance;
const char* CLASSNAME = "APIOPENGL";
WNDCLASS cl = { };
cl.lpfnWndProc = wndproc;
cl.hCursor = LoadCursor(NULL, IDC_ARROW);
cl.hInstance = hInstance;
cl.lpszClassName = CLASSNAME;
RegisterClass(&cl);
HWND hWnd = CreateWindowEx(
WS_EX_APPWINDOW|WS_EX_CLIENTEDGE,
CLASSNAME,
"Opengl Window",
WS_OVERLAPPEDWINDOW |WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (hWnd == NULL)
{
return 0;
}
ShowWindow(hWnd, nShowCmd);
MSG msg;
MessageBox(NULL, (char*)GetLastError(), "Hello", NULL);
bool bRet;
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
if (GetKeyState(VK_RMENU) & 0x8000)
{
cout << "Pressed" << endl;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Check this out: Resize window with Win32 API
In your WM_PAINT handler, it's possible that your getting that strange result because you aren't calling the FillRect function.
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(m_hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 6));
EndPaint(m_hwnd, &ps);
The problem in the screenshot (black background) is because you have not set the hbrBackground member of the window class. According to the document:
When this member is NULL, an application must paint its own
background whenever it is requested to paint in its client area. To
determine whether the background must be painted, an application can
either process the WM_ERASEBKGND message or test the fErase
member of the PAINTSTRUCT structure filled by the
BeginPaint function.
Like the answer of #Stra, use FillRect to fill the background.
Or initialize hbrBackground:
hInstanceGlobal = hInstance;
const char CLASSNAME[] = "APIOPENGL";
WNDCLASS cl = { };
cl.lpfnWndProc = wndproc;
cl.hCursor = LoadCursor(NULL, IDC_ARROW);
cl.hInstance = hInstance;
cl.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
cl.lpszClassName = CLASSNAME;
RegisterClass(&cl);
Then you don’t need to process the WM_SIZE message.
In addition, you don’t need to call GetKeyState to get the VK_RMENU key Presse event, just process the WM_SYSKEYDOWN message and Indicates whether the key is an extended key:
/*case WM_SIZE:
SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
PostMessage(hWnd, WM_PAINT, 0, 0);
return 0;*/
case WM_SYSKEYDOWN:
if (VK_MENU == wParam)
{
BOOL bit = (lParam >> 24) & 1;
if (bit)
cout << "Pressed" << endl;
}
return 0;

How to change border color of a listview

I have created a list view using win32 api.
InitCommonControls();
HWND hwndList1 = CreateWindow(WC_LISTVIEW , L"", WS_VISIBLE|WS_CHILD | LVS_REPORT | LVS_EDITLABELS | LVS_ICON | LV_VIEW_TILE | LVS_EX_GRIDLINES | WS_BORDER | LVS_EX_FULLROWSELECT | ES_LEFT , 10, 10, 300, 190, hwnd, NULL, GetModuleHandle(NULL), 0);
SendMessageW( hwndList1,
LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_FULLROWSELECT ,
LVS_EX_FULLROWSELECT );
CreateItem(hwndList1 , (char*)L"fault RS458");
CreateItem(hwndList1 , (char*)L"fault RS455");
CreateColumn(hwndList1 , 0 , (char*)L"Insert column" , 300);
I see a black border around the list view. How can i change its color?
You can subclass the window using SetWindowSubclass (requires comctl32.lib) and handle WM_NCPAINT to paint the non-client area of the control as follows:
#include <Windows.h>
#include <CommCtrl.h>
LRESULT CALLBACK ListViewProc(HWND hwnd,
UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR)
{
switch(msg)
{
case WM_NCPAINT:
{
RECT rc;
GetWindowRect(hwnd, &rc);
OffsetRect(&rc, -rc.left, -rc.top);
auto hdc = GetWindowDC(hwnd);
auto hpen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
auto oldpen = SelectObject(hdc, hpen);
SelectObject(hdc, GetStockObject(NULL_BRUSH));
Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);//draw red frame
SelectObject(hdc, oldpen);
DeleteObject(oldpen);
ReleaseDC(hwnd, hdc);
//*** EDIT
//documentation says we should return 0
//but that causes problem with vertical scrollbar
//maybe we should break for this subclass case
break; //not return 0!
}
case WM_NCDESTROY:
RemoveWindowSubclass(hwnd, ListViewProc, 0);
break;
}
return DefSubclassProc(hwnd, msg, wp, lp);
}
...
HWND hwndList1 = CreateWindow(...);
SetWindowSubclass(hwndList1, ListViewProc, 0, NULL);
Side note, (char*)L"text" doesn't make sense. Either use ANSI ((char*)"text") or Unicode ((wchar_t*)L"text", recommended). You can change CreateItem to accept const wchar_t*, then cast to (wchar_t*) for LVITEM at the last step to avoid errors.
Edit
WM_NCPAINT will break, not return zero.

Change window's background color with button using winapi

I'm trying to make button change background color of window when clicked. I know that I need to handle this event in WM_COMMAND, where I also check ID of this button, but nothing happens. I tried to debug and my program recognizes ID correctly. The piece of code used for changing color works well when in main loop but it doesn't do anything when in WM_COMMAND. How do I solve this problem? Whole code:
#include <Windows.h>
#define BUTTON_ID 100
struct status_info {
const char* waiting = "Waiting for connection...";
const char* connected = "Connected.\nWaiting for frajer to copy number.";
const char* changed = "Number changed.";
}status_info;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) {
const wchar_t CLASS_NAME[] = L"Name";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = (LPCSTR)CLASS_NAME;
wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
RegisterClass(&wc);
//main window
HWND hwnd = CreateWindowEx(0, (LPCSTR)CLASS_NAME, (LPCSTR)"Hacker", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInstance, NULL);
//number window
HWND number = CreateWindowEx(WS_EX_WINDOWEDGE, TEXT("Static"), TEXT("Account number:\n00 1234 1234 1234 1234 1234 1234"), WS_CHILD | WS_VISIBLE, 5, 5, 240, 40, hwnd, NULL, NULL, NULL);
//status window
const char* status_message = status_info.waiting;
HWND status = CreateWindowEx(WS_EX_WINDOWEDGE, TEXT("Static"), TEXT(status_message), WS_CHILD | WS_VISIBLE, 5, 55, 240, 40, hwnd, NULL, NULL, NULL);
//button
HWND button = CreateWindowEx(0, "BUTTON", "Nightmode", WS_CHILD | WS_VISIBLE, 100, 100, 150, 30, hwnd, (HMENU)BUTTON_ID, hInstance, NULL);
MSG msg;
WNDCLASS okno;
while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
SetWindowText(status, status_message);
}
return msg.wParam;
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
//MessageBox(hwnd, L"1", L"1", 0);
return (0);
case WM_DESTROY:
//MessageBox(hwnd, L"2", L"2", 0);
PostQuitMessage(0);
return (0);
case WM_COMMAND: {
if (LOWORD(wParam) == BUTTON_ID) {
PAINTSTRUCT ps;
RECT rc;
HDC hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
SetBkColor(hdc, BLACK_BRUSH);
ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, 0, 0, 0);
EndPaint(hwnd, &ps);
}
break;
}
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
BeginPaint/EndPaint should be used in response to WM_PAINT only.
You can use GetDC(hwnd)/ReleaseDC(hwnd, hdc) to obtain hdc for painting on device context outside of WM_PAINT, but this will be temporary. The next refresh message causes the window to be erased and repainted according to what's in WM_PAINT
SetDCBrushColor can be used if the goal is to avoid creating brush handle.
static COLORREF bkcolor = RGB(255,255,255);
switch(message)
{
case WM_COMMAND:
if(LOWORD(wparam) == BUTTON_ID)
{
bkcolor = RGB(255, 0, 0);
InvalidateRect(hwnd, NULL, TRUE);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT rc;
HDC hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
SetDCBrushColor(hdc, bkcolor);
FillRect(hdc, &rc, (HBRUSH)GetStockObject(DC_BRUSH));
//or use ps.rcPaint to repaint only the section which requires update
//FillRect(hdc, &ps.rcPaint, (HBRUSH)GetStockObject(DC_BRUSH));
EndPaint(hwnd, &ps);
return 0;
}
case WM_ERASEBKGND:
//return 0 means WM_PAINT handles the background
return 0;
Alternatively, use SetClassLongPtr to replace the background brush:
static HBRUSH bkbrush = NULL;
switch(message)
{
case WM_COMMAND:
if(LOWORD(wparam) == BUTTON_ID)
{
COLORREF bkcolor = RGB(rand() % 256, rand() % 256, rand() % 256);
if(bkbrush)
DeleteObject(bkbrush);
bkbrush = CreateSolidBrush(bkcolor);
SetClassLongPtr(hwnd, GCL_HBRBACKGROUND, (LONG)bkbrush);
InvalidateRect(hwnd, NULL, TRUE);
}
break;

Button Press Handling win32 c++

I'm creating a plugin which loads a native window with 2 buttons, on press they should make a message box pop-up but there's no pup-up
Creating a thread for the message loop
//create message thread
class startMessageThreadLoop
{
public:
static DWORD WINAPI StaticThreadStart(void* Param)
{
MessageBox(hWnd, L"StaticThreadStart", L"StaticThreadStart", 0);
startMessageThreadLoop* This = (startMessageThreadLoop*)Param;
return This->ThreadStart();
}
DWORD ThreadStart(void)
{
MessageBox(hWnd, L"ThreadStart", L"ThreadStart", 0);
//create message loop for buttons
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
//translate and send messages
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void startMyThread()
{
MessageBox(hWnd, L"startMyThread", L"startMyThread", 0);
DWORD ThreadID;
CreateThread(NULL, 0, StaticThreadStart, (void*) this, 0, &ThreadID);
char szTest[100];
printf(szTest, "%d", ThreadID);
MessageBox(hWnd, LPCWSTR(szTest), L"ThreadIDBaby", 0);
}
};
Starting the thread
startMessageThreadLoop ThreadLoopInstance;
ThreadLoopInstance.startMyThread();
Creating the window
vidUploader.cbSize = sizeof(WNDCLASSEX);
vidUploader.style = CS_HREDRAW | CS_VREDRAW;
vidUploader.lpfnWndProc = WndProc;
vidUploader.cbClsExtra = 0;
vidUploader.cbWndExtra = 0;
vidUploader.hInstance = hUpload;
vidUploader.hIcon = LoadIcon(hUpload, MAKEINTRESOURCE(IDI_P2GOVIDEOUPLOADER20));
vidUploader.hCursor = LoadCursor(NULL, IDC_ARROW);
vidUploader.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
vidUploader.lpszMenuName = MAKEINTRESOURCE(IDC_P2GOVIDEOUPLOADER20);
vidUploader.lpszClassName = (LPCWSTR)(L"UploadVideo");
vidUploader.hIconSm = LoadIcon(wcexUpload.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassEx(&vidUploader);
hInst = hUpload; // Store instance handle in our global variable
hWnd = CreateWindow((LPCWSTR)(L"UploadVideo"), (LPCWSTR)(L"Upload Video's"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, NULL, hUpload, NULL);
Handle Button
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDC_SELECT_VIDEO) {
MessageBox(hWnd, L"Heeey", L"Hoi", 0);
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
Creating buttons & show Window
SelectVideoBTN = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Select Video's", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
10, // x position
460, // y position
100, // Button width
25, // Button height
hWnd, // Parent window
(HMENU)IDC_SELECT_VIDEO, // Assign appropriate control ID
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
UploadBTN = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Upload", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
390, // x position
460, // y position
100, // Button width
25, // Button height
hWnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
RECT rect = { 0, 0, uploadWNDWidth, uploadWNDHeight };
AdjustWindowRect(&rect, GetWindowLong(hWnd, GWL_STYLE), FALSE);
SetWindowPos(hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
if (!hWnd)
{
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
}
MSG msg;
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
//nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
nCmdShowUpload);
UpdateWindow(hWnd);
I believe that I'm doing everything that I have to in order to get the buttons to work, I have a message loop in a thread, I'm registering & handling the buttons and window, what am I missing?
The message queue of a window is linked to the thread in which the window was created. The message loop must be processed in the same thread.

Win32 API: How to catch escape key in Edit control?

I found that for some unknown reason when focus is in Edit control, the Escape key never produces a messages.
Below is a code to create a parent window, and Edit control above it. In MyCallBckProcedure() I laid printf() under a *WM_COMMAND*, to catch a messages, produced by Edit. More over -- I even tried to print all the messages catched in MyCallBckProcedure(); but if focus on Edit, the escape key never produces any of messages.
What a weird problem could be here?
#include <iostream>
#include <windows.h>
#include <stdio.h>
#define IDC_MAIN_EDIT 101
LRESULT __stdcall MyCallBckProcedure( HWND window, unsigned msg, WPARAM wp, LPARAM lp ){
printf("%x ",msg);
switch(msg){
case WM_COMMAND://here should be catched the escape key of Edit
printf("%x ",msg);
break;
case WM_KEYDOWN:
printf("%x ",msg);
if(wp == VK_ESCAPE)PostQuitMessage(0);
break;
case WM_DESTROY:
std::cout << "\ndestroying window\n" ;
PostQuitMessage(0);
return 0;
default:
return DefWindowProc( window, msg, wp, lp ) ;
break;
case WM_SIZE:{
HWND hEdit;
RECT rcClient;
GetClientRect(window, &rcClient);
hEdit = GetDlgItem(window, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
}
break;
}
}
int main(){
const char* const myclass = "myclass";
WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyCallBckProcedure,
0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
0, myclass, LoadIcon(0,IDI_APPLICATION) };
if(RegisterClassEx(&wndclass)<0){
printf("ERR: in registering window class\n");
return 1;
}
//Creating window
HWND window = CreateWindowEx( 0, myclass, "title",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, GetModuleHandle(0), 0 );
if(!window){
printf("ERR: in creating window\n");
return 1;
}
ShowWindow( window, SW_SHOWDEFAULT );
MSG msg;
//creating TextBox on the window
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx(0, "edit", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
window, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL){
MessageBox(window, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
return 1;
}
hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
//Now resize TextBox to fill whole parent window
RECT RectSize;
GetClientRect(window,&RectSize);
hEdit = GetDlgItem(window,IDC_MAIN_EDIT);
SetWindowPos(hEdit, 0,0,0,RectSize.right,RectSize.bottom,SWP_NOZORDER);
//Process messages
while(GetMessage(&msg,0,0,0) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Pressing ESC while the Edit is in focus does not generate a WM_COMMAND message to the Edit's parent window. It generates WM_KEYDOWN, WM_KEYUP, WM_CHAR and WM_UNICHAR messages to the Edit window itself.
Update: You are only handling messages that are destined for the parent window. You need to assign a message procedure to the edit window instead, eg:
WNDPROC lpEditWndProc;
LRESULT CALLBACK MyEditCallBckProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if( (uMsg == WM_CHAR) && (wParam == VK_ESCAPE) )
{
PostQuitMessage(0);
return 0;
}
return CallWindowProc(lpEditWndProc, hWnd, uMsg, wParam, lParam);
}
...
HWND hEdit = CreateWindowEx(...);
#ifdef _WIN64
lpEditWndProc = (WNDPROC) SetWindowLongPtr(hEdit, GWLP_WNDPROC, (LONG_PTR)&MyEditCallBckProcedure);
#else
lpEditWndProc = (WNDPROC) SetWindowLongPtr(hEdit, GWL_WNDPROC, (LONG_PTR)&MyEditCallBckProcedure);
#endif
Alternatively:
LRESULT CALLBACK MyEditCallBckProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
if( (uMsg == WM_CHAR) && (wParam == VK_ESCAPE) )
{
PostQuitMessage(0);
return 0;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
...
HWND hEdit = CreateWindowEx(...);
SetWindowSubclass(hEdit, &MyEditCallBckProcedure, 0, 0);