473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Paint select function...

I am writing an application using MFC and was wanting to include a function similar to the select function available in Paint.
I want to allow the user to grab the screen and drag it where they want to, can anyone give me some pointers as to how I would go about this?

Any help would be greatly appreciated.
Oct 16 '06 #1
6 3798
Banfa
9,065 Expert Mod 8TB
What you will need to do is this, when the set the first point (a rectangle is defined by 2 points) just record it, when they select the second point you have the rectangle to copy. At this point you will probably need to create 2 bitmaps, 1 cotaining the contents of the screen (with the selction removed and 1 containing the selection. These can be created with CreateCompatableBitmap, then you will also need a compatable device context (with the screen) for each bitmap and you will need to select the created bitmaps into the created device contexts. You can then fill in the bitmaps by using BitBlt to copy what is currently on the screen. Then as they are draging the selection around you can use BitBlt to fill in the Window client area from the 2 bitmaps until they have finished. Once finished you can Blt everything back onto the screen and delete the created device contexts and bitmaps.
Oct 16 '06 #2
What you will need to do is this, when the set the first point (a rectangle is defined by 2 points) just record it, when they select the second point you have the rectangle to copy. At this point you will probably need to create 2 bitmaps, 1 cotaining the contents of the screen (with the selction removed and 1 containing the selection. These can be created with CreateCompatableBitmap, then you will also need a compatable device context (with the screen) for each bitmap and you will need to select the created bitmaps into the created device contexts. You can then fill in the bitmaps by using BitBlt to copy what is currently on the screen. Then as they are draging the selection around you can use BitBlt to fill in the Window client area from the 2 bitmaps until they have finished. Once finished you can Blt everything back onto the screen and delete the created device contexts and bitmaps.

Hey Banfa,

This is what I have at the moment (which isn't much);

OnMouseMove(...){
...
CRect rect(0,0,wWidth,wHeight);
CreateCompatibleBitmap(m_hDC, wWidth, wHeight);
CDC compDevice; // device compatible to the screen
?
}
where wWidth & wHeight are the width and height of the screen in pixels.
HDC m_hDC =GetDC(m_hWnd); where m_Wnd is a handle to the window.

I am wanting the whole screen to be dragged which is why there is only a single rectangle - is this correct?

Also I am unsure as to how you select the bitmaps into the device context and use BitBlt?

Cheers
Oct 17 '06 #3
Banfa
9,065 Expert Mod 8TB
Looks about right but don't mix WIN32 and MFC if possible, it will just get confusing, look up the methods

Get a Device Context for the Window
CDC* CWnd::GetDC( );

Create a bitmap compatable with the device context
BOOL CBitmap::CreateCompatibleBitmap( CDC* pDC, int nWidth, int nHeight );

Create a device contaxt compatable with another device context
virtual BOOL CDC::CreateCompatibleDC( CDC* pDC );

Select a bitmap object into a device context, having done this you can use the device context to draw to the bitmap
CBitmap* CDC::SelectObject( CBitmap* pBitmap );

Copy bits of a bitmap between different (or the same) device contexts
BOOL CDC::BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop );


If you are not using MFC then don't use CDC compDevice, use HDC compDevice and all the above functions have WIN32 API equivilents with the same name.
Oct 17 '06 #4
Banfa,
I have tried to add the code as you instructed however I am not getting it right, the code I have is below;

CRect rect(0,0,wWidth,wHeight);
CreateCompatibleBitmap(m_hDC, wWidth, wHeight);
CDC *compDevice = GetDC();
CreateCompatibleBitmap(*compDevice, wWidth, wHeight);
CreateCompatibleDC(*compDevice);
CBitmap *bitmap;
compDevice->SelectObject(*bitmap);
compDevice->BitBlt(0,0,wWidth,wHeight,compDevice,0,0,SRCCOPY) ;

While the code compiles the SelectObject line throws an exception when it is executed (because it has no value, I assume) however I am unsure as to what should be there. Also I don't think that the compDevice (5th arg in BitBlt) that I have put in is correct?

Again any help would be greatly appreciated.
Oct 17 '06 #5
Sorry should also mention that it is MFC that I am using.
Oct 17 '06 #6
Banfa
9,065 Expert Mod 8TB
OK, you are using MFC so I assume you are calling this from a class sub-classed from CWnd

Creation Code
Expand|Select|Wrap|Line Numbers
  1. CClientDC wndDC; //CClientDC autoatically calls GetDC and ReleaseDC
  2.  
  3. CBitmap bitmap;
  4. bitmap.CreateCompatibleBitmap(&wndDC, wWidth, wHeight);
  5.  
  6. CDC bitmapDC;
  7. bitmapDC.CreateCompatibleDC(&wndDC);
  8.  
  9. CBitmap *pOldBitmap = bitmapDC.SelectObject(&bitmap);
  10.  
  11. bitmapDC.BitBlt(0,0,wWidth,wHeight,wndDC,0,0,SRCCOPY); 
  12.  
however these variables

CBitmap bitmap;
CDC bitmapDC;
CBitmap *pOldBitmap;

need to be persistent so that you can call the deletion code

Expand|Select|Wrap|Line Numbers
  1. bitmapDC.SelectObject(pOldBitmap);
  2.  
  3. bitmap.DeleteObject();
  4.  
  5. bitmapDC.DeleteDC();
  6.  
Oct 17 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Attila | last post by:
Hello, I have seen the select function used on forms fields, and it usually takes place immediately after a setfocus call is made. However, after adding the code to a test page, I don't see what...
6
by: Dennis | last post by:
In my program I have defined a global vector with //=======prototypes section ==================== float select(const int k, std::vector<float> &myArr, int iStart, int iEnd); ..... //====Global...
1
by: supline | last post by:
I have a function that creates a select list: function make_select_ns($name,$array,$persist){ $string = "<select name=\"$name\">"; $string .="<option selected>" . ucfirst($name) . "</option>\n";...
4
by: Bari | last post by:
Hi all, My problem is: I'm waiting with select() on a socket array, when data is written in one of the sockets the select awakes. The problem is that untill it finishes I can't listen to the...
3
by: ravip | last post by:
I am trying to select some text in a texbox , on keyDown event. but when i write such simple code its not selecting the text in that textbox , instead its only focusing that textbox. It...
1
by: radders | last post by:
Hi everyone, I've written a simple programme using Visual C++ that assigns the values of the mouse's coordinates to a set of variables each time the mouse is moved to a new position with a button...
0
by: Sonbol | last post by:
Hi, I am new to linux programming. I am using c to develop a program that aims to create many children in order to communicate with their parents through pipes I need the parent to wait for all...
2
by: printline | last post by:
Hi all I have a problem with a form script. The form script should do the following: When a user selects a value from a drop down list and hits the "next" button it should take him to a...
7
by: SteveGod | last post by:
Hi, I'm trying to create a report that will produce automated sets of Committee Minutes for School Appeals, where there are a set range of different outcomes. The field involved are all from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.