-
July 19th, 2018, 12:38 PM
#1
Changing the color of elements - Win API
I have some problems when working with Win32 api.
1. Changing the text and background color of a button doesn't take effect.
My code:
Code:
case WM_CTLCOLORBTN:
COLORREF colorref;
colorref = RGB(0,0,255);
HBRUSH hBrushBtn;
hBrushBtn = CreateSolidBrush(colorref);
return ((LRESULT)hBrushBtn);
break;
2. How do I create selectable text.
My code:
Code:
text = CreateWindow("STATIC","text",WS_VISIBLE | WS_CHILD , 0,0,100,60, hwnd , NULL, NULL, NULL);
3. How do I create a filled rectangle?
-
July 19th, 2018, 01:00 PM
#2
Re: Changing the color of elements - Win API
1. You must use owner/custom draw for the button.
2. You could use "EDIT" class with the style ES_READONLY instead.
3. ???
Victor Nijegorodov
-
July 19th, 2018, 02:17 PM
#3
Re: Changing the color of elements - Win API
I searched the internet , I don't seem to find any examples. Can you show me an example of this
-
July 19th, 2018, 02:40 PM
#4
Re: Changing the color of elements - Win API
Victor Nijegorodov
-
July 19th, 2018, 03:46 PM
#5
Re: Changing the color of elements - Win API
For creating a filled rectangle I have this code:
Code:
hdc=CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
rect = {0, 0, 500,500};
brush = CreateSolidBrush(RGB(50, 151, 151));
FillRect(hdc, &rect, brush);
DeleteObject(brush);
This goes in "case WM_CREATE:"
The problem is that the rectangle is created at x = 0, y = 0 relative to the screen instead of the window
-
July 19th, 2018, 03:58 PM
#6
Re: Changing the color of elements - Win API
You can use ScreenToClient and/or ClientToScreen functions to convert between the screen and the window coordinates
Victor Nijegorodov
-
July 20th, 2018, 08:04 AM
#7
Re: Changing the color of elements - Win API
I created a button with owner draw, but it doesn't display the button
Code:
Code:
button = CreateWindow("BUTTON","Overview",WS_VISIBLE | WS_CHILD | BS_OWNERDRAW, 0,20,200,50, hwnd , (HMENU) 2 , NULL, NULL);
Code:
case WM_CTLCOLORBTN:
{
LPDRAWITEMSTRUCT Item;
Item = (LPDRAWITEMSTRUCT)lParam;
SelectObject(Item->hDC, CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial Black"));
FillRect(Item->hDC, &Item->rcItem, CreateSolidBrush(0));
SelectObject(Item->hDC, CreateSolidBrush(0));
if (Item->itemState & ODS_SELECTED)
{
SetTextColor(Item->hDC, 0);
SelectObject(Item->hDC, CreateSolidBrush(0xFF00));
SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0xFF00));
}
else
{
SetTextColor(Item->hDC, 0x00FF00);
SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0x00FF00));
}
SetBkMode(Item->hDC, TRANSPARENT);
RoundRect(Item->hDC, Item->rcItem.left, Item->rcItem.top, Item->rcItem.right, Item->rcItem.bottom, 20, 20);
int len;
len = GetWindowTextLength(Item->hwndItem);
LPSTR lpBuff;
lpBuff = new char[len+1];
GetWindowTextA(Item->hwndItem, lpBuff, len+1);
DrawTextA(Item->hDC, lpBuff, len, &Item->rcItem, DT_CENTER);
}
break;
-
July 20th, 2018, 08:56 AM
#8
Re: Changing the color of elements - Win API
You have to handle WM_DRAWITEM message, not a WM_CTLCOLORBTN one.
See exampe in
https://www.daniweb.com/programming/...-draw-controls
Victor Nijegorodov
-
July 20th, 2018, 10:07 AM
#9
Re: Changing the color of elements - Win API
Thank you!
I'm creating several "EDIT" elements with a for, when I move the window outside the screen and inside again the "EDIT" elements disappear along with a line in the WM_PAINT event
The "create" event:
Code:
case WM_CREATE:
for( int i =0; i < 4; i++ ){
text = CreateWindow("EDIT","text",WS_VISIBLE | WS_CHILD , 0,i*40+100,100,10, hwnd , NULL, NULL, NULL);
}
// Other elements
break;
The "paint" event:
Code:
case WM_PAINT:
{
hdc = GetDC(hwnd);
SetRect(&secondbackground, 0, 70, 800, 500);
FillRect(hdc, &secondbackground, CreateSolidBrush(RGB(0,200,200)) );
ReleaseDC(hwnd, hdc);
hdc = GetDC(hwnd);
SetRect(&secondbackground, 0, 0, 800, 20);
FillRect(hdc, &secondbackground, CreateSolidBrush(RGB(0,100,200)) );
ReleaseDC(hwnd, hdc);
hdc = BeginPaint( hwnd, &ps );
MoveToEx(hdc,1,52,NULL);
LineTo(hdc,100,200);
EndPaint( hwnd, &ps );
hdc = GetDC(hwnd);
SetPixel(hdc,300,300,RGB(255,255,255));
EndPaint( hwnd, &ps );
}
break;
-
July 20th, 2018, 11:44 AM
#10
Re: Changing the color of elements - Win API
Originally Posted by binary2
Thank you!
I'm creating several "EDIT" elements with a for, when I move the window outside the screen and inside again the "EDIT" elements disappear along with a line in the WM_PAINT event
...
Perhaps, you paint your rects over the edit controls?
Victor Nijegorodov
-
July 20th, 2018, 11:57 AM
#11
Re: Changing the color of elements - Win API
The paint event is under the create event
My Window Procedure is structured like this:
Code:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
for( int i =0; i < 4; i++ ){
text = CreateWindow("EDIT","asd",WS_VISIBLE | WS_CHILD , 0,i*20+100,100,20, hwnd , NULL, NULL, NULL);
}
break;
case WM_PAINT:
{
hdc = GetDC(hwnd);
SetRect(&secondbackground, 0, 0, 800, 500);
FillRect(hdc, &secondbackground, CreateSolidBrush(RGB(255,255,255)) );
ReleaseDC(hwnd, hdc);
hdc = BeginPaint( hwnd, &ps );
MoveToEx(hdc,1,52,NULL);
LineTo(hdc,100,200);
EndPaint( hwnd, &ps );
hdc = GetDC(hwnd);
SetPixel(hdc,300,300,RGB(255,255,255));
EndPaint( hwnd, &ps );
}
break;
case WM_CTLCOLORSTATIC:
break;
case WM_DRAWITEM:
break;
case WM_CTLCOLOREDIT:
break;
case WM_COMMAND:
switch ( LOWORD(wParam) ){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
-
July 20th, 2018, 12:10 PM
#12
Re: Changing the color of elements - Win API
Originally Posted by binary2
The paint event is under the create event
My Window Procedure is structured like this:
You mean the sequence of commands/operators in your C++ code.
But it has nothing to with the Windows OS run-time!
The WM_PAINT message is sent (from MSDN) "... to a window procedure when changes to the window have altered the content of the client area..."
So every time you "move the window outside the screen and inside again" the WM_PAINT message is sent to a window procedure...
Last edited by VictorN; July 20th, 2018 at 12:11 PM.
Reason: link was added
Victor Nijegorodov
-
July 21st, 2018, 03:56 AM
#13
Re: Changing the color of elements - Win API
You don't use GetDC() in WM_PAINT message. Consider
Code:
hdc = BeginPaint( hwnd, &ps );
SetRect(&secondbackground, 0, 0, 800, 500);
FillRect(hdc, &secondbackground, CreateSolidBrush(RGB(255,255,255)) );
MoveToEx(hdc,1,52,NULL);
LineTo(hdc,100,200);
SetPixel(hdc,300,300,RGB(255,255,255));
EndPaint( hwnd, &ps );
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 21st, 2018, 11:50 AM
#14
Re: Changing the color of elements - Win API
Thank you!
Is there a way to set a border color and style to a "static" control? I'm interested in applying a dotted border to the top and bottom of the control
-
July 21st, 2018, 11:57 AM
#15
Re: Changing the color of elements - Win API
Originally Posted by binary2
Is there a way to set a border color and style to a "static" control? I'm interested in applying a dotted border to the top and bottom of the control
Hmm, interesting idea...
I guess you should handle the WM_NCPAINT message
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|