Can i put my window code in function or header - c++

I have this code for crearing a window and i was wondering if i could put it in aheader or in a separate function to optimize my projects. And if so, could I manipulate its size and content from the separate program The code is:
#include <windows.h>
#include <windowsx.h>
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass1";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL, "WindowClass1",
"Our First Windowed Program",
WS_OVERLAPPEDWINDOW,
300,
300,
500,
400,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}

Related

LoadImage() returns NULL when trying to load bitmap image from disk

I have this directory structure:
---- test.cpp
---- test.exe (created after compiling test.cpp)
---- sample.bmp
I have this code, which (for the time being) reads the image sample.bmp and stores it in a HBITMAP handle:
#include <tchar.h>
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
{
HWND sampleImg = CreateWindowEx(0, "Static", "", WS_CHILD | WS_VISIBLE | SS_BITMAP, 0, 0, 0, 0, hWnd, NULL, NULL, NULL);
/* --------------HERE----------------- */
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, "D:\\Rest\\New Folder\\sample.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBitmap == NULL)
{
/* This block is reached, hBitmap is NULL */
MessageBox(NULL, "LoadImage failed", "Error", MB_OK);
return 0;
}
SendMessage(sampleImg, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX 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, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "Test";
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow("Test", "Test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
I tried to call GetLastError() and check the error code. The error code is 0 (ERROR_SUCCESS), implying there are no errors.
Could anyone tell me what is wrong with the code?
Directory structure of D:/Rest/New Folder:
I wanted to fetch a bitmap image, but the necessary routine returns NULL.

How to get window control by position (win32 API)?

Is there a way to get the control handle using the coordinate position of the control? In other words, is there a way to find the control in the window area provided that we know its coordinate position?
You can use WindowFromPoint:
Here is the sample:
#include <Windows.h>
#include <iostream>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR szCmdLine, _In_ int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
100,
100,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
POINT pt;
char bf[100];
HWND h;
HDC hdc;
RECT rc;
case WM_LBUTTONDOWN:
pt.x = 10;
pt.y = 10;
hdc = GetDC(hwnd);
h = WindowFromPoint(pt);
GetClientRect(hwnd, &rc);
sprintf_s(bf, 100, "%p\n", h);
TextOut(hdc,rc.right/2,rc.bottom/2,bf,strlen(bf));
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
When I press the left button, the handle for setting the coordinates is displayed in the middle of the screen.

Forcing the recall of WM_PAINT

I've created a window using CreateWindowEx and that is working fine. The window opens.
In the function where I process messages, specifically WM_PAINT, I grab the cursor position using GetCursorPos and then draw a rectangle there. However, I would like the window to continuously update and redraw this rectangle, acting like a cursor.
I've created a thread like the following to try and force this:
DWORD WINAPI RedrawLoop(LPVOID lpParam) {
HWND handle = (HWND)lpParam;
while (true) {
RECT lpRect;
GetClientRect(handle, &lpRect);
InvalidateRect(handle, &lpRect, TRUE);
UpdateWindow(handle);
}
return 0;
}
However, this is not working. I've already checked that the handle passed in is the same as the window handle outside the thread.
I've also tried sending SendMessage(handle, WM_PAINT, 0, 0); and RedrawWindow(handle, NULL, NULL, 0); continuously with no luck.
First, you can get the current cursor position by processing the WM_MOUSEMOVE message and invalidate the current window.
Then draw a rectangle through the Rectangle function in the WM_PAINT message. (Note: The origin of coordinates is the upper left corner of the screen)
Here is the sample:
#include <Windows.h>
#include <commctrl.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static POINT pt;
switch (message)
{
case WM_CREATE:
{
}
case WM_MOUSEMOVE:
{
Sleep(100);
GetCursorPos(&pt);
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
Rectangle(hdc, pt.x-rect.right/4 , pt.y-rect.bottom/4, pt.x + rect.right / 4, pt.y + rect.bottom / 4);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

Win32 - Child Window Painting

I have a main window that contains two child windows (Child1 & Child2). Moving Child2 on top of Child1 and then moving Child1 away, the Child 2 client area and non-client area gets painted onto Child 1.
See image here: child windows
Any help please?
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndChildProc1(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndChildProc2(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
{
WNDCLASS wndclass;
HWND hwndMain, hwndChild1, hwndChild2;
MSG msg;
//register main window class
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hIcon = 0;
wndclass.hCursor = LoadCursor(0, IDC_ARROW);
wndclass.hInstance = hInstance;
wndclass.hbrBackground = (HBRUSH) COLOR_WINDOW;
wndclass.lpszMenuName = 0;
wndclass.lpszClassName = TEXT("MainWindow");
if(!RegisterClass(&wndclass)) return 0;
//register child window class
wndclass.lpfnWndProc = WndChildProc1;
wndclass.lpszClassName = TEXT("ChildWindow1");
if(!RegisterClass(&wndclass)) return 0;
wndclass.lpfnWndProc = WndChildProc2;
wndclass.lpszClassName = TEXT("ChildWindow2");
if(!RegisterClass(&wndclass)) return 0;
//create main window
hwndMain = CreateWindow(TEXT("MainWindow"), TEXT("Main Window"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
0, 0, hInstance, 0);
if(!hwndMain) return 0;
//create child windows
hwndChild1 = CreateWindow(TEXT("ChildWindow1"), TEXT("Child Window 1"),
WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_VISIBLE,
10, 10, 200, 300,
hwndMain, 0, hInstance, 0);
hwndChild2 = CreateWindow(TEXT("ChildWindow2"), TEXT("Child Window 2"),
WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_VISIBLE,
250, 10, 200, 300,
hwndMain, 0, hInstance, 0);
ShowWindow(hwndMain, SW_SHOW);
UpdateWindow(hwndMain);
while(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
LRESULT CALLBACK WndChildProc1(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, msg, wParam, lParam);
}
LRESULT CALLBACK WndChildProc2(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Thanks.

How to create a Windows-style textbox in a C++ Win32 application

I tried this:
#include <windows.h>
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR nCmdLine, int nCmdShow)
{
LPTSTR windowClass = TEXT("WinApp");
LPTSTR windowTitle = TEXT("Windows Application");
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WndProc;
wcex.lpszClassName = windowClass;
wcex.lpszMenuName = NULL;
wcex.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_ICONERROR);
return EXIT_FAILURE;
}
HWND hWnd;
if (!(hWnd = CreateWindow(windowClass, windowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL)))
{
MessageBox(NULL, TEXT("CreateWindow Failed!"), TEXT("Error"), MB_ICONERROR);
return EXIT_FAILURE;
}
HWND hWndEdit = CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 140, 20, hWnd, NULL, NULL, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return EXIT_SUCCESS;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(EXIT_SUCCESS);
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return FALSE;
}
I use:
CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 140, 20, hWnd, NULL, NULL, NULL);
to create a textbox, and it's just an ugly black-solid-border textbox.
How to create a windows style textbox (with a 3D light blue border)?
Instead of using CreateWindow, use CreateWindowEx and specify WS_EX_CLIENTEDGE as the first parameter.
You can compare the styles of your created edit control with a stock one (for example, when you show 'Properties' on a file in the explorer) with the Spy++ tool that comes along Visual Studio.
The OP edited his question, removing the original code and therefore invalidating the answers, saying:
This is a simple example to create a window with a textbox.
I rolled it back to restore the original code, but I thought the working example was nice, so here it is:
#include <windows.h>
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR nCmdLine, int nCmdShow)
{
LPTSTR windowClass = TEXT("WinApp");
LPTSTR windowTitle = TEXT("Windows Application");
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WndProc;
wcex.lpszClassName = windowClass;
wcex.lpszMenuName = NULL;
wcex.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"),
MB_ICONERROR);
return EXIT_FAILURE;
}
HWND hWnd;
if (!(hWnd = CreateWindow(windowClass, windowTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL)))
{
MessageBox(NULL, TEXT("CreateWindow Failed!"), TEXT("Error"), MB_ICONERROR);
return EXIT_FAILURE;
}
HWND hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("test"),
WS_CHILD | WS_VISIBLE, 100, 20, 140,
20, hWnd, NULL, NULL, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return EXIT_SUCCESS;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(EXIT_SUCCESS);
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return FALSE;
}
In Codeblocks put manifest file near project:
program_name.exe.manifest
And to program_name.exe.manifest write this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="*"
name="CompanyName.ProductName.YourApplication"
type="win32"
/>
<description>Program name</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
And then your program look like this: