 |
ActiveDelphi .: O site do programador Delphi! :.
|
| Exibir mensagem anterior :: Exibir próxima mensagem |
| Autor |
Mensagem |
charlo Aprendiz

Registrado: Quinta-Feira, 13 de Julho de 2006 Mensagens: 178
|
Enviada: Ter Out 13, 2009 5:39 pm Assunto: Selecionar (linha) no DBGrid com o botão direito (RESOLVIDO) |
|
|
Olá gente boa tarde.
Não sei se alguém poderia me ajudar.
Eu tenho um DBGrid e um popup menu para esse DBGrid, eu gostaria que quando o usuário clicasse com o botão direito do mouse em algum registro nesse DBGrid o mesmo fosse selecionado como se faz com o botão esquerdo do mouse e depois aparecesse o popup menu.
Ou seja, a mesma função, idêntica quando você clica com o botão direito do mouse no Windows explorer e o mesmo seleciona o arquivo clicado com o botão direito e aparace o popup menu.
Se alguém puder me ajudar eu agradeço.
Obrigado mais uma vez gente.
Abraços,
Charlo.
Editado pela última vez por charlo em Sex Out 30, 2009 5:43 pm, num total de 1 vez |
|
| Voltar ao Topo |
|
 |
charlo Aprendiz

Registrado: Quinta-Feira, 13 de Julho de 2006 Mensagens: 178
|
Enviada: Qua Out 14, 2009 6:05 pm Assunto: |
|
|
up  |
|
| Voltar ao Topo |
|
 |
charlo Aprendiz

Registrado: Quinta-Feira, 13 de Julho de 2006 Mensagens: 178
|
Enviada: Sáb Out 17, 2009 3:07 pm Assunto: |
|
|
Gente, eu acho que consegui a solução mas não estou sabendo usar esse código que encontrei. Alguém poderia me ajudar a usar esse código?
Eu tenho que criar uma nova unit e salvar na pasta do meu projeto ou adaptar tudo isso que está abaixo no meu projeto?
Obrigado mais uma vez pessoal.
Add a New Event to a Component: Right-Click in a TDBGrid
Question:
I need to add a right-click event to a TDBGrid, but am at a loss as to how to accomplish it. I want to pop up a menu. Do you know how?
Answer:
Before I start into this, there is an easier way to handle a right-mouse click to pop up a menu of sorts: Drop a TPopupMenu on a form and set its AutoPopup to True, then set the DBGrid's PopupMenu property to the name of the TPopupMenu.
It's amazing how easy it is (at least in many cases) to add functionality to a component. Mouse clicks are problably the easiest to implement, because the message structure of the windows message is fairly simple -- it's only one parameter!
unit Extgrid;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Grids, DBGrids;
type
TExtDBGrid = class(TDBGrid)
private
{ Private declarations }
FOnRightClick : TNotifyEvent;
procedure WMRButtonDown(var Message : TWMRButtonDown); message
WM_RBUTTONDOWN;
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property OnRightClick: TNotifyEvent read FOnRightClick write FOnRightClick;
end;
procedure Register;
implementation
procedure TExtDBGrid.WMRButtonDown(var Message : TWMRButtonDown);
begin
inherited;
if Assigned(FOnRightClick) then
FOnRightClick(Self);
end;
procedure Register;
begin
RegisterComponents('Samples', [TExtDBGrid]);
end;
end.
Okay, what did we just do? First of all we defined an event handling procedure:
procedure WMRButtonDown(var Message : TWMRButtonDown); message WM_RBUTTONDOWN;
the message directive following the declaration tells the component to watch for the message WM_RBUTTONDOWN, which is the message for the right mouse button down. What we've done with this procedure is make the component look for this message. However, mere awareness isn't enough, by far! We have to add a property of type TNotifyEvent (which if you remember is the domain in which mouse clicks fall) so that users can add code to process it. So, in the published section of the unit, we added:
property OnRightClick: TNotifyEvent read FOnRightClick write FOnRightClick;
to assign the message to the component.
If you're familiar with writing components, you'll notice the difference in how message properties are handled as opposed to regular properties. Typically, when setting and getting property values you would use a Get or Set method or a direct read and write to the properties' fields. In essence, things happen in response to the change in state of the property. This appears to occur in the notation of the unit above, but there's actually a bit more going on. In fact, it's a bit backwards. The FOnRightClick is not really a variable, but rather a pointer to an event-handling procedure. Luckily we don't have to know the mechanics behind this, because the compiler will automatically do all the work for us in creating the handler's declaration. However, if we look at the WMRButtonDown procedure, we'll see that the variable is actually being used as a procedure call:
procedure TExtDBGrid.WMRButtonDown(var Message : TWMRButtonDown);
begin
inherited;
if Assigned(FOnRightClick) then
FOnRightClick(Self);
end;
As I mentioned above, message handling works sort of in reverse of regular property setting. Where with a regular property, the change in the state of the property causes some code to fire off with an event, it's a message-aware procedure that sits in memory that changes the state of the message property. To illustrate this, in the code example above, the WMRButtonDown procedure makes the component aware of the right-click. Whenever the message appears in the message queue, WMRButtonDown fires. When it fires, it in turn fires off the FOnRightClick procedure, which on the component would be the event handler OnRightClick.
Use this code as a basis for writing your own events. The online help under WinAPI help has excellent discussions of messages and how they interact with the system. In addition to this though, I strongly recommend getting a book on the Windows API and getting the Resource Kit and SDK to really get into event handling.
Abraços,
Charlo |
|
| Voltar ao Topo |
|
 |
charlo Aprendiz

Registrado: Quinta-Feira, 13 de Julho de 2006 Mensagens: 178
|
Enviada: Sex Out 30, 2009 5:41 pm Assunto: |
|
|
Bem gente, aqui está a rotina que inventei para poder dar certo o que eu queria.
| Código: | procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
vlLinha: Integer;
begin
begin
//Aqui verifico se o botão pressionado é o botão direito do mouse
if Button = mbRight then
begin
//Aqui verifico se os registros selecionados são maiores que 1 (no caso de a opção Multiselect do DBGrid estiver em True)
if DBGrid1.SelectedRows.Count > 1 then
begin
//Aqui verifico se o registro selecionado está em foco (esse é o tal do HighLigh)
if DBGrid1.SelectedRows.CurrentRowSelected = True then
begin
//Se é verdade então o CheckBox1 será desmarcado
CheckBox1.Checked := False;
end
else
begin
//Se é falso vai ser deselecionado...
DBGrid1.UnSelectAllClick(DBGrid1.SelectedRows);
begin
//...e será feito uma varrida nos registros do DBGrid...
with DBGrid1.DataSource.DataSet do
begin
for vlLinha := 0 to RecordCount - 1 do
begin
//...e será selecionado somente aquele registro em que você apontou e clicou com o botão direito do mouse
DBGrid1.SelectedRows.CurrentRowSelected := True;
end;
end;
//Atualizo os registros selecionados do DBGrid
DBGrid1.SelectedRows.Refresh;
end;
end;
end
else
begin
//Aqui começa a segunda parte da condição principal. Se for menor que 1 então o CheckBox1 será desmarcado e o DBGrid1 será todo deselecionado
CheckBox1.Checked := False;
DBGrid1.UnSelectAllClick(DBGrid1.SelectedRows);
begin
//Varro novamente os registros do DBGrid...
with DBGrid1.DataSource.DataSet do
begin
for vlLinha := 0 to RecordCount - 1 do
begin
//...e será selecionado somente aquele registro em que você apontou e clicou com o botão direito do mouse
DBGrid1.SelectedRows.CurrentRowSelected := True;
end;
end;
//Atualizo os registros selecionados do DBGrid
DBGrid1.SelectedRows.Refresh;
end;
end;
end;
end;
end; |
OBS.: Eu usei o componente SMDBGrid muito conhecido entre muitos (ele já vem com funções de SelectAll e UnselectAll). Notem também que tive que usar um CheckBox para poder ficar fazendo as verificações necessárias. E mais uma nota, o CheckBox1 deve ser iniciado com a opção Checked em True e não esqueçam de colocar um PopupMenu apontando para o DBGrid.
Eu programo só por distração, não sou nenhum profissional nem nada, adoro o Delphi e programo por prazer. Se a minha rotina falhar ou estiver em desacordo com no que diz respeito a estrutura ou "poderia ser feito com menos linhas de código de outra maneira" eu peço desculpas. A minha intenção foi apenas ajudar sobre um problema que eu estava tendo e só consegui fazer dessa maneira como descrevi.
Abraços,
Charlo. |
|
| Voltar ao Topo |
|
 |
|
|
Enviar Mensagens Novas: Proibido. Responder Tópicos Proibido Editar Mensagens: Proibido. Excluir Mensagens: Proibido. Votar em Enquetes: Proibido.
|
|