Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Custom Controls is annoying me...

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Fri Feb 12, 2010 9:30 am    Post subject: Custom Controls is annoying me... Reply with quote

Im attempting to create this control to be used for my Disassembler, however it simply desided not to work...

For some reason, it keeps doing WM_PAINT, causing it to loop infinitively, hooking up 50% of my CPU (1 core runs on 100%)
Second, FillRect keeps making annoying patterns on my background instead of colors, why??? Sad, for once i attempted to be a little organized in my coding, and created a class for my windows

CustomControl.h
Code:
#include <Windows.h>

class CustomControl{
public:
   CustomControl(HINSTANCE hInstance, HWND hWndParent, int X, int Y, int nWidth, int nHeight);
   ~CustomControl();
   BOOL registerClass(HINSTANCE hInstance);
   //Window Handlers
   LRESULT OnPaint();
   //Window Procedure
   static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
private:
   HWND hWnd;
};


CustomControl.cpp
Code:
#include <stdio.h>
#include "CustomControl.h"

#define CUSTOM_CLASS "custcls"

CustomControl::CustomControl(HINSTANCE hInstance, HWND hWndParent, int X, int Y, int nWidth, int nHeight){
   if(!registerClass(hInstance))
      return;
   HWND hWnd = CreateWindowEx(WS_EX_STATICEDGE, CUSTOM_CLASS, NULL, WS_VISIBLE | WS_CHILD, X, Y, nWidth, nHeight, hWndParent, NULL, hInstance, NULL);
}

CustomControl::~CustomControl(){

}

BOOL CustomControl::registerClass(HINSTANCE hInstance){
   WNDCLASSEX wc;
   if(GetClassInfoEx(hInstance, CUSTOM_CLASS, &wc))
      return TRUE;
   wc.cbSize        = sizeof(WNDCLASSEX);
   wc.style         = 0;
   wc.lpfnWndProc   = WndProc;
   wc.cbClsExtra    = 0;
   wc.cbWndExtra    = sizeof(CustomControl*);
   wc.hInstance     = hInstance;
   wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
   wc.lpszMenuName  = NULL;
   wc.lpszClassName = CUSTOM_CLASS;
   wc.hIconSm       = 0;
   if(!RegisterClassEx(&wc)){
      MessageBox(NULL, "Window Registration Failed!", "Error!",
      MB_ICONEXCLAMATION | MB_OK);
      return FALSE;
   }
   return TRUE;
}

LRESULT CustomControl::OnPaint(){
   HDC hDC = GetDC(hWnd);
   HDC hDCMem = CreateCompatibleDC(hDC);
   RECT rect;
   GetClientRect(hWnd, &rect);
   int iHeight = rect.bottom-rect.top;
   int iWidth = rect.right-rect.left;
   HBITMAP hBM = CreateCompatibleBitmap(hDCMem, iWidth, iHeight);
   HBITMAP hOldBM = (HBITMAP)SelectObject(hDCMem, hBM);
   HBRUSH hbr = CreateSolidBrush(RGB(rand()%255, rand()%255, rand()%255));
   FillRect(hDCMem, &rect, hbr);
   TextOut(hDCMem, 5, 5, "Hello world!", 12);
   BitBlt(hDC, 0, 0, iWidth, iHeight, hDCMem, 0, 0, SRCCOPY);
   SelectObject(hDCMem, hOldBM);
   DeleteObject(hBM);
   DeleteDC(hDCMem);
   ReleaseDC(hWnd, hDC);
    return 0;
}

CustomControl* getCustomControl(HWND hWnd){
   return (CustomControl*)GetWindowLong(hWnd, 0);
}

LONG setCustomControl(HWND hWnd, CustomControl *customControl){
   return SetWindowLong(hWnd, 0, (LONG)customControl);
}

LRESULT CALLBACK CustomControl::WndProc(HWND hWnd, UINT Msg,  WPARAM wParam, LPARAM lParam){
   CustomControl *pcc = getCustomControl(hWnd);
   switch(Msg){
      case WM_NCCREATE:
         pcc = (CustomControl*)malloc(sizeof(CustomControl));
         pcc->hWnd = hWnd;
         setCustomControl(hWnd, pcc);
         return TRUE;
      break;
      case WM_ERASEBKGND:
         return FALSE;
      break;
      case WM_PAINT:
         return pcc->OnPaint();
      break;
      default:
         return DefWindowProc(hWnd, Msg, wParam, lParam);
   }
   return 0;
}


(and yes, i do attempt to do Double Buffering, which i think is why its not working...)



Custom Control.jpg
 Description:
 Filesize:  38.49 KB
 Viewed:  5795 Time(s)

Custom Control.jpg




Last edited by Anden100 on Sat Feb 13, 2010 6:10 am; edited 1 time in total
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Fri Feb 12, 2010 5:20 pm    Post subject: Re: Custom Controls is annoying me... Reply with quote

Anden100 wrote:
Im attempting to create this control to be used for my Disassembler, however it simply desided not to work...

Second, FillRect keeps making annoying patterns on my background instead of colors, why??? Sad, for once i attempted to be a little organized in my coding, and created a class for my windows


Can you get a screenshot of that?
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Feb 13, 2010 6:14 am    Post subject: Re: Custom Controls is annoying me... Reply with quote

slovach wrote:
Can you get a screenshot of that?


Screenshot in original post, keep in mind that i had to put a Sleep(200) when taking the screenshot, it changes so fast, that every time i try to take a screenshot i catch it in the middle of painting (half of the Control is one pattern, and the other half is another pattern), The OnPaint function keeps being called, and the background changes, after some time (a second or two), the backgrounds gets white, and it gets drawn outside the rectangle.

I figured i need to do BeginPaint/EndPaint for the WM_PAINT message, to "validate" it (dunno?) to avoid it from being called again, changing that avoids the loop. But i still don't understand why i get those wierd patterns
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sat Feb 13, 2010 12:44 pm    Post subject: Reply with quote

at first glance, it looks like you're ending up with a monochromatic bitmap somewhere along the lines.


check MSDN and look over your OnPaint() function.
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Feb 13, 2010 2:29 pm    Post subject: Reply with quote

slovach wrote:
at first glance, it looks like you're ending up with a monochromatic bitmap somewhere along the lines.


check MSDN and look over your OnPaint() function.


Allready figured out that i need to use BeginPaint()/EndPaint(), except that i can't figure out what's wrong Sad

And, monochromatic?, thats something about how light the color is right?
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sat Feb 13, 2010 5:16 pm    Post subject: Reply with quote

it means 1 bit, black and white.

the 'Compatable' functions should try and match your desktop color depth, but i think it's possible to end up with a black and white surface under some circumstances.

i don't have time right now to go perusing through MSDN right now, but you should check out the documentation for the functions you're calling. GetObject() may also be useful as it returns information about a GDI object you pass it, so you can check bit depth etc.
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Feb 13, 2010 6:21 pm    Post subject: Reply with quote

slovach wrote:
it means 1 bit, black and white.

the 'Compatable' functions should try and match your desktop color depth, but i think it's possible to end up with a black and white surface under some circumstances.

i don't have time right now to go perusing through MSDN right now, but you should check out the documentation for the functions you're calling. GetObject() may also be useful as it returns information about a GDI object you pass it, so you can check bit depth etc.


That information sure was helpful!, as for what i understand, i then have to CreateCompatibleBitmap using the real HDC instead of the one i got from CreateCompatibleDC?, seems to work, tyvm!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites