i am trying to show a bmp in the screen using this code: (this code is from a book but i cant show the windows when i compile only appear the white window)
#include "resource2.h"
#include <vector>
#include <windows.h>
using namespace std;
HWND window1;
HWND window2;
HINSTANCE happ;
HDC handledevice;
PAINTSTRUCT ps;
HBITMAP hbitmap;
BITMAP bitmap;
HDC bmhdc;
HBITMAP oldbm;
LRESULT CALLBACK WindTyp1(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
switch (msg)
{
case WM_KEYDOWN:
if (wparam==VK_ESCAPE)
{
DestroyWindow(window1);
}
return 0;
case WM_PAINT:
handledevice=BeginPaint(hwnd,&ps);
BitBlt(handledevice,0,0,bitmap.bmWidth,bitmap.bmHeight,bmhdc,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
return 0;
}
return DefWindowProc(hwnd,msg,wparam,lparam);
}
LRESULT CALLBACK WindTyp2(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
switch (msg)
{
case WM_KEYDOWN:
if (wparam==VK_F1)
{
DestroyWindow(window1);
}
if (wparam==VK_F2)
{
DestroyWindow(window2);
}
if (wparam==VK_ESCAPE)
{
DestroyWindow(window1);
DestroyWindow(window2);
}
}
return DefWindowProc(hwnd,msg,wparam,lparam);
}
int WINAPI WinMain(HINSTANCE histance,HINSTANCE hprevinstance,PSTR cmdline,int showcmd)
{
happ=histance;
MSG msg;
hbitmap=LoadBitmap(happ,MAKEINTRESOURCE(happ,IDB_BITMAP1));
GetObject(hbitmap,sizeof(BITMAP),&bitmap);
bmhdc=CreateCompatibleDC(handledevice);
SelectObject(bmhdc,&bitmap);
//clase 1
WNDCLASS windowstyle1,windowstyle2;
windowstyle1.cbClsExtra=0;
windowstyle1.cbWndExtra=0;
windowstyle1.hbrBackground=(HBRUSH) ::GetStockObject(WHITE_BRUSH);
windowstyle1.hCursor=::LoadCursor(0,IDC_ARROW);
windowstyle1.hIcon=::LoadIcon(0,IDI_APPLICATION);
windowstyle1.hInstance=histance;
windowstyle1.lpfnWndProc=WindTyp1;
windowstyle1.lpszClassName="Class 1";
windowstyle1.lpszMenuName=0;
windowstyle1.style= CS_HREDRAW | CS_VREDRAW;
//clase 2
windowstyle2.cbClsExtra=0;
windowstyle2.cbWndExtra=0;
windowstyle2.hbrBackground=(HBRUSH) ::GetStockObject(BLACK_BRUSH);
windowstyle2.hCursor=::LoadCursor(0,IDC_ARROW);
windowstyle2.hIcon=::LoadIcon(0,IDI_APPLICATION);
windowstyle2.hInstance=histance;
windowstyle2.lpfnWndProc=WindTyp2;
windowstyle2.lpszClassName="Class 2";
windowstyle2.lpszMenuName=0;
windowstyle2.style= CS_HREDRAW | CS_VREDRAW;
//registrar ambas clases
RegisterClass(&windowstyle1);
RegisterClass(&windowstyle2);
//crear ventanas
window1=::CreateWindow("Class 1","Ventana Blanca",WS_OVERLAPPEDWINDOW,0,0,1400,1000,0,0,happ,0);
if (window1==0)
::MessageBox(0,"error failed to create window","error",0);
//Show & Update
ShowWindow(window1,true);
UpdateWindow(window1);
//Message Loop
ZeroMemory(&msg,sizeof(MSG));
while (GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Have a look at this example here
http://www.functionx.com/win32/Lesson13.htm
bmhdc=CreateCompatibleDC(handledevice);
IN WinMain you're using handledevice - which isn't yet initialised, so selecting the bitmap would cause problems, but it's probably 0, so it would use the desktop DC. Usually I wouldn't do this in WinMain but in paint like in the example in the url.
hth
You are making life harder for yourself than necessary. The way you are expected to do this is with a STATIC window with the SS_BITMAP style.
I realise this is not an indirect answer to your question but I offer it since it will make your task much simpler - let the system do the heavy lifting for you!
Related
This question already has an answer here:
CreateWindow() in Visual C++ always return null
(1 answer)
Closed 4 years ago.
I was trying to make application using win32 and directX now, but I stuck at the first step of my project, which is making window. I tried many things to fix, but it keep saying, failed to create window which means hWnd is null.
This is my code.
app.h
#include <windows.h>
#include <string>
class App
{
public:
App (void);
~App (void);
HRESULT InitApp (HINSTANCE hInstance, int nCmdShow);
//void UpdateApp (void);
HWND window(void) { return m_hWnd; }
private:
static LRESULT CALLBACK WndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam);
private:
HINSTANCE m_hInstance;
HWND m_hWnd;
RECT m_rect;
static bool s_running;
std::string m_className;
}
app.cpp
#include "app.h"
// std::string, c.str()
#include <cstring>
#include <iostream>
bool App::s_running = true;
App::App(void)
{
}
App::~App(void)
{
}
HRESULT App::InitApp(HINSTANCE hInstance, int nCmdShow)
{
m_hInstance = hInstance;
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = App::WndProc;
wc.hInstance = m_hInstance;
wc.lpszClassName = "Hello" ;
RECT rc = {0, 0, 800, 600};
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
if(!RegisterClassEx(&wc))
{
OutputDebugString("Failed to register win class\n");
return E_FAIL;
}
m_hWnd = CreateWindow("Hello", "Hello",
WS_OVERLAPPED | WS_CAPTION | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
nullptr, nullptr, m_hInstance,
nullptr);
if(!m_hWnd)
{
OutputDebugString("Failed to create window\n");
return E_FAIL;
}
ShowWindow(m_hWnd, nCmdShow);
return S_OK;
}
LRESULT CALLBACK App::WndProc(
HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_CREATE:
{
} break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
} break;
default:
{
DefWindowProc(hwnd, msg, wParam, lParam);
}
}
return 0;
}
and usage in main.cpp
#include "app.h"
#include "graphics.h"
// test comment
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)
{
App app;
Graphics graphics(app.window());
if(FAILED(app.InitApp(hInstance, nCmdShow)));
return 0;
/*if(FAILED(graphics.InitGraphics()))
{
graphics.CleanUpDevice();
return 0;
}i*/
MSG msg = {0};
while(WM_QUIT != msg.message)
{
if(PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//graphics.Render();
}
}
//graphics.CleanUpDevice();
return 0;
}
I have researched many of things like this, but nothing worked. I think I missed something very little...but I couldn't find anything. Please help out this pity noob coder....
WndProc does not return the value returned by DefWindowProc so when window receives WM_NCCREATE it will still return 0 indicating that window should not be created. Note that returning 0 from WM_CREATE is fine. Window proc should look like this:
LRESULT CALLBACK App::WndProc(
HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
LRESULT result{};
switch(msg)
{
case WM_CREATE:
{
result = 0;
} break;
case WM_PAINT:
{
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
result = 0;
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
result = 0;
} break;
default:
{
result = DefWindowProc(hwnd, msg, wParam, lParam);
}
}
return result;
}
I am creating a program of painting a text in multiline edit using windows api in c++. It consists of WM_PAINT message which paints the text in client area, but the caret shows unexpected behaviour in edit. It sometimes does not print characters, sometimes printed characters hides.
The code is
#define UNICODE
/*
**
**ALL HEADER FILES
**
*/
#include<wchar.h>
#include<windows.h>
#include <errno.h>
#include<conio.h>
#include<iostream>
#include<stdio.h>
#include<resource.h>
/*
**
**ALL DEFINES CONSTANT
**
*/
#define WINVER 0x0A00
#define _WIN32_WINNT 0x0A00
LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
using namespace std;
HWND hwnd_handle;
HWND hw;
HINSTANCE his;
int WINAPI WinMain(HINSTANCE hvalue,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
his=hvalue;
/////com=nCmdShow;
LPWSTR myname=L"MY WINDOWS CLASS";
MSG msg={0};
WNDCLASSW wc;
wc.hbrBackground=(HBRUSH)COLOR_BTNFACE;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.lpszMenuName=NULL;
wc.lpszClassName=myname;
wc.hCursor=NULL;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.style=0;
wc.hInstance=hvalue;
wc.lpfnWndProc=WindowProcedure;
if(!RegisterClassW(&wc))
{
return 0;
}
///////main window
hwnd_handle=CreateWindowW(myname,L"CODE EDITOR",WS_OVERLAPPEDWINDOW,240,80,600,600,NULL, NULL,hvalue, NULL);
ShowWindow(hwnd_handle,nCmdShow);
UpdateWindow(hwnd_handle);
////////HWND hwnd=CreateWindowW(L"STATIC",L"CODE EDITOR",WS_OVERLAPPEDWINDOW,24,8,60,6,hwnd_handle, NULL,hInstance, NULL);
while(GetMessageW(&msg,NULL,NULL,NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
/////main_structure(hwnd);
hw=CreateWindowW(TEXT("edit"),TEXT(""), WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL ,3,0,600,600,hwnd,NULL,his,NULL);
}
break;
case WM_COMMAND:
{
/////menu_function(hwnd,wParam);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT info;
HBRUSH hbrush;
HDC hdc = BeginPaint(hw,&ps);
wcout<<"vishal";
TextOut(hdc,20,20,L"vishak",6);
EndPaint(hw,&ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
break;
}
}
///////all men
Since you are using an edit control as the client area of your window don't handle WM_PAINT yourself; instead add the text to the edit control (using SendMessage with EM_SETTEXT or possibly EM_REPLACESEL) and let it do the drawing.
Also, you should handle WM_SIZE and set the edit control to the actual client area size.
I want the text change as time changes like a clock, however, it doesn't change. I found that the text will change when I minimize or maximize the window.
I guess I should redraw the window, but I am new to windows api, anyone good advice?
This is the main.cpp codeļ¼
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
//....
}
void Paint(HWND hwnd, LPCTSTR txt)
{
UpdateWindow(hwnd);
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
DrawText(hdc, txt, -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);
}
// Thread function
DWORD WINAPI ThreadFun(LPVOID lpParameter)
{
HWND hwnd = (HWND)lpParameter;
while (1)
{
string dateStr = Ticker::GetCurrentTimeStr();
Paint(hwnd, dateStr.c_str());
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
CreateThread(NULL, 0, ThreadFun, hwnd, 0, NULL);
}
return 0;
case WM_PAINT:
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
You need to call InvalidateRect to tell the system the drawing area has changed.
Edit
Instead of creating a new thread, you can create a timer with SetTimer (see example) and respond to WM_TIMER message. Call InvalidateRect in response to WM_TIMER, to repaint the window every second.
Do all of the painting in response to WM_PAINT.
Use BeginPaint/EndPaint only in response to WM_PAINT, don't use BeginPaint/EndPaint elsewhere.
main.cpp (dll) http://pastebin.com/Z811YSz7
Funkcje.h http://pastebin.com/siZrUBh0
When i injected this dll to a game then i dont see letters.
When i maked drawing function in a while(1) then the letters are appearing and disappearing.
How to make the drawing function to be on the top of this game all the time.
#include <windows.h>
#include <vector>
#include "Funkcje.h"
#include <cstdio>
#include <string>
BOOL WINAPI DllMain(
HINSTANCE hinstDLL,
DWORD fdwreason,
LPVOID lpReversed)
{
switch (fdwreason)
{
case DLL_PROCESS_ATTACH:
PAINTSTRUCT ps;
HDC hdc;
for (size_t i = 0; i < getToplevelWindows().size(); i++){
hdc = GetDC(getToplevelWindows()[i]);
TextOut(hdc, 150, 150, L"TEST", 4);
ReleaseDC(getToplevelWindows()[i], hdc);
}
break;
}
return TRUE;
}
DllMain is only called once, when the DLL is loaded. This is not where you should be putting your code.
In order to properly "inject" code into a process (via DLL or any other mean) you must first determine what function or event in that process you want to hook.
In this case, I suppose you want to hook the window event WM_PAINT, a callback from the system sent to the window's procedure telling it that it should repaint its content for whatever reason. (This may not be always correct though, because a program can use other methods of updating a window content.)
So what you are going to do is find the window handle you want to hook, and replace its callback procedure with your own:
WNDPROC originalProc = SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)myHookProc);
But of course, not before having myHookProc defined and doing its job of hooking WM_PAINT:
LRESULT CALLBACK myHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT originalResult = originalProc(hWnd, uMsg, wParam, lParam); // call original first
if (uMsg == WM_PAINT)
{
PAINTSTRUCT ps;
HDC hdc = GetDC(hWnd);
TextOut(hdc, 150, 150, L"TEST", 4);
ReleaseDC(hWnd, hdc);
}
return originalResult;
}
If you are dealing with multiple windows, you may want to use a std::unordered_map<HWND, WNDPROC> to keep relation between each window handle and its original procedure.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Code:
#include "CST CAL.h"
int SUSU()//Start Up
{
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2,2), &WSAData) != 0)
{
printf("WSASTATUP Falure: ", WSAGetLastError);
Sleep(1200);
return 1;
}
return 0;
}
int SR()//Send and Recive Data
{
return 0;
}
int DirX_Screen()//Render and Buffrer stuff and put on screen
{
return 0;
}
int SHUT()//ShutDown Program
{
return 0;
}
int GDI()//Get User Data Imput
{
return 0;
}
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR 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 _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
//HERE IS CODE
if (SUSU() != 0)
{
return 1;
}int C = 0;
do{while(GetForegroundWindow() == GetForegroundWindow()){
int GeDI = GDI();
int SeR = SR();
int DirX = DirX_Screen();
if( GeDI != 0 || SeR != 0 || DirX != 0){C = 1;}else{printf("FR Suc\n");}
Sleep(0);
}}while (C == 0);
SHUT();
//HERE IS END OF CODE SPACE
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));
// Main message loop:
while (GetMessage(&msg, NULL, 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)
{
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, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&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)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
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)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// 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_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
This is originally a console application but then i use this template and just placed my code... my loop is supposed to print out the text but its not... it doesn't eve make the window. Please Help!
This is originally a console application but then i use this template
and just placed my code...
I wish things were that easy :)
You can't switch from console programming to the event driven one that easily. Maybe you should go through this tutorial.
my loop is supposed to print out the text but its not...
In winapi we usually use MessageBox to display text, or you could change the text of the static control or even pop a tooltip. This is done usually in response to a certain event, like button press for an example.
it doesn't eve make the window.
This happens because you have an infinite while loop before your call to InitInstance( ... ).
Your condition while(GetForegroundWindow() == GetForegroundWindow()) is always true so you never leave the while loop, thus never reaching the code that creates the window ( InitInstance( ... ) ).
Please Help!
After you go through the tutorial I recommended above, you will be able to recode properly your application.
I do not know enough about what you are trying to do, but I will try to offer you some advice:
Put your initialization code in WM_CREATE handler:
case WM_CREATE:
{
if( SUSU() != 0 )
return (LRESULT)-1;
else
return (LRESULT)0;
}
In your WM_DESTROY you should put your cleanup code ( note that WM_CLOSE sometimes does not get called! ):
case WM_DESTROY:
{
SHUT();
}
Your GDI() should be a part of an event, maybe a button press-I don't know it is entirely up to you. The same goes for SR(). They will be processed in WM_COMMAND handler.
I do not know DirectX so I do not know where to put your DirX_Screen(), but you could leave fancy graphics last and concentrate on functionality first.
Hopefully this helps a little.
I strongly advise you to go through that tutorial and to learn to read MSDN documentation.
Best regards and good luck!