How can I add some text line in my window? - c++

I am able to create a window with a Tile. How can I add now new text line inside the Window ?
All what I succeed to did was only to change the title of the window which is not what I want . I want to add some text line in the window box.
SendMessage function was not working for me.
Please if somebody has some tip for this to tell me !
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
//The Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
// Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Title of window",
WS_OVERLAPPEDWINDOW,
1390, 540, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}

To draw text in the client area, your wndProc would normally use something like DrawText or TextOut. You typically do that in response to WM_PAINT.
To be able to respond to an external message, you'd typically send a message containing the text. The window would receive that, store (a copy of) the text it received, and (usually) invalidate the window's rectangle. Since the window is now invalidated, the next chance it gets, Windows will send your window a WM_PAINT message (and then you'll draw out the text).

Handling the WM_PAINT message and drawing the text directly on the window's HDC is one option.
Another option is to create a child STATIC control in your window, and then you can assign the desired text to that child using SetWindowText() or the WM_SETTEXT message. No manual drawing needed.

In the end I figure out how to finish this:
win32 app picture
#ifndef UNICODE
#define UNICODE
#endif
using namespace std;
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
int X_Coordinate = 215;
int Y_Coordinate = 415;
int Width = 700;
int Height = 500;
char Text[] = {"abc123"};
char Window_Title[] = "My title";
char Window_Image[] = "D:\\bitmap1.bmp";
const char* csWindow_Title = Window_Title;
const char* csWindow_Image = Window_Image;
HBITMAP bitmap; // Creates bitmap object based on a handle to a Windows Windows Graphics Device Interface (GDI) bitmap and a handle to a GDI palette.
// Utilities
bool ConvertConstChartoLPWSTR (const char* as , wchar_t* wString )
{
memset(wString,0,sizeof(wString));
MultiByteToWideChar(CP_ACP, 0, as, -1, wString, 4096);
return wString;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
//Registering the Window Class
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255)); // set window background color ( RGB ) - white
RegisterClass(&wc); // register the window class with the operating system
wchar_t* wWindow_Title=new wchar_t[4096];
memset(wWindow_Title,0,sizeof(wWindow_Title)); // init variable
ConvertConstChartoLPWSTR(csWindow_Title,wWindow_Title); // convert
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
wWindow_Title, // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
X_Coordinate, Y_Coordinate, Width, Height,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// ----------------------------- START -> SWITCH case -----------------------------------------------------
switch (uMsg)
{
// ----------------------------- START -> case WM_DESTROY -------------------------------------------------
case WM_DESTROY:
PostQuitMessage(0);
return 0;
// ----------------------------- END -> case WM_DESTROY ---------------------------------------------------
// ----------------------------- START -> case WM_PAINT ---------------------------------------------------
case WM_PAINT:
{
wchar_t* wWindow_Image=new wchar_t[4096];
memset(wWindow_Image,0,sizeof(wWindow_Image));
ConvertConstChartoLPWSTR(csWindow_Image,wWindow_Image); // convert
bitmap=(HBITMAP)LoadImage(NULL, // A handle to the module that contains the image to be loaded. To load a stand-alone resource (icon, cursor, or bitmap file)—for example, c:\myimage.bmp — set this parameter to NULL
wWindow_Image, // The image to be loaded.
IMAGE_BITMAP, // The type of image to be loaded.
690, // The width, in pixels, of the icon or cursor.
540, // he height, in pixels, of the icon or cursor.
LR_LOADFROMFILE); //Loads the stand-alone image from the file specified by lpszName (icon, cursor, or bitmap file).
PAINTSTRUCT ps; // declare structure with information for an application
HDC hdc = BeginPaint(hwnd, &ps); // prepare the specified window for painting
int index = sizeof(Text);
HDC hMemDC=CreateCompatibleDC(hdc); // create a compatible DC ( hMemDC ) o be the same like another one ( hdc )
::SelectObject(hMemDC,bitmap); // Selects an object into the specified device context (DC). The new object replaces the previous object of the same type.
long retval=SetTextAlign(hdc,TA_TOP); // alignment of written area
const char* theval;
int u = 5;
for(int b = 0; b < sizeof(Text); b++)
{
string sym(1, Text[b]); // convert char to const char*
theval = sym.c_str();
cout<<b<<theval;
wchar_t wtext[sizeof(Text)];
memset(wtext,0,sizeof(wtext));
ConvertConstChartoLPWSTR(theval,wtext); // convert
// Here application is laid out.
TextOut (hdc, 5, u, wtext, sizeof(Text));
u = u + 15;
}
index = index + u; // claculate the size of written area
BitBlt( hdc, // handler
0, // The x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
index, // The y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
700, // The width, in logical units, of the source and destination rectangles.
980, // The height, in logical units, of the source and the destination rectangles.
hMemDC, // handler for source ( image ).
0, // The x-coordinate, in logical units, of the upper-left corner of the source rectangle.
0, // The y-coordinate, in logical units, of the upper-left corner of the source rectangle.
SRCCOPY ); // A raster-operation code. These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color.
// SRCCOPY - Copies the source rectangle directly to the destination rectangle.
EndPaint(hwnd, &ps); // function marks the end of painting in the specified window
}
return 0;
// ----------------------------- END -> case WM_PAINT ---------------------------------------------------
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
// ----------------------------- END -> SWITCH case -----------------------------------------------------
} // END -> LRESULT CALLBACK WindowProc

Related

snap layouts menu doesn't appear on windows 11 for custom maximize button

My goal is to create a custom maximize button for my app and I also want to trigger snap layouts menu on the button on Windows 11. For that, I am following the guide from here, but it doesn't work.
Here is the sample code, For testing purpose, I take a rectangle {point(0,0), size(200,200)} and when the mouse is over it and I receive WM_NCHITTEST message I return the HTMAXBUTTON as instructed in the guide but the snap layouts menu doesn't show up.
#include <stdio.h>
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <windowsx.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Test Window", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
case WM_NCHITTEST:
{
// Get the point in screen coordinates.
// GET_X_LPARAM and GET_Y_LPARAM are defined in windowsx.h
POINT point = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
// Map the point to client coordinates.
::MapWindowPoints(nullptr, hwnd, &point, 1);
RECT maximizeRect {0, 0, 200, 200};
// If the point is in your maximize button then return HTMAXBUTTON
if (::PtInRect(&maximizeRect, point))
{
printf("maxmize button rect %d\n", rand());
fflush(stdout);
return HTMAXBUTTON;
}
break;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
At the beginning, custom title bar before supporting the snap layouts menu
maximizeRect is client coordinates and not the maximize button rectangle.
The WM_NCHITTEST return value is wrong. If the point is in your maximize button then return HTMAXBUTTON.
There is a HitTestNCA example which calculates in the screen coordinate instead of the client coordinate and works fine. Another win32-window-custom-titlebar sample.

Painting colored text to c++ win32

Is it possible to paint colored text to what I've already done?
I've tried WM_CTLCOLORSTATIC, CreateSolidBrush(), and several other functions.
//-----------------------------------
// Learning the win32 API for C++
//
// Created by: Cosmic Cruizer
//-----------------------------------
#include <windows.h>
#include <tchar.h>
// Function declarations
bool SetUpWindowClass (char*, int, int, int); // Remove window structure from WinMain and put into function
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) // Pre-declare Windows procedure/function
// Global variables
const char CLASS_NAME[] = "My Window Class Array"; // declared for window class; Can be static or const
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){
//Step 1: Registering the Window Class
HWND hwnd{}; // This is the handle for our window
MSG msg{}; // Handle for messages to the application
SetUpWindowClass (NULL, NULL, NULL, NULL); // The Window structure - removed from main and made into a function
// Step 2: Creating the Window
hwnd = CreateWindowEx( // returns a handle to the new window, or zero if the function fails
WS_EX_CLIENTEDGE, // Optional window styles. Can be set to 0
CLASS_NAME, // Name of window class, see set 1b. Also set as the constant
"My First C++ Windows App", // Window title text
WS_OVERLAPPEDWINDOW, // Window style, title bar, a border, a system menu, and Minimize and Maximize buttons.
200, 200, 500, 400, // Size and position
NULL, NULL, hInstance, NULL); // Parent window, Menu, Instance handle, Additional app data
// Add an exit button
CreateWindow(
"BUTTON", // Predefined class; Unicode assumed
"EXIT", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
200, 200, 60, 25, // x position, y position, Button width, Button height
hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL); // Pointer not needed.
ShowWindow(hwnd, nCmdShow); // Make the window visible on the screen
// Step 3: The Message Loop
while(GetMessage(&msg, NULL, 0, 0) != 0) { // Run the message loop. It will run until GetMessage() returns 0
TranslateMessage(&msg); // Translate virtual-key messages into character messages
DispatchMessage(&msg); // Send message to WindowProcedure
}
return msg.wParam;
}
//---------- Functions ----------//
// Setup the window structure
bool SetUpWindowClass (char *cpTitle, int iR, int iG, int iB) {
//Step 1a: The Window structure
WNDCLASSEX wc{}; // Data structure for the windowclass
wc.cbSize = sizeof(WNDCLASSEX); // Sets the size of the Windows API
wc.style = 0; // define additional elements of the window class
wc.lpfnWndProc = WndProc; // defines most of the behavior of the window. See setp 4 "LRESULT CALLBACK WndProc" function
wc.cbClsExtra = 0; // No extra bytes after the window class
wc.cbWndExtra = 0; // structure for the window instance
wc.hInstance = GetModuleHandle (NULL); // handle to the application instance.
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // handle to icon class, if NULL, system provides a default icon.
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // handle to cursor class
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+18); // Add color as the background of the window
wc.lpszMenuName = NULL; // No menu
wc.lpszClassName = CLASS_NAME; // string that identifies the window class
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//Step 1b: Register the window class, and if it fails quit the program
if (RegisterClassEx (&wc)) return true;
else return false;
}
// Step 4: the Window Procedure in this function
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
switch(uMsg){
case WM_CLOSE:{
DestroyWindow(hwnd);
break;
}
case WM_COMMAND:{ // Close the window when exit is pressed
if (MessageBox(hwnd, "Really quit?", "Exit Warning", MB_OKCANCEL) == IDOK){ // what the hell, just wanted this.
PostQuitMessage(0);
}
break;
}
case WM_DESTROY:{
PostQuitMessage(0);
break;
}
//--- trying to create colored text ---//
case WM_CTLCOLORSTATIC:{
HDC hdcStatic = (HDC) wParam; // handle to display context
hwnd = (HWND) lParam; // handle to control window
SetTextColor(hdcStatic, RGB(100,255,255));
SetBkColor(hdcStatic, RGB(250,250,6));
return (INT_PTR)CreateSolidBrush(RGB(250,250,100));
}
case WM_CTLCOLOREDIT:{
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(0,0,255));
SetBkColor(hdcStatic, RGB(0,230,0));
return (INT_PTR)CreateSolidBrush(RGB(0,230,0));
}
case WM_PAINT:{ // All painting (text) occurs here, between BeginPaint and EndPaint.
PAINTSTRUCT ps; // Holds info about current painting session.
HDC hdc = BeginPaint(hwnd, &ps); // Create the device context (DC)
// Each character is added to the cpaText array. Then the for loop goes through and paints each character
int iY = 7; // Vertical spaces and number of lines for the array
const char *cpaText [iY] = { // changed from char to const char to get rid of warning. and added 1 for each line and a return
"Hello Peoples",
"",
"This is my first attempt to program using the Win32 API.",
"I can only hope it gets better with time.",
"",
"Created by \"The\" Cosmic Cruizer"
};
for (int iLoopCounter = 0; cpaText [iLoopCounter] != 0; iLoopCounter++, iY += 20) {
TextOut (hdc, 5, iY, cpaText [iLoopCounter], strlen (cpaText [iLoopCounter]));
}
EndPaint(hwnd, &ps); // Free up HDC created with BeginPaint
break;
}
default:{
return DefWindowProc(hwnd, uMsg, wParam, lParam); // Return is needed either here or at the end
break;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam); // Return is needed either here or in the default case
}
WM_CTLCOLORSTATIC and WM_CTLCOLOREDIT are notification messages used by STATIC/EDIT controls, neither of which you have on your window, so you will never receive those messages and should remove those handlers from your code.
You are trying to draw colored text directly onto your window using TextOutA() in a WM_PAINT handler, which is fine. But per the TextOutA() documentation:
The TextOut function writes a character string at the specified location, using the currently selected font, background color, and text color.
Your WM_PAINT handler is not selecting anything into the HDC that BeginPaint() returns, before trying to draw on it. It simply needs to configure the desired font/color values as desired, eg:
HFONT hFont;
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
switch(uMsg){
...
case WM_CREATE:{
hFont = CreateFont(...); // or CreateFontIndirect()
break;
}
case WM_DESTROY:{
DeleteObject(hFont);
PostQuitMessage(0);
break;
}
case WM_SETTINGCHANGE:
case WM_FONTCHANGE:{
DeleteObject(hFont);
hFont = CreateFont(...); // or CreateFontIndirect()
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case WM_PAINT:{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HFONT hOldFont = (HFONT) SelectObject(hdc, hFont);
SetTextColor(hdc, ...);
SetBkColor(hdc, ...);
int iY = 7;
const char *cpaText [iY] = {
"Hello Peoples",
"",
"This is my first attempt to program using the Win32 API.",
"I can only hope it gets better with time.",
"",
"Created by \"The\" Cosmic Cruizer"
};
for (int iLoopCounter = 0; cpaText [iLoopCounter] != 0; iLoopCounter++, iY += 20) {
TextOutA (hdc, 5, iY, cpaText [iLoopCounter], strlen (cpaText [iLoopCounter]));
}
SelectObject(hdc, hOldFont);
EndPaint(hwnd, &ps);
return 0;
}
...
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

How do I show controls on top of a gradient background with pure WINAPI?

In my program I am trying to create a child window (via CreateWindow("STATIC", ...)) to contain some other controls such as edit boxes and buttons. However, I want the background for this static control to be a gradient.
My goal is to have something that looks like this as a child window:
My efforts so far have yielded the window being created with controls visible but as soon as it is redrawn with WM_ERASEBKGND, the controls are hidden behind the drawn gradient.
I can find plenty of examples on drawing a gradient (or other such graphic backgrounds) and also, independently, creating a window with controls. But I have yet to find any resource of having both at the same time.
Here is my example code:
#include <windows.h>
#pragma comment( lib, "Msimg32" ) // load that dll for GradientFill
// Global macros
inline COLOR16 ToColor16(BYTE byte) { return byte << 8; }
inline COLOR16 RVal16(COLORREF color) { return ToColor16(GetRValue(color)); }
inline COLOR16 GVal16(COLORREF color) { return ToColor16(GetGValue(color)); }
inline COLOR16 BVal16(COLORREF color) { return ToColor16(GetBValue(color)); }
// Global variables
HINSTANCE hInst;
// Forward declarations
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
// Entry point
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const TCHAR CLASS_NAME[] = "mcvewinapi";
WNDCLASS wc = { };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
"MCVE WINAPI", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
300, //horizontal position
50, //vertical position
700, //width
500, //height
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
hInst = hInstance;
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// vill geven area with vertical gradient
void VerticalGradient(HDC hDC, const RECT &RectToFill, COLORREF rgbTop, COLORREF rgbBottom) {
GRADIENT_RECT gradRect;
TRIVERTEX TriVert[2];
// references for the verticies
gradRect.UpperLeft = 0; // point to TriVert[0]
gradRect.LowerRight = 1; // point to TriVert[1]
//setup top of gradient attributes
TriVert[0].x = RectToFill.left - 1;
TriVert[0].y = RectToFill.top - 1;
TriVert[0].Red = RVal16(rgbTop);
TriVert[0].Green = GVal16(rgbTop);
TriVert[0].Blue = BVal16(rgbTop);
TriVert[0].Alpha = 0x0000;
//setup bottom of gradient attributes
TriVert[1].x = RectToFill.right;
TriVert[1].y = RectToFill.bottom;
TriVert[1].Red = RVal16(rgbBottom);
TriVert[1].Green = GVal16(rgbBottom);
TriVert[1].Blue = BVal16(rgbBottom);
TriVert[1].Alpha = 0x0000;
// draw the shaded rectangle
GradientFill(hDC, TriVert, 2, &gradRect, 1, GRADIENT_FILL_RECT_V);
}
// discover area to fill with gradient
void vFill(HWND ctrlhwnd) {
HDC gBoxDC;
HWND gBoxH;
RECT gBoxR;
POINT xWhere;
// get rectangle from control
gBoxH = ctrlhwnd;
GetWindowRect(gBoxH, &gBoxR);
// get DC for control
gBoxDC = GetDC(gBoxH);
// load up RECT from POINT
xWhere = { gBoxR.left, gBoxR.top };
ScreenToClient(gBoxH, &xWhere);
gBoxR.left = xWhere.x + 2;
gBoxR.top = xWhere.y + 2;
// load up RECT from POINT
xWhere = { gBoxR.right, gBoxR.bottom };
ScreenToClient(gBoxH, &xWhere);
gBoxR.right = xWhere.x - 1;
gBoxR.bottom = xWhere.y - 1;
//paint area
VerticalGradient(gBoxDC, gBoxR, RGB(250, 191, 145), RGB(191, 191, 191));
ReleaseDC(gBoxH, gBoxDC);
}
// Processes messages for the main window.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
struct wAnchor {
int udPos;
int lrPos;
int width;
int height;
};
wAnchor Section1[4], TextLine;
unsigned int LineHeight, k;
HWND FillBox;
LineHeight = 24;
Section1[1].lrPos = 5;
Section1[1].udPos = 20;
Section1[1].width = 325;
Section1[1].height = 360;
TextLine.lrPos = Section1[1].lrPos + 5;
TextLine.udPos = Section1[1].udPos + 5;
TextLine.width = 0;
TextLine.height = LineHeight;
k = 1;
FillBox = CreateWindow("STATIC",
"",
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_EX_CLIENTEDGE,
Section1[1].lrPos,
Section1[1].udPos,
Section1[1].width,
Section1[1].height,
hWnd, // child of parent
(HMENU)8200,
hInst,
NULL);
CreateWindow("STATIC",
"Section title",
WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER | WS_EX_CLIENTEDGE,
TextLine.lrPos,
TextLine.udPos + (TextLine.height * k++),
TextLine.width + 315,
TextLine.height,
FillBox, // child of FillBox
(HMENU)8201,
hInst,
NULL);
CreateWindow("STATIC",
"Entry:",
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_EX_CLIENTEDGE,
TextLine.lrPos,
TextLine.udPos + (TextLine.height * k),
TextLine.width + 75,
TextLine.height,
FillBox, // child of FillBox
(HMENU)8202,
hInst,
NULL);
CreateWindow("EDIT", // edit bos
"109",
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_MULTILINE,
TextLine.lrPos + 75,
TextLine.udPos + (TextLine.height * k),
TextLine.width + 35,
TextLine.height,
FillBox, // child of FillBox
(HMENU)8203,
hInst,
NULL);
CreateWindow(
"BUTTON", // Predefined class; Unicode assumed
"test", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
50, // x position
150, // y position
50, // Button width
30, // Button height
FillBox, // child of FillBox
(HMENU)8204, // No menu
hInst,
NULL); // Pointer not needed
UpdateWindow(FillBox);
break;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_ERASEBKGND:
vFill(GetDlgItem(hWnd, 8200)); //fill background with gradient
return 1L;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Initially, no gradient is drawn until I resize the window, then the gradient covers the controls. Of course, I would like the gradient to be redrawn as needed, but the controls to remain visible.
How do I accomplish this?
You can add the WS_CLIPCHILDREN style to the parent window:
FillBox = CreateWindow(TEXT("STATIC"),
TEXT(""),
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_EX_CLIENTEDGE | WS_CLIPCHILDREN,
Section1[1].lrPos,
Section1[1].udPos,
Section1[1].width,
Section1[1].height,
hWnd, // child of parent
(HMENU)8200,
hInst,
NULL);
This will not include its child windows when drawing the parent window, which works for me:
If you're going to create a window to act as a container for some controls, the obvious choice is usually to create a dialog. It already (automatically) handles most of what you're doing right now, and a few things you might want to (like using the tab to move between controls) but haven't yet.
So, for example, starting from a generic Windows app, I changed the default About box to host roughly the controls you wanted, then popped it up as a modeless dialog when the application initialized.
// frame.cpp : Defines the entry point for the application.
//
#include "framework.h"
#include "frame.h"
#pragma comment( lib, "Msimg32" ) // load that dll for GradientFill
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_FRAME, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FRAME));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FRAME));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_FRAME);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND dialog;
switch (message)
{
case WM_CREATE:
dialog = CreateDialog(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
ShowWindow(dialog, SW_SHOW);
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
inline COLOR16 ToColor16(BYTE byte) { return byte << 8; }
inline COLOR16 RVal16(COLORREF color) { return ToColor16(GetRValue(color)); }
inline COLOR16 GVal16(COLORREF color) { return ToColor16(GetGValue(color)); }
inline COLOR16 BVal16(COLORREF color) { return ToColor16(GetBValue(color)); }
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_ERASEBKGND: {
COLORREF rgbTop(RGB(250, 191, 145));
COLORREF rgbBottom(RGB(191, 191, 191));
RECT RectToFill;
GetClientRect(hDlg, &RectToFill);
GRADIENT_RECT gradRect;
TRIVERTEX TriVert[2];
// references for the verticies
gradRect.UpperLeft = 0; // point to TriVert[0]
gradRect.LowerRight = 1; // point to TriVert[1]
//setup top of gradient attributes
TriVert[0].x = RectToFill.left - 1;
TriVert[0].y = RectToFill.top - 1;
TriVert[0].Red = RVal16(rgbTop);
TriVert[0].Green = GVal16(rgbTop);
TriVert[0].Blue = BVal16(rgbTop);
TriVert[0].Alpha = 0x0000;
//setup bottom of gradient attributes
TriVert[1].x = RectToFill.right;
TriVert[1].y = RectToFill.bottom;
TriVert[1].Red = RVal16(rgbBottom);
TriVert[1].Green = GVal16(rgbBottom);
TriVert[1].Blue = BVal16(rgbBottom);
TriVert[1].Alpha = 0x0000;
HDC dc = GetDC(hDlg);
GradientFill(dc, TriVert, 2, &gradRect, 1, GRADIENT_FILL_RECT_V);
ReleaseDC(hDlg, dc);
return TRUE;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
I didn't really complete the transformation from it being a modal dialog to modeless (e.g., a modeless dialog usually won't have an OK button, or code to handle its being pressed, but it's still enough to get the idea of how it can look:

Icon in Windows Explorer showing 2 different icons when using resource.rc

I'm trying to change the icon of my Windows application. I'm using the simple window code below and added a resource.h and resource.rc to the solution. I'm using a test.ico and a test2.ico-file which both contain 64x64, 32x32, 24x24 and 16x16 sizes.
The when I switch between icon and icon2 by simple changing this line TESTICON ICON "test.ico" to TESTICON ICON "test2.ico" in the .rc-file, the icons change accordingly in program header bar, taskbar and alt-tab view. But the icon in my windows explorer is acting totally strange. When I set the view to details,list or small I see the test.ico-icon, but for medium, large and extra large I see the test2.ico-icon, no matter what I set in the .rc-file. I'm totally lost here, which setting am I missing?
The 'resource.h'
#pragma once
#define TESTICON 1
and the 'resource.rc'
#include "resource.h"
TESTICON ICON "test.ico"
The simple windows source:
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASSEX wc = { };
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = CLASS_NAME;
wc.hIcon = (HICON)LoadImage(hInstance,
MAKEINTRESOURCE(TESTICON), IMAGE_ICON,
::GetSystemMetrics(SM_CXICON),
::GetSystemMetrics(SM_CYICON), 0);
wc.hIconSm = (HICON)LoadImage(hInstance,
MAKEINTRESOURCE(TESTICON), IMAGE_ICON,
::GetSystemMetrics(SM_CXSMICON),
::GetSystemMetrics(SM_CYSMICON), 0);
RegisterClassEx(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
As pointed out by Jonathan Potter: Explorer has an icon cache; if it's showing the wrong icon you probably need to clear the cache.
http://leodavidson.blogspot.com/2011/05/clear-icon-cache-1001.html

Why is WinAPI not creating the window with the configured size?

I am fairly new to C++ coding so this could be a really easy fix. I have recently created a basic WinMain window. When I run the program from either the IDE or .exe file the application doesn't open at the proper size.
I can resize the window but I'm not sure why it isn't opening at that size set.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
// Function prototypes
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
bool CreateMainWindow(HINSTANCE, int);
LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
// global variable
HINSTANCE hinst;
// Constants
const char CLASS_NAME[] = "WinMain";
const char APP_TITLE[] = "Hello World";
const char WINDOW_WIDTH = 400;
const char WINDOW_HEIGHT = 400;
//==================================
// Starting point for the windows application
// Parameters are
// hInstance. Handle to the current instance of the application
// hPrevInstance. Always NULL, obsolete parameter
// lpCmdLine. Pointer to null-terminated string of command arguements
// nCmdShow. Specifies how the window is to be shown
//=================================
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
MSG msg;
// Create thw window
if (!CreateMainWindow(hInstance, nCmdShow))
return false;
// Main message loop
int done = 0;
while (!done)
{
// PeekMessage is a non blocking message for Windows messages
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Look for quit message
if (msg.message == WM_QUIT)
done = 1;
// Decode and pass messages on to WinProc
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
// Window event callback function
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
//Tell windows to kill this program
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
// Create the window, returns False on error
bool CreateMainWindow(HINSTANCE hInstance, int nCmdShow)
{
WNDCLASSEX wcx;
HWND hwnd;
// Fill in the window class structure with parameters
// That describe the main window
wcx.cbSize = sizeof(wcx); // Size of the structure
wcx.style = CS_HREDRAW | CS_VREDRAW; // Redraw if the size changes
wcx.lpfnWndProc = WinProc; // Points to windows procedure
wcx.cbClsExtra = 0; // No extra class memory
wcx.cbWndExtra = 0; // No extra window memory
wcx.hInstance = hInstance;
wcx.hIcon = NULL;
wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // Predifined arrow
// Background brush
wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcx.lpszMenuName = NULL; // Name of menu resource
wcx.lpszClassName = CLASS_NAME; // Name of window class
wcx.hIconSm = NULL;
// Register the window class
// RegisterClassEx return 0 on error
if (RegisterClassEx(&wcx) == 0) // if error
return false;
// Create Window
hwnd = CreateWindow(
CLASS_NAME, // Name of window class
APP_TITLE, // Title bar text
WS_OVERLAPPEDWINDOW, // Window style
CW_USEDEFAULT, // Default horizontal postion of window
CW_USEDEFAULT, // Default vertical postion of window
WINDOW_WIDTH, // Width of window
WINDOW_HEIGHT, // Height of window
(HWND) NULL, // No parent window
(HMENU) NULL, // No menu
hInstance, // Handle to application window
(LPVOID) NULL); // No window parameters
// If there was an error the window
if (!hwnd)
return false;
// Show the window
ShowWindow(hwnd, nCmdShow);
// Send a WM_PAINT message to the window procedure
UpdateWindow(hwnd);
return true;
}
Change the two constant definitions from char to int:
const int WINDOW_WIDTH = 400;
const int WINDOW_HEIGHT = 400;
Assuming a signed character is 8-bits then char x = 400 actually sets x to 16.