473,659 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Paint select function...

27 New Member
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 3811
Banfa
9,065 Recognized Expert Moderator Expert
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 CreateCompatabl eBitmap, 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
InNeedOfHelp
27 New Member
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 CreateCompatabl eBitmap, 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);
CreateCompatibl eBitmap(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 Recognized Expert Moderator Expert
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::Create CompatibleBitma p( CDC* pDC, int nWidth, int nHeight );

Create a device contaxt compatable with another device context
virtual BOOL CDC::CreateComp atibleDC( 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::SelectObje ct( 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
InNeedOfHelp
27 New Member
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);
CreateCompatibl eBitmap(m_hDC, wWidth, wHeight);
CDC *compDevice = GetDC();
CreateCompatibl eBitmap(*compDe vice, wWidth, wHeight);
CreateCompatibl eDC(*compDevice );
CBitmap *bitmap;
compDevice->SelectObject(* bitmap);
compDevice->BitBlt(0,0,wWi dth,wHeight,com pDevice,0,0,SRC COPY);

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
InNeedOfHelp
27 New Member
Sorry should also mention that it is MFC that I am using.
Oct 17 '06 #6
Banfa
9,065 Recognized Expert Moderator Expert
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
4679
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 it does. Can anyone tell me what exactly it does? Thanks, Attila
6
2728
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 Vectors ================================== std::vector<float> gTmpArr(8000,0); In Main, I would like to pass this vector to a function such as:
1
1392
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"; if ($name == $array){array_shift($array);} //removes duplicate when table name equals 1st item in array foreach ($array AS $value) { if (($value == $persist) && is_string($persist)){$selected = "selected";}
4
3629
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 other sockets (I'm using only one task). I've heard that there is an option for a non blocking select, is it true? If not does anyone have any idea?
3
3831
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 selects the same when called on keyUp event. But its my application need that it should get called on keyDown event. Please provide help , if possible Thnks,
1
8179
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 pressed. It then draws a line between the new cursor position and the position stored in the variables, allowing you to sketch with the mouse. What I would like to do is assign a function to repaint the dialogue box to its initial setting (i.e. only...
0
1771
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 the children to finish processing and write to their parent using the appropriate pipe for each children. I know that I may use the select function for this purpose and I have read a lot about it but I don't know how to use it in my program code...
2
1668
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 specific site. Also he has the option to check a checkbox "Remeber this choice" so that when he returns to the site it remembers what he has choosen from the drop down list and automatically takes him to a specific site. Here is my code:
7
1710
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 the same table, and called: , , , and In the query where the result of the outcome in needed, it has the following in a field: Minute: MINUTEOUTCOME(,,,,)
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8523
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.