static text color and transparent background

Started by golite, October 03, 2013, 05:18:55 PM

Previous topic - Next topic

golite

I need an example of setting a static text background to transparent and setting the text color. I am learning to control every aspect of windows programming in c.
Please bear with me.
Thanks for all your help.

TimoVJL

#1
Search this forum and you can found this:
...
static HBRUSH hbBack = 0;
...
case WM_INITDIALOG:
hbBack = CreateSolidBrush(RGB(255, 255, 0));
...
case WM_CLOSE:
DeleteObject(hbBack);
...
case WM_CTLCOLORSTATIC:
//hdc = (HDC)wParam;
SetTextColor((HDC)wParam, RGB(255, 0, 0)); // red
SetBkColor((HDC)wParam, RGB(255, 255, 0)); // yellow
return (LRESULT)hbBack;
...
EDIT: better now ?
May the source be with you

jj2007

#2
Quote from: timovjl on October 03, 2013, 06:21:52 PM
         return (LRESULT)CreateSolidBrush(RGB(255, 255, 0));

You are not worried about a leak?

EDIT: Much better ;-)

golite

#3
... This is my complete start up code.
... Included is a picture of my window after I run this code.
... What am I doing wrong? I don't know the words to ask for the help I need sometimes.
... My static text is not transparent. Why?
... Be as critical and constructive as you want. I need a lot of help.


...DrawingInterface.h

// menu identifiers
#define IDM_FILE_NEW  1001
#define IDM_FILE_OPEN 1002
#define IDM_FILE_QUIT 1003
#define IDM_FILE_SAVEAS 1004

#define IDM_VIEW_TOOLBAR  2001 // toolbar area
#define IDM_VIEW_TV_TITLE  2002 // treeview title area
#define IDM_VIEW_TREEVIEW  2003 // treeview area
#define IDM_VIEW_TV_BUTTONS  2004  // treeview buttons area
#define IDM_VIEW_PB_TITLE  2005 // picturebox title area
#define IDM_VIEW_PICTUREBOX 2006 // picturebox area
#define IDM_VIEW_STATUSBAR  2007  // statusbar area
#define IDS_STATIC_TREE 3001 // Treeview Label

struct VisibleControls {
bool toolbarVisible;
bool treeviewTitleVisible;
bool treeviewVisible;
bool treeviewButtonsVisible;
bool pictureboxTitleVisible;
bool pictureboxVisible;
bool statusbarVisible;
}visCon;



... DrawingInterface.c
/*
* Author Steve Griffin
* Begin Sep. 28, 2013
* Create a basic user interface for my drawing applications.
* Will include menubar, toolbar, 2 labels, treeview, picturebox, button area and statusbar.
*
*/

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <commctrl.h>
#include <stdbool.h>
#include <stdio.h>
#include "DrawingInterface.h"

#pragma comment(linker, "-subsystem:console")
#pragma comment(linker, "-entry:wWinMainCRTStartup")

// prototypes
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void AddMenus(HWND);
void AddToolbar(HWND);
void hideShowTool(bool);
void AddTreeViewTitle(HWND);

// module level variables
HWND  hThisInstance;
HMENU hMenu;
HMENU hMenuView;

static HWND hTool  = NULL;
static HWND hLabel = NULL;

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow){


printf("The console will be my value checker log.\n");

//fclose(stdout);
    //*stdout = *fopen("c:\\debug.log", "w");
//printf ("This is the degug log for DrawingInterface.exe");

HBRUSH hb = CreateSolidBrush(RGB(0,128,128));
MSG msg;
WNDCLASSW wc = { 0 };
wc.lpszClassName = L"DrawingInterface";
wc.hInstance = hInstance;
wc.hbrBackground = hb;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);

RegisterClassW(&wc);
CreateWindowW(wc.lpszClassName, L"Drawing Interface", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 30, 400, 400, 0, 0, hInstance, 0);


while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){

// obligatory call - does nothing but cause win32.dll to recognize us
INITCOMMONCONTROLSEX iccex;
iccex.dwICC = ICC_WIN95_CLASSES;
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCommonControlsEx(&iccex);
UINT state;

static HBRUSH hbBack = 0;

switch (msg)
{
case WM_CREATE:
AddMenus(hwnd);
AddToolbar(hwnd);
AddTreeViewTitle(hwnd);
break;

case WM_SIZING:
//FitAreas(hwnd);
break;

case WM_EXITSIZEMOVE:
//RepositionPanels(hwnd);
break;

case WM_MOVE:
break;

case WM_INITDIALOG:
hbBack = CreateSolidBrush(RGB(255, 255, 0));

case WM_CLOSE:
DeleteObject(hbBack);

case WM_CTLCOLORSTATIC:
//hdc = (HDC)wParam;
SetTextColor((HDC)wParam, RGB(255, 0, 0)); // red
SetBkColor((HDC)wParam, RGB(255, 255, 0)); // yellow
return (LRESULT)hbBack;



case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_FILE_NEW:
break;
case IDM_FILE_OPEN:
Beep(50, 100);
break;
case IDM_FILE_QUIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;

case IDM_VIEW_TOOLBAR:
state = GetMenuState(hMenuView, IDM_VIEW_TOOLBAR, MF_BYCOMMAND);
                if (state == SW_SHOWNA) {
                hideShowTool(0);
                    CheckMenuItem(hMenuView, IDM_VIEW_TOOLBAR, MF_UNCHECKED); 
                } else {
                    hideShowTool(1);
                    CheckMenuItem(hMenuView, IDM_VIEW_TOOLBAR, MF_CHECKED); 
                }
break;

case IDM_VIEW_TV_TITLE:
break;
case IDM_VIEW_TREEVIEW:
break;
case IDM_VIEW_TV_BUTTONS:
break;
case IDM_VIEW_PB_TITLE:
break;
case IDM_VIEW_PICTUREBOX:
break;
case IDM_VIEW_STATUSBAR:
break;

}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;
}

return DefWindowProcW(hwnd, msg, wParam, lParam);
}

void AddMenus(HWND hwnd){
HMENU hMenubar;

hMenubar  = CreateMenu();
hMenu     = CreateMenu();
hMenuView = CreateMenu();

// file menu sub items
AppendMenuW(hMenu, MF_STRING, IDM_FILE_NEW,  L"&New");
AppendMenuW(hMenu, MF_STRING, IDM_FILE_OPEN, L"&Open");
AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenuW(hMenu, MF_STRING, IDM_FILE_QUIT, L"&Quit");


// view menu sub items
AppendMenuW(hMenuView, MF_STRING, IDM_VIEW_TOOLBAR, L"Toolbar");
AppendMenuW(hMenuView, MF_STRING, IDM_VIEW_TV_TITLE, L"TV Title");
AppendMenuW(hMenuView, MF_STRING, IDM_VIEW_TREEVIEW, L"Treeview");
AppendMenuW(hMenuView, MF_STRING, IDM_VIEW_TV_BUTTONS, L"TV Buttons");
AppendMenuW(hMenuView, MF_STRING, IDM_VIEW_PB_TITLE, L"PB Title");
AppendMenuW(hMenuView, MF_STRING, IDM_VIEW_PICTUREBOX, L"Picturebox");
AppendMenuW(hMenuView, MF_STRING, IDM_VIEW_STATUSBAR, L"Statusbar");

// turn menuitems into check menus
CheckMenuItem(hMenuView, IDM_VIEW_TOOLBAR,    MF_CHECKED);
CheckMenuItem(hMenuView, IDM_VIEW_TV_TITLE,   MF_CHECKED);
CheckMenuItem(hMenuView, IDM_VIEW_TREEVIEW,   MF_CHECKED);
CheckMenuItem(hMenuView, IDM_VIEW_TV_BUTTONS, MF_CHECKED);
CheckMenuItem(hMenuView, IDM_VIEW_PB_TITLE,   MF_CHECKED);
CheckMenuItem(hMenuView, IDM_VIEW_PICTUREBOX, MF_CHECKED);
CheckMenuItem(hMenuView, IDM_VIEW_STATUSBAR,  MF_CHECKED);

// file menu bar item
AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenu,     L"&File");
AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenuView, L"&View");

SetMenu(hwnd, hMenubar);


}

void AddToolbar(HWND hwnd)
{
// obligatory call - does nothing but cause win32.dll to recognize us
//INITCOMMONCONTROLSEX iccex;
//iccex.dwICC = ICC_WIN95_CLASSES;
//iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
//InitCommonControlsEx(&iccex);

hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);

// Send the TB_BUTTONSTRUCTSIZE message, which is required for
// backward compatibility.
SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

TBBUTTON tbb[3];
TBADDBITMAP tbab;

tbab.hInst = HINST_COMMCTRL;
tbab.nID = IDB_STD_SMALL_COLOR;
SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM) & tbab);

ZeroMemory(tbb, sizeof(tbb));
tbb[0].iBitmap = STD_FILENEW;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[0].idCommand = IDM_FILE_NEW;

tbb[1].iBitmap = STD_FILEOPEN;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
tbb[1].idCommand = IDM_FILE_OPEN;

tbb[2].iBitmap = STD_FILESAVE;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_BUTTON;
tbb[2].idCommand = IDM_FILE_SAVEAS;

SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb) / sizeof(TBBUTTON), (LPARAM) & tbb);



}



void hideShowTool(bool visibility){

if (visibility == 0) {
ShowWindow(hTool,SW_HIDE);
MessageBoxA(hThisInstance,"hidden","Hide Toolbar",MB_OK);
} else {
ShowWindow(hTool,SW_SHOW);
MessageBoxA(hThisInstance,"hidden","Hide Toolbar",MB_OK);
}
}


void AddTreeViewTitle(HWND hwnd)
{
static char *TreeViewTitle = " TREE VIEW TITLE";

hLabel = CreateWindow("STATIC", TreeViewTitle, WS_CHILD | WS_VISIBLE | SS_LEFT,
5, 40, 125, 18, hwnd, (HMENU) IDS_STATIC_TREE, NULL, NULL);

static HFONT hFont;
static int nSize = 18;

if (hFont) DeleteObject(hFont);
hFont = CreateFont(nSize, 0, 0, 0, FW_BOLD, 0, 0, 0, ANSI_CHARSET,
0, 0, 0, 0, TEXT("Comic Sans MS"));


SendDlgItemMessage(hwnd, IDS_STATIC_TREE, WM_SETFONT, (WPARAM)hFont, TRUE);


}









DMac

One thing that you need to do is move the following code:
// obligatory call - does nothing but cause win32.dll to recognize us
INITCOMMONCONTROLSEX iccex;
iccex.dwICC = ICC_WIN95_CLASSES;
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCommonControlsEx(&iccex);

Out of the proc and put it into WinMain().  It should be the first lines of the entry point.  Here's an example:
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    INITCOMMONCONTROLSEX icc;
    WNDCLASSEX wcx;

    ghInstance = hInstance;

    icc.dwSize = sizeof(icc);
    icc.dwICC = ICC_WIN95_CLASSES /*|ICC_COOL_CLASSES|ICC_DATE_CLASSES|ICC_PAGESCROLLER_CLASS|ICC_USEREX_CLASSES*/;
    InitCommonControlsEx(&icc);

    /* Get system dialog information */
    wcx.cbSize = sizeof(wcx);
    if (!GetClassInfoEx(NULL, MAKEINTRESOURCE(32770), &wcx))
        return 0;

    /* Add our own stuff */
    wcx.hInstance = hInstance;
    wcx.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_ICO_MAIN));
    wcx.lpszClassName = _T("SimpleGrClass");
    if (!RegisterClassEx(&wcx))
        return 0;

    /* The user interface is a modal dialog box */
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)Main_DlgProc);
}

The reason for this is that for every message that your window receives the proc gets called.  You do not want to initialize common controls 50 times a second while you resize your windows now do you?

As a rule of thumb.  Everything in the WndProc should be inside of a handled case.  Better yet, use message crackers to route each call to a seperate message handling method.
No one cares how much you know,
until they know how much you care.

DMac

#5
Concerning the color of texboxes, static, and other controls, This question came up some time ago Check here:
http://forum.pellesc.de/index.php?topic=3783.msg14031#msg14031

Quite a while back I posted a working demo that shows a technique for changing the color of the text box that has the focus.  Please look here:
http://forum.pellesc.de/index.php?topic=3713.msg13789#msg13789


One more example coded a little differently:

http://forum.pellesc.de/index.php?topic=3154.msg11944#msg11944
No one cares how much you know,
until they know how much you care.

TimoVJL

#6
Quote from: golite on October 04, 2013, 03:30:30 AM
... My static text is not transparent. Why?
Create that static hFont in WM_CREATE block.

WM_INITDIALOG is for DialogProcs.

BTW: Missing break after WM_CLOSE block.

EDIT:
If you want have brush that follow system colors, use GetSysColorBrush(nIndex); and no need to delete that object because it comes from system cache.
May the source be with you

golite

It works now. Thanks. I'll try to get well acclimated to this forum and all it's previous posts because I can see right now it's going to be my life saver.

golite

Just in case it might help someone else. You will get a better search result if you are in the root folder of the forum. When I did a search on "color" I found all the posts that were sited in the replies to me. If I had performed my search correctly the first I would have found most of the answers to my query. I was searching while in "Tips and Tricks" and only got a few posts. This is exactly what it points out in the help file when you click "Help" then "Search" but who reads help files? I thought I did.

I'll quit rambling now.

laurro

Another example using fonts, brushes, static controls and transparent backgrounds.

Laur

aMarCruz


    ...
    case WM_CTLCOLORSTATIC:
        if ((HWND) lParam == hwndTransparentBackgroundControl)
            SetBkMode((HDC) wParam, TRANSPARENT);
    ...


;)